DAL, Model Layer, EF code-first and business logic, how do they fit together?

Bit by bit, I am unlikely and becoming more experienced with ASP.NET MVC4. However, I still cannot understand what concerns this very important point. Say I have a model:

public class WorkPaper { public int WorkPaperID { get; set; } public string name { get; set; } public string description {get ; set; } public bool verified {get; set;} public DateTime dateAdded {get; set;} } 

I use this model with the first Entity Framework code approach, and I have the following DB context:

 public class NicodemeContext : DbContext { public DbSet<WorkPaper> Workpapers { get; set; } } 

I don’t understand what the model layer is and what is the level of data access. For me, the WorkPaper class tends to be part of the DAL since I designed it and selected property names and types (navigation property, etc.) to match the EF pattern. But the problem is that if I'm right, I really don’t see where I have to put the business logic.

In this particular case, if I want to add a rule saying that the Working Paper cannot be sent if it has not been verified, and another rule saying that if it was added more than 2 weeks ago, it cannot be sent (strange rules, but this is just an example). Where should I add them? Should I create another class, but where should I place it and what should it contain? Wouldn't that be very redundant with the class that I already have? But on the other hand, since this class is "database oriented", would it be useless to add business rules there?

My point is to understand where I have to put my business logic and understand the difference between DAL and the model. It is difficult for me to identify the DAL and the model layer, as their appearance is very similar to me. I would like to do everything right.

(Basically, I don’t know where to encode “my program” with MVC! I’m not comfortable as soon as I want to encode functionality that I'm really interested in. It seems to me that I'm not doing everything right)

+8
c # entity-framework asp.net-mvc-4 data-access-layer
source share
2 answers

There is no single “right” way to do this. There are probably a number of “wrong” ways, but, like most others, “it depends.”

You will find many opinions on this subject. And a lot of this depends on how you approach him. For example, @Tarzan suggests using your model for both business logic and data-level objects. Others suggest creating separate Entity objects, business objects, and presentation model objects.

If you are making a relatively simple application like CRUD, then you can probably do what @Tarzan offers, and it has few problems. However, in more complex applications, you begin to run into problems doing it this way. For example, what happens when your business logic is different from your data logic? If you combine them into one set of entities, then you are forced to make the logic the same everywhere or spend a lot of time on modernization.

The most flexible approach is to completely isolate the layers of presentation, business, and data. Thus, the requirements (for example) of the user interface should not correspond to other layers. (Here's a simple example, imagine that for some kinds of objects, a particular field is allowed to be null, but not for others. The data layer will only know that the field is NULL, while your business layer will know that certain objects can 't null).

However, this approach can have a lot of overhead and requires what seems like duplicate code at every level ... creating a lot of extra effort, which is often not worth it for small or simple applications.

Keep in mind that MVC is a presentation template. This means that it ONLY refers to the user interface. A “model” is usually considered a “presentation model,” which is the model that the view uses. This model is customized for presentation requirements. For example, using data attributes that you would not put on a data model.

If you think that MVC is strictly presentation, MVC does not care about what data access you use, and does not care about which business layer you use. This can be a service level, a repository, or a library of business objects (for example, CSLA).

A common pattern in MVC applications is to use a service level of some type or repository if you just perform CRUD operations. Typically, there is some sort of mapping system between each layer, using technologies such as AutoMapper or custom assembly mapping classes.

The point here is simple. MVC does not apply to business and data, so do not think about how they fit into an MVC application. They do not, or at least they do not differ from the elementary interface itself.

Your application is a collection of objects in which you can look at architecture. This architecture consists of different layers. MVC, Service, Data, etc. MVC may be the main user interface, but that does not mean that everything else is also MVC.

+7
source share

You can put the NicodemeContext class in the DAL and WorkPaper classes at the model level.

  • In the "Model Level" you define your business domain. That's where your business logic goes.
  • The data access level is the place where you read and write to the database and fill objects from your model level.

A useful method is to use partial classes in the Model. In one partial class, save everything that can be written by a code generator, and everything written by hand in another partial class. For example, if you use the tool to create a partial WorkPaper class, it will usually contain all the properties and associations. In a handwritten partial class, you can place your business logic. If something changes with your domain, it allows you to run the class generator again and again, without fear of losing your business logic. I know that you said that you are using the first code, but partial classes can still be useful.

In models, you host business logic and exchange data with a data access layer that stores and retrieves data. Your views are intended to represent the user interface. Controllers are designed to transfer information between types and models. It takes some time to get an idea of ​​MVC, but you are asking the right questions and are on the right track.

Hope this helps. Greetings.

+2
source share

All Articles