Where to put viewmodels inside the n-level app web project?

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

  • Business logic
  • Objects (business domain and POCOs databases)
  • Shared (resources, constants)
  • Data access (EF database queries, EF EDMX models, etc.)
  • Web Application (MVC Web Application)

We take a view model approach. The current view models are in the Entities layer. Data access requests return view models (due to performance issues, so we avoid using the carper).

The web tier refers to all other layers. Data access links. Common and Entities layer. Business logic refers to objects and shared layers, and in the future also to the data access layer.

There is an idea to move view models to the web tier. What for? Because they are actually related to a specific technology (MVC) and interface implementation. But we are facing a problem here because in this scenario the data access layer needs to refer to the Web and the data access Web references, so we have a circular dependency issue.

In addition, we have a scenario where a reference to the data access layer is required for some validation of the view model. We will keep the validation method in viewmodels. We currently want to implement it by introducing a database context class (which is at the data access layer) to view the model by constructor.

Do you have an idea how we can avoid this? Is it good to keep view models inside our web tier?

+1


source to share


1 answer


I'm not sure how you can use it ViewModel

in web applications. AFAIK this is harder than a desktop app because of its data binding.

However, your web layer links directly to the data layer (more precisely, to access directly). If it is not used to simplify the user interface and not for a business process, it also violates the purpose of the N-tier.



What needs to be done to keep the design clean is to make 2 different models. Model for entities (Domain / data / business model) and another for the view model . Your viewmodel is hosted on the web tier and when the domain model is retrieved it is mapped to the web tier. You cannot avoid being displayed here.

+3


source







All Articles