Is the ViewModel in asp.net comparable to the ViewModel in WPF

给你一囗甜甜゛ 提交于 2019-12-08 02:31:16

问题


If you know the MVVM pattern for WPF then you know Josh smith msdn article where a CustomerViewModel does not hold a simple property like:

public string FirstName {get;set;}

Rather a ViewModel wraps a Model and delegates the property access like this:

public string FirstName
{
    get { return _customer.FirstName; }
    set
    {
        if (value == _customer.FirstName)
            return;
        _customer.FirstName = value;
        base.OnPropertyChanged("FirstName");
    }
}

I have not seen this in asp.net mvc. Is this due to the missing INotifyPropertyChanged interface?


回答1:


I have not seen this in asp.net mvc

That's normal. You shouldn't see it. MVC is a different pattern than MVVM. In MVC the view has nobody to notify of any changes. The MVVM pattern is not adapted to the stateless nature of the web.




回答2:


The ViewModel and Model pieces from MVVM have a different definition than when used in MVC

In MVVM, the ViewModel is your application, while the View just provides a user-friendly interface for it. In MVC, the View is your application, the ViewModel provides data for it, and the Controller handles application flow and logic.

The Models are also different between the two patterns. In MVC, the M represents both data models and view models, while in MVVM the M only represents data models.

To summarize, MVC's M+C is equal to MVVM's VM, and MVC's M contains a mix of both MVVM's M and VM pieces

As a side note, the INotifyPropertyChanged interface is used by WPF to automatically update the UI when a property changes. This sort of thing is not used in MVC, so not needed.



来源:https://stackoverflow.com/questions/11213644/is-the-viewmodel-in-asp-net-comparable-to-the-viewmodel-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!