问题描述
编辑:我做的东西的填充和读取使用的ViewModels 从视图中的数据,把它称为 ValueInjecter 好得多。
它是由 - 一个ASP .NET MVC示例应用程序
**你可以看到prodinner使用的ViewModels的最佳途径**
使用视图模型来存储映射逻辑是不是一个好主意,因为有重复和SRP违规,但现在随着ValueInjecter我有干净的ViewModels和干映射code
这是旧的东西,不使用它:
的我在做asp.net的MVC进行编辑的东西一个ViewModel模式
当你做一个表格编辑一个实体,你必须把表格上的一些下拉菜单供用户选择一些价值观这种模式是非常有用的
公共类OrganisationBadViewModel
{
// paramterless构造所需的辩论,因为我们会得到从形式的OrganisationViewModel对象后保存方法
公共OrganisationViewModel():这个(新组织()){}
公共OrganisationViewModel(组织O)
{
组织= O;
国=新的SelectList(LookupFacade.Country.GetAll(),ID,描述,CountryKey);
}
//这类型我为谁创建视图模型
公共机构组织{搞定;组; }
... }
有两件事是打扰我。
-
的术语。视图模型是这样的情况是填充,后来由控制器使用一个简单的查看数据。查看一无所知控制器ASP.NET MVC的基础设施是负责选择控制器和相应的操作。控制器处理用户交互。我认为它看起来更像是被动视图比视图模型(我认为通过视图模型你的意思是模型 - 视图 - 视图模型模式)。
-
的细节。用于填充视图数据控制器不应该知道的观点是如何实现的细节。然而OrganisationViewModel.Country披露不必要的细节(SelectListItem是纯粹的视图实现细节)。从而使控制器依赖于视图实现细节。我认为它应该被改变,以避免它。考虑使用一些对象,将持有的数据的国家。
希望这有助于。
EDIT: I made something much better to fill and read data from a view using ViewModels, called it ValueInjecter. http://valueinjecter.codeplex.com/
it is used by http://prodinner.codeplex.com - an ASP.net MVC sample application
**you can see the best way of using ViewModels in prodinner**
using the ViewModel to store the mapping logic was not such a good idea because there was repetition and SRP violation, but now with the ValueInjecter I have clean ViewModels and dry mapping code
That's the old stuff, don't use it:
I made a ViewModel pattern for editing stuff in asp.net mvcthis pattern is useful when you have to make a form for editing an entity and you have to put on the form some drop-downs for the user to choose some values
public class OrganisationBadViewModel
{
//paramterless constructor required, cuz we are gonna get an OrganisationViewModel object from the form in the post save method
public OrganisationViewModel() : this(new Organisation()) {}
public OrganisationViewModel(Organisation o)
{
Organisation = o;
Country = new SelectList(LookupFacade.Country.GetAll(), "ID", "Description", CountryKey);
}
//that's the Type for whom i create the viewmodel
public Organisation Organisation { get; set; }
...
}
There are couple of things that bother me.
The terminology. ViewModel is this case is a simple view data that is populated and later consumed by controller. View knows nothing about controller as ASP.NET MVC infrastructure is responsible for selecting controllers and appropriate actions. Controller handles user interaction. I think it looks more like Passive View than ViewModel (I assume that by ViewModel you mean Model-View-ViewModel pattern).
The details. The controller that populates view data is not supposed to know the details of how the view is implemented. However OrganisationViewModel.Country discloses unnecessary details (SelectListItem is pure view implementation detail). Thus making controller dependent on view implementation details. I think it should be changed to avoid it. Consider using some object that will hold the data for a country.
Hope this helps.
这篇关于ASP.NET MVC视图模型模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!