我的 ViewModel 的构造函数是否应该填充 ViewModel 的数据?
我的 ViewModel 是否应该封装填充它所需的服务?
目前我正在这样做:
public PartialViewResult Companies(SearchViewModel search)
{
search.Summary = _entitySearchService.GetSearchDataSummary(search.SearchExpression);
search.PagedCompanies = _companyService.GetPagedEntities<Company>(search.SearchExpression);
return PartialView(search);
}
但是如果 SearchViewModel
在其构造函数中填充这些属性会怎样?我可以让 StructureMap 将接口传递给服务。这是 ViewModel 的有效使用吗?
Should my ViewModel encapsulate the Services needed to populate it?
Currently I'm doing it as follows:
public PartialViewResult Companies(SearchViewModel search)
{
search.Summary = _entitySearchService.GetSearchDataSummary(search.SearchExpression);
search.PagedCompanies = _companyService.GetPagedEntities<Company>(search.SearchExpression);
return PartialView(search);
}
But what if SearchViewModel
populated these properties in its constructor? I could get StructureMap to pass in the interfaces to the services. Would that be a valid use of a ViewModel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
观看这个精彩视频让你的控制器节食。它涵盖了您正在寻找的东西,甚至更多。
check this amazing video Put your controller on a diet. It covers the things which you are looking for and even more.
不,不要这样做,因为您将在不同的视图模型中重复使用相同类型的属性,请从此处查看示例 asp.net MVC 项目: http://valueinjecter.codeplex.com
No, don't do that because your going to have repetition for the same type of properties in different viewmodels, look at the sample asp.net MVC project from here: http://valueinjecter.codeplex.com