- Notifications
You must be signed in to change notification settings - Fork0
Extension for EFCore that decouples the read model from entities themselves
License
Dasync/EntityFrameworkCore.Extensions.Projections
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A small weekend project with a higher future purpose
This extension to the EntityFramework Core allows to decouple the read model from entities themselves. If you practice Domain-Driven Design, then you want to separate Domain Entity/Aggregate repositories from their querying mechanism. Query results must return Entity Projections/Views instead of Entities themselves (as they contain behavior), but building projection types by hand is tedious.
The full description is published in theDomain Entity Projections in EFCore post.
This is a Domain Entity that contains behaviors and implements a projection interface (as a contract) defined below.
publicclassCity:ICityProjection{publicstringName{get;privateset;}publicstringState{get;privateset;}publiclongPopulation{get;privateset;}publicintTimeZone{get;privateset;}publicvoidSwitchToSummerTime(){TimeZone+=1;}}
A sample projection interface. An entity can have implement multiple projection interfaces.
publicinterfaceICityProjection{stringName{get;}stringState{get;}longPopulation{get;}}
Just use theHasProjections extension method on the desired entity(-ies). That will automatically find all interfaces with get-only properties.
usingDasync.EntityFrameworkCore.Extensions.Projections;publicclassSampleDbContext:DbContext{protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<City>(e=>{// Declare that this entity has projection interfaces.e.HasProjections();});}}
Time to query entities using their projection.
varsmallCities=awaitdbContext.Set<ICityProjection>()// Query directly on the projection interface.Where(c=>c.Population<1_000_000).ToListAsync();
Note that the result set does not contain instances of the originalCity entity type. Instead, this extension library generates types at runtime that implement given projection interfaces.
Then you can safely serialize your result set as it represents projections but not entities with behavior. Useful for API methods.
varjson=JsonConvert.SerializeObject(smallCities);
Deserialization back can be done without involving entities as well (visit the 'samples' folder to see howEntityProjectionJsonConverter is implemented).
varcityProjections=JsonConvert.DeserializeObject<List<ICityProjection>>(json,EntityProjectionJsonConverter.Instance);
See the 'samples' folder, but in a nutshell:
- AddDasync.EntityFrameworkCore.Extensions.Projections NuGet Package to your app
- Define projection interfaces and implement them on your entities
- Add
using Dasync.EntityFrameworkCore.Extensions.Projections;to your code - Use
HasProjectionsextension method on entities while building your DbContext model - Query with projection interfaces as shown above
- Serialize projections at the API layer
About
Extension for EFCore that decouples the read model from entities themselves
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
