Should my ViewModel's constructor populate the ViewModel's data?
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.GetSear开发者_运维百科chDataSummary(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?
check this amazing video Put your controller on a diet. It covers the things which you are looking for and even more.
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
精彩评论