Where to convert business model to view model?

Repositories should return domain models, not view models. As far as the mapping between the models and the view models is concerned, personally I use AutoMapper so I have a separate mapping layer but this layer is called from the controller.

Here’s how a typical GET controller action might look like:

public ActionResult Foo(int id)
{
    // the controller queries the repository to retrieve a domain model
    Bar domainModel = Repository.Get(id);

    // The controller converts the domain model to a view model
    // In this example I use AutoMapper, so the controller actually delegates
    // this mapping to AutoMapper but if you don't have a separate mapping layer
    // you could do the mapping here as well.
    BarViewModel viewModel = Mapper.Map<Bar, BarViewModel>(domainModel);

    // The controller passes a view model to the view
    return View(viewModel);
}

which of course could be shortened with a custom action filter to avoid the repetitive mapping logic:

[AutoMap(typeof(Bar), typeof(BarViewModel))]
public ActionResult Foo(int id)
{
    Bar domainModel = Repository.Get(id);
    return View(domainModel);
}

The AutoMap custom action filter subscribes to the OnActionExecuted event, intercepts the model passed to the view result, invokes the mapping layer (AutoMapper in my case) to convert it to a view model and substitutes it for the view. The view is of course strongly typed to the view model.

Leave a Comment