ASP.NET MVC ViewModelBuilder Suggestions

Posted by Marco on Stack Overflow See other posts from Stack Overflow or by Marco
Published on 2010-05-18T21:37:52Z Indexed on 2010/05/18 22:20 UTC
Read the original article Hit count: 471

Filed under:
|
|
|
|

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(accountBuilder.Build());
    }

Or option two:

    [HttpGet, ImportModelStateFromTempData, Compress]
    public ActionResult MyAccount()
    {
        return View<IMyAccountViewModelBuilder>();
    }

Any thoughts or suggestions would be great!

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about mvc