MVVM: Should a VM object expose an M object directly, or only through getters delegating to M's getters?

后端 未结 4 2106
暗喜
暗喜 2020-12-23 11:35

the best way to explain is with example so:

this is the model

public class Person 
{
    public int age;
    public string name;
}

4条回答
  •  死守一世寂寞
    2020-12-23 12:30

    An interesting solution to this was proposed by Robert McCarter in MSDN volume 25.

    http://msdn.microsoft.com/en-us/magazine/ff798279.aspx

    He uses a dynamic View Model to provide a layer on top of the Model while avoiding proxying all of the Model properties.

    If your problem space doesn't require high performance (dynamics do incur a performance hit), this is an excellent solution. The View does not need to know anything about the Model, but the View Model does not have to proxy properties that are being provided "as is." At any time properties can be added to the View Model to wrap the Model properties without modifying the View or the Model. Read the article for more details.

提交回复
热议问题