Strategies for Customizing Entity Framework Data Classes
A topic that hasn't been addressed much really in documentation or talks is the customization of the generated data classes. According to Mike Pizzo we basically have the following four options each varying in terms of difficulty to use and/or implement in your own application scenarios.
Generated Partial Classes
- Add business logic, methods, non persisted properties to partial classes
DTO Pattern
- Materialize your own classes from entities
Codegen customization
- Add custom logic to property, type generation
- Attributes, Interfaces, Members, BaseClass,...
- Getters, Setters, BackingField, Return Type,...
Be sure to check out the custom EDM code generator on CodePlex which exposes generation events in which you can wire your own custom logic. Or check out the EDMX Code Generator sample to get the idea.
Custom classes
- Write your own classes that
- Derive from EntityObject, or,
- Implement interfaces: IEntityWithKey, IEntityWithChangeTracker,
IEntityWithRelationships
In the next version of the EF there will be a template based code generator to simplify implementing the above mentioned strategies. Wouldn't it be cool if the EF would ship with a set of templates based on the Domain Driven Design paradigm.