Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC ViewModelBuilder Suggestions

For anything but trivial 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 ActionFilter 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!

like image 698
Marco Avatar asked May 18 '10 21:05

Marco


People also ask

What is the purpose of ViewModel in MVC?

ViewModel = Model that is created to serve the view. ASP.NET MVC view can't have more than one model so if we need to display properties from more than one model in the view, it is not possible. ViewModel serves this purpose. View Model is a model class that can hold only those properties that are required for a view.

How do you use a model in razor view?

Right-click in the Store Index action method and select Add View as before, select Genre as the Model class, and press the Add button. This tells the Razor view engine that it will be working with a model object that can hold several Genre objects.

Where do I put ViewModels?

Just create a new folder called ViewModels inside your project. So that along with the Views, Controllers and Models folders, you'll also have ViewModels. Like you already said, you talk to your DAL using your models and you talk to your views using your view models.


1 Answers

TheBuilder

I think you could move the responsibility of construct the correct view model to the builder. and you could pass the ViewModel type you want to build as a parameter, something like:

[HttpGet, ImportModelStateFromTempData, Compress]
public ActionResult MyAccount()
{
   return View( AccountBuilder.Build<MyAccountViewModel>( ) );
}

About candidates

FirstOptionThis

the above approach allows you to have more flexibility in the view you will render inside the action (what happens if your controller should choose between 3 views to show, and each one has a different view model? Filter solutions start to get complex.)

SecondOption

The View method responsibility is to take the model and render the view using it. it's the controller responsibility to build the model. so being a little orthodox, I would recommend to avoid putting model building logic in the View method : ).

like image 57
SDReyes Avatar answered Nov 01 '22 13:11

SDReyes