Confusion about where to put business logic when using Entity framework

c# entity-framework


I've only recently begun using the Entity Framework, so I'm not sure how the classes that are typically in the business layer combine with the entities that the Entity Framework creates.

When using classic ADO.NET, for instance, I would have a class called Customer and a class called DALCustomer to handle database interaction. In this organizational structure, I would have put the code to perform calculations, filtering, and to create an instance of the DAL inside Customer for saving, updating, and deleting in the Customer class.

This is where my uncertainty with the Entity Framework starts; if you have a table called Customer, the Entity Framework creates an entity called Customer. Does this entity eliminate the requirement for a Customer in the business layer? In essence, the object created by the Entity Framework has all the attributes and methods that belong in the business layer. Or is it still necessary for a class named CustomerBL to be declared in the business layer that has the fields and methods required to carry out the business logic needed for calculations and filtering and still requires an instance of the EF DAL to manage data access?

One more question comes to mind if a business class is necessary, in this example CustomerBL: should the fields defined in the customer entity be reproduced in CustomerBL or should an instance of the customer entity be declared in CustomerBL so that the fields wouldn't need to be stated in two places?

1/15/2013 8:11:36 PM

Popular Answer

With the separation of the data model and conceptual model in mind, the entity framework was created. It supports the following extensions: inheritance, separating entities (not EF core), table division, advanced kinds or owned kinds, and clear many-to-many relationships (not EF core), all of which allow one to customize the domain model to their needs without being overly confined by the data store model. To cover up cross-cutting issues, EF core supports shadow attributes. according to the disclosed class model.

Working with POCOs in which only a few properties are mapped to data store columns and others satisfy domain objectives is possible using the code-first method. Model-first and database-first approaches generate partial classes that can be extended.

Of fact, there is a limit to how well the conceptual model and store model can be separated. Some factors work against stubborn ignorance's objective. For example -

  • Declaring navigation properties as lazy is important if lazy loading is desired.virtual , allowing EF to replace them in proxy types. DDD (Domain-Driven Design) would promote utilizingvirtual when polymorphism is necessary, only.

  • It is practical to have simple foreign key characteristics (like,ParentId ) that are present with the "actual" relationships (aParent reference). Purists view this as going against the DDD tenets.

  • The primary purpose of the EF class model, which is a component of a data access layer, should be to do this. As a result, it will have a lot of reciprocal interactions so that LINQ queries may take full advantage of navigation features. These connections between parties go against DDD principles once more.

  • There are LINQ-to-entities and LINQ-to-objects differences in plenty. You just cannot ignore the reality that when you LINQ against things in memory, you are doing so against a completely separate universe. The code for this is either leaky abstraction or tight coupling.

  • EF cannot map interfaces; it can only map actual classes.

Then again, leveraging created EF classes or POCOs from a code-first model as domain classes generally makes me happy. I've yet to witness a seamless changeover from one data store or ORM to another, if it ever does. Constant ignorance is a myth. Irregularities from the DAL can easily be detected in the domain. It only makes sense to minimize or abstract away this footprint when you have to write code for various data stores and models or when those stores and models are anticipated to change often.

The fact that many modern applications have many tiers and send various view models or DTOs to clients in serial form may further help to promote EF classes as domain classes. Using domain classes in user interfaces rarely meets the criteria. Use the EF class model as the domain instead, and have services return specific models and DTOs when a UI or service consumer requests them. If solely for performance reasons, adding another abstraction layer can be more of a burden than a benefit.

12/4/2019 10:54:44 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow