0

I'm following your typical Domain Driven Design tier design. I have:

Web

  • Contains Views, and Controllers

ApplicationService

  • Facade to the application, i.e. service(s)

  • Contains view models

  • AutoMapper

Domain Model

  • BLL

Repository

  • Persistence logic

But since I'm using drop down lists in my Views, I'm confused how to incorporate the SelectListItem in this design. Note, drop down lists are populated with data from the database.

From what I've read, View Models should reside in the ApplicationService tier. Good theory, as the Web layer only "presentation", i.e. Views and Controllers and therefore can be replaced easily without re-designing the app.

BUT, this doesn't work when View Models are using SelectListItem (for drop down lists). SelectListItem requires System.Web.Mvc. And the ApplicationService layer I believe should not have a reference to System.Web.Mvc.

So, I'm stuck!

Either I give the ApplicationService layer a reference to System.Web.Mvc (ugly option).

Or I find a way how to populate drop down lists without SelectListItem.

Or I move the View Models back to the Web layer, which goes against the design.

And add another AutoMapper and related logic in the Web layer.

So, how are others implementing drop down lists logic in their multi-layered designs?

OpcodePete
  • 887
  • 1
  • 14
  • 28

2 Answers2

1

Application Services shouldn't deal with ViewModels. ViewModel is a Presentation layer concept (hence the name). It contains stuff tailored for display and you don't want it to leak into Application space.

Dependencies should always go from the outside to the inside, thus your ViewModel should depend on (be composed of / mapped from) whatever data structure is exposed by the application service, not the other way around.

See Should a service layer return view models for an MVC application?

Community
  • 1
  • 1
guillaume31
  • 13,738
  • 1
  • 32
  • 51
  • I've been reading up on DDD patterns, primarily Domain Model and Repository. There seems to be two camps on where View Models should reside, i.e. UI layer or the Service layer. I initially had view models in the UI layer, but now have moved them to the Service layer. Your link has helped alot. Answers go either way, but gives me food for thought! E.g. http://stackoverflow.com/a/11326600/3089510 , and this broad description, http://stackoverflow.com/a/3002194/3089510 . So I think either option is good design. Going with the Services layer, and will post findings here. – OpcodePete Jun 12 '14 at 04:51
  • 1
    I'd still say that *View*Model isn't a good name for something served by the application layer. Application Services have no clue how things should be presented in the UI, so call it anything you want but *View*Something. A true ViewModel may well contain things like your SelectListItems, UI-specific paginated collections, colors, etc. You absolutely **don't want that to be referenced** on the Application side. – guillaume31 Jun 12 '14 at 07:44
0

Created a class DropDownListItem, identical to SelectListItem, i.e. properties Selected, Text, Value.

View model uses DropDownListItem, where I previously used SelectListItem.

View converts DropDownListItems to SelectListItems for drop down lists to use.

OpcodePete
  • 887
  • 1
  • 14
  • 28