Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding the use of SelectList in the business layer (MVC 3)

I am working on a fairly large MVC 3 application, and I'm running into an issue that doesn't smell quite right to me. This question takes a little set up to understand, so here are the premises that I'm currently operating on:

  • Views should be strongly typed (ViewBag/ViewData should be avoided)
  • Because the views are strongly typed, view model objects should be created that encapsulate all the data the view needs to display
  • When we have a need for drop down menus we should have two properties:
    1. A property in the model that stores the selected value of the drop down
    2. A SelectList property in the view model that represents the items in the drop down
  • The view itself always uses the @Html.DropDownListFor() helper method
  • We are using Entity Framework 4, and letting it generate entity classes from our already designed database
  • To avoid duplication and take advantage of LINQ, we are not creating our own separate business/model classes but adding to the partial classes generated by the entity framework
  • These partial classes that we write are located in the business layer to make everything compile correctly
  • Most model classes have a shared editor template that can be used in multiple views

Here's where the trouble comes in. The shared editor template's model type is set to the model class. This means that the partial view that makes up the editor template does not have access to the containing view model object where the list of drop down items is stored.

I was able to "solve" this by adding a SelectList property directly to the model class in the business layer instead of keeping it in the view model. But the SelectList class is specific to MVC, which in turn means that my business layer has a dependcy on MVC. That doesn't seem right to me because the BL should be agnostic to the UI.

Has anyone else run into this issue? How should I solve this? It's also possible that one of my premises are wrong.

like image 299
Shea Daniels Avatar asked Feb 18 '11 21:02

Shea Daniels


1 Answers

Everything seems very nice and good design up until this point (which is not surprising as it is that point that is causing you headaches :-)):

Most model classes have a shared editor template that can be used in multiple views

It's view models that should have editor templates and not EF models. And because view models are specific to the requirements of the view you are free to put whatever information you need into them, like in this case the SelectList. So don't simply define a root view model that has your EF models as properties (that's not a view model). Define a view model that is designed to meet the requirements of the particular view. Don't put a single EF class in your view model hierarchy and you will see how much simpler your life will be :-)

And don't worry if you have duplicate properties in your view models. That's what those classes are designed for. Also AutoMapper could greatly simplify the mapping between your models and view models.

like image 51
Darin Dimitrov Avatar answered Oct 06 '22 00:10

Darin Dimitrov