Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach to share Entity Framework model between application layers

I want to create an asp.net application which has several layers (classic layer design): A business layer and a presentation layer. Data layer seems obsolete as EF does all the work.

So when I create the EF model in Business Layer, I cannot use the entities in presentation layer because I can't add data annotations for display and validation and so on (especially display attribute is typically a part of presentation layer). And it seems not very good to me to create copy all the data to similar "viewmodel"-classes in presentation layer.

So is there a good approach to create object context in business layer and having a shared "contracts"-assembly for the entities? Most samples that I have found place everything in one single assembly, which is not the best approach for more complex applications in my opinion.

like image 209
Martin H. Avatar asked Oct 01 '13 06:10

Martin H.


People also ask

Which is best approach in Entity Framework?

As in this diagram, if we already have domain classes, the Code First approach is best suited for our application. The same as if we have a database, Database First is a good option. If we don't have model classes and a database and require a visual entity designer tool then Model First is best suited.

Is Entity Framework core fast?

EF Core 6.0 performance is now 70% faster on the industry-standard TechEmpower Fortunes benchmark, compared to 5.0. This is the full-stack perf improvement, including improvements in the benchmark code, the . NET runtime, etc. EF Core 6.0 itself is 31% faster executing queries.

What is model first approach in Entity Framework?

Model First allows you to create a new model using the Entity Framework Designer and then generate a database schema from the model. The model is stored in an EDMX file (. edmx extension) and can be viewed and edited in the Entity Framework Designer.


2 Answers

You should abstract EF away from your business layer, never use such frameworks directly. I usually create a generic repository interface and a generic EF repository (this is your data layer) which implements the interface. An IoC framework will take care of the injection of the right implementation at runtime.

Also, you are exactly describing the need of ViewModels in your presentation layer. They take care of showing only the information you need on the view along with the validation based on the data annotations. In the beginning they might look like duplicates of your domain entities, but in the end it will save you a lot of trouble, it's definitively the way to go.

Your business layer should result domain entities, in your controller you map them to your ViewModels. You could use AutoMapper for this.

You can check out this question and answer for more information about service/business layers and domain/view models.

like image 189
Henk Mollema Avatar answered Oct 25 '22 10:10

Henk Mollema


You can have layers like this.

  1. EF model layer project
  2. Business layer, which will interact with EF model
  3. Presentation layer, which interact with business layer, and you can give reference to EF model too, if needed

You can create your partial classes in EF model layer - for data annotation and all. So it can in turn - will be used in business layer also.

like image 28
Paritosh Avatar answered Oct 25 '22 10:10

Paritosh