ASP.NET MVC - Model in a Web Project

I am new to ASP.NET MVC and have inherited a project that uses this technology.

This web project contains three folders: Views

, Controllers

and Model

. As I understand it, the model actually contains your domain / business logic and is called by your controllers. The controllers themselves act as delegates between Views and Model.

Now, in a typical layered architecture, there should be no project references in a Web / UI project.

I find this rather confusing:
-> The user interface contains a model that - in an ideal world - is based on "Domain Driven Design" applications.
-> Layers over UI (Services and DataAccess) cannot have UI reference

How can you write efficient services and data access layers if they don't know your model?

What am I missing here? Is the Web.Model different from "DDD" and should I have a separate BL project? If so, what is the Web.Model that should contain?

+3


source to share


3 answers


I see the model as a concept. You can have a completely separate project containing your domain (your entities, your services, etc.) and a link to it in your "UI" project. In this case, it will be your "Model". This is what I usually do. In the My Models folder, I save the ViewModels which I use for Binding / Validation (for the UI). For example, if I have an Employee, but I don't want to use all of its properties (or, for that matter, different properties), I will create EmployeeViewModel

, adjust it the way I want, add an acknowledgment (if required), and I will pass it to my viewing.



This is by no means the "correct way" / "only way", but it has worked for me in the past and I thought I would share (also, I'm pretty terrible at explaining, so I really hope this post makes sense if it's not so, or updates are needed - let me know).

+10


source


You don't need your model to be in the same project. You can, of course, have those in different layers.

This is how I usually set up my projects

1) User interface project . This is an MVC web application type project where I will have my controllers as well as views and other UI related stuff.

2) Business entities . This will be a class library type project where I will define my domain objects (example: client). This is mostly similar to what my DB schema looks like. These are usually POCOs that represent my domain modal (I use this to generate the CodeFirst database).

3) Access to data . It will be a different class library type project that has data access classes. Usually my repository class / interfaces, my DBContext class and other data access classes will be in this project.

4) Tests - Unit tests for the project



enter image description here

The Business Objects project has been added as a reference to the data access project so that I can use these classes in my data access code.

Business object and data access projects are added as references to the user interface project. I would call the data accessors from my classes Controllers / Service.

You can also add a Service / Business Logic Layer between your controller and the Data Access Layer as needed.

I have several ViewModel classes also in my ViewModels folder of the UI project. I am using this for some screens where I have to display data from multiple domain entities. I have a mapping / service class that maps a domain object to view the model object. If your project is biff, you can save it as a supported project within the same solution.

+1


source


  • Views Contains your HTML layouts
  • Controllers do the heavy lifting of getting data from models or the models themselves and passing it to views.
  • Models are used to perform actions on BL or sample data.

Council. You can use EntityFramework (I recommend it because it's easy to get started with) to get your data and it's dead simple to customize, thus eliminating your DAL and saving you time from writing the whole thing.

Services: You can have controllers that return XML / JSON (different format?) By converting the data you received from the DB to XML / JSON and returning that instead of a view. Take a look at MVC 4 WebApi for more details. Note that you can pretty much do the same with mvc 3 too

Also see the asp.net/mvc site for tutorials to get you started, they are really helpful.

0


source







All Articles