1

Let's assume we have an ASP.NET MVC web application with following tiers:

  • Business logic
  • Entities (business domain and database POCOs)
  • Common (resources, consts)
  • Data access (database EF queries, EDMX EF models and so on)
  • Web application (MVC web application)

We're using view models approach. Currently view models are placed in Entities layer. Data access queries returns view models (due to efficiency issues, so we avoid using mapper).

Web layer references all other layers. Data access references Common and Entities layers. Business logic references Entities and Common layers, in the future also Data access layer.

There's an idea to move view models to Web layer. Why? Because they're in fact bound with a particular technology (MVC) and UI implementation. But we're facing a problem here, because in this scenario Data access layer must reference Web and Web references Data access, so we have a circular dependency issue.

Moreover we have scenario when some validation of view model requires reference to Data access layer. We're going to keep validation method inside view models. Currently we want to implement it by injecting database context class (which is in Data access layer) to view model by constructor.

Do you have any idea how can we avoid it? Is it good idea to keep view models inside our Web layer?

  • 1
    What is the point of having business logic if your web app can just bypass it and go straight to data access? – JeffO Apr 3 '15 at 12:24
  • So Data access layer should only expose db context and Business layer should contain queries? I rather thought that Business layer will have some other business logic, not related to database access like for instance e-mail sending. Anyway, even if we move queries to Business logic and view models to web project we still have circular reference... – Landeeyo Apr 3 '15 at 12:39
1

I keep view models in the web project, for reason you stated, it's usually only useful to the relevant view.

I'm not sure why your data access layer would reference the web project though?

  • If view models are placed in Web project (Web layer) then Data access layer which is returning view models must reference Web. – Landeeyo Apr 3 '15 at 12:35
  • Ah, different terms for different things. I would call the representation of DB objects just Models. They have property for each column in DB. A view model takes Models and other required things (maybe lists for drop downs for instance), and gathers it all together - everything the view needs. A view usually doesn't match up exactly with 1 DB table. – Andy Wiesendanger Apr 3 '15 at 12:45
  • You're right, but we really return view models in Data access methods. It's done so to avoid overhead by mapping models to view models. – Landeeyo Apr 3 '15 at 12:55
  • 2
    That "overhead" is what allows a nicely separated layer. – Andy Wiesendanger Apr 3 '15 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.