For anything but trival view models, I use a view model builder that handles the responsibility of generating the view model object. Right now, I use constructor injection of the builders into my controllers but this smells a little since the builder is really dependent upon which action method is being executed. I have two ideas in mind. The first one would involve a custom ActioFilter allowing me to decorate each action method with the appropriate builder to use. The second would be to add an override of the View method that is open to accepting a generic.
This is what my code currently looks like. Note, the builder get injected via the ctor.
[HttpGet, ImportModelStateFromTempData, Compress]
public ActionResult MyAccount()
{
return View(accountBuilder.Build());
}
Here is what option one would look like:
[HttpGet, ImportModelStateFromTempData, Compress, ViewModelBuilder(typeof(IMyAccountViewModelBuilder)]
public ActionResult MyAccount()
{
return View();
}
Or option two:
[HttpGet, ImportModelStateFromTempData, Compress]
public ActionResult MyAccount()
{
return View<IMyAccountViewModelBuilder>();
}
Any thoughts or suggestions would be great!