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.
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.
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.
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.
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.
You can have layers like this.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With