Are your code classes first-class classes?

I'm not sure I can even formulate it correctly, but ...

I am starting to architect the solution using the first Entity Framework framework and starting to realize that I am polluting my domain classes (the EF classes will use to generate the database) with too much database-specific information: I have to make certain methods virtual so lazy loading can happen , I add attributes explicitly directed to the database configuration, to my properties, etc. I am also concerned about how common these classes are throughout the project.

Firstly, I make sense and / or do I fundamentally misunderstand how to use EF?

Secondly, if I understand this right, my question is: does someone else distinguish between their first-class classes used to generate their database and their domain classes (perhaps using an auto-mapper to populate one from the other)?

+6
source share
2 answers

For a long time I tried different approaches to this problem. In its simplicity with the Entity Framework, it is possible and very simple to use data classes as domain classes.

My experience is that in small projects you can get away with using your EF classes as domain classes. It’s quick and easy, but as projects become larger, this start becomes a problem, because you cannot control access in any way.

The most common scenario is to expose navigation properties on EF classes. Now your application will be able to move around the entire data set. Thus, with this model, you give up control over your data and domain objects.

There are several advantages to using your domain classes separately from EF. First of all, you will not be so attached to EF or code. With the level of separation / indirection, you can change your data structure if you want. Secondly, you can manage your data more efficiently.

Personally, I reached a pragmatic moment when I make this decision at the beginning of each project. If the project is small and contained, I could avoid this additional abstraction in favor of simplicity. In almost always medium and / or large projects, I have a separation.

+2
source

If you intend to use SOLID principles in your coding, you really should separate your data implementation (in your case EF code) from your domain / business logic. Matching them is a little overhead, but think about what happens when you need to access some data from web services?

In addition, your domain classes often contain calculated or derived values ​​(for example, full name, address) that will not be present in your database classes and potentially vice versa (for example, database registration information)

I would search for the repository template in the first instance.

+2
source

All Articles