DDD entities and Entity Framework Core
DDD requires entities to be defined in a way that is different from the way we defined entities in Chapter 8, Interacting with Data in C# – Entity Framework Core. In fact, Entity Framework entities are record-like lists of public properties with almost no methods, while DDD entities should have methods that encode domain logic, more sophisticated validation logic, and read-only properties. While further validation logic and methods can be added without breaking Entity Framework's operations, adding read-only properties that must not be mapped to database properties can create problems that must be handled adequately. Preventing properties from being mapped to the database is quite easy—all we need to do is decorate them with the NotMapped
attribute.
The issues that read-only properties have are a little bit more complex and can be solved in three fundamental ways:
- Map EF entities to different classes. Define...