开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜