20

In my Controller I have a ProductInfo class from my Domain Model and I need some of its information to populate my View Model ProductStatsVM.

How do you populate the View Model? I heard three possible ways:

  1. Populate the View Model directly from the Controller (not good, I want to keep my Controller slim)
  2. By using a View Model constructor and pass the domain model as parameter. (I have to create a constructor for each domain model class I want to use)
  3. By using a Fill() method. (I saw it on the web, no idea how it works I guess this way the ViewModel should be aware of the Service Layer and creates coupling).

I know there are tools like AutoMapper, which I am going to use indeed, but before I want to understand the logic on how to fill a View Model from the Controller without using any additional tool.

CiccioMiami
  • 8,028
  • 32
  • 90
  • 151
  • looks like you have answered your own question there. – dice Feb 20 '12 at 16:34
  • 1
    Glad you asked this, im surprised no-one has +1 this question like they did back it the day. Ive been toying with various suggestions and proposed practices but Darins answer is right up my street, good question, great answer. – LenPopLilly Mar 03 '12 at 16:07

2 Answers2

27

The idea is that your controller action queries some repository to fetch a domain model. Then it passes this domain model to a mapping layer which is responsible to convert it to a view model and finally it passes the view model to the view:

public ActionResult Index(int id)
{
    ProductInfo product = repository.GetProductInfo(id);
    ProductViewModel viewModel = Mapper.Map<ProductInfo, ProductViewModel>(product);
    return View(viewModel);
}

and you could even make your controller slimmer by introducing a custom action filter that will automatically intercept the Model in the OnActionExecuted event and call into the mapping layer to substitute it with the corresponding view model so that your controller action now becomes:

[AutoMapTo(typeof(ProductViewModel))]
public ActionResult Index(int id)
{
    ProductInfo product = repository.GetProductInfo(id);
    return View(product);
}

and of course now the view is strongly typed to ProductViewModel:

@model ProductViewModel
...

Up to you to implement the Mapper.Map<TSource, TDest> method. And if you don't want to implement it yourself you could download AutoMapper which already has this method for you.

The mapping layer is something that is part of the MVC application. It must be aware of both the domain models coming from your service layer and the view models defined in your MVC application in order to be able to perform the mapping.

Don't use constructors (other than the default parameterless one) in your view models. The default model binder will choke if the view model doesn't have a parameterless constructor in your POST actions and you will have to implement custom model binders.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • By the way, where do you configure the mapping (i.e. Mapper.CreateMap()...)? – Abe Jun 14 '12 at 23:25
  • 1
    @Abe, this should be done only once for the lifetime of the AppDomain, ideally in a method called from Application_Start. – Darin Dimitrov Jun 15 '12 at 05:50
  • Thanks! I configured the mapping the global.asax.cs file in the OnApplicationStarted() method (Ninject) and it worked! – Abe Jun 16 '12 at 00:06
1

Since viewmodels are needed to populate UI, it should be good idea to get them populated via controllers. You still may keep them slim by using Automapper.

the_joric
  • 11,986
  • 6
  • 36
  • 57