views:

119

answers:

2

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?

+1  A: 

Hi DaveDev, check this amazing video Put your controller on a diet. It covers the things which you are looking for and even more.

Sergey
A: 

No, don't do that cuz 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

Omu