https://github.com/Dasync/EntityFrameworkCore.Extensions.Projections
Extension for EFCore that decouples the read model from entities themselves
https://github.com/Dasync/EntityFrameworkCore.Extensions.Projections
api c-sharp cqrs database ddd domain-driven-design dotnet-core ef-core entity-framework extension json orm projection projection-mapping read-model serialization view view-model
Last synced: 2 months ago
JSON representation
Extension for EFCore that decouples the read model from entities themselves
- Host: GitHub
- URL: https://github.com/Dasync/EntityFrameworkCore.Extensions.Projections
- Owner: Dasync
- License: mit
- Created: 2018-12-02T17:55:34.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-15T04:26:43.000Z (over 6 years ago)
- Last Synced: 2024-11-13T04:53:46.927Z (8 months ago)
- Topics: api, c-sharp, cqrs, database, ddd, domain-driven-design, dotnet-core, ef-core, entity-framework, extension, json, orm, projection, projection-mapping, read-model, serialization, view, view-model
- Language: C#
- Homepage:
- Size: 281 KB
- Stars: 12
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> _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 the [Domain Entity Projections in EFCore](https://medium.com/@sergiis/domain-entity-projections-in-efcore-2dbd6a9116ff) post.
## Example
This is a Domain Entity that contains behaviors and implements a projection interface (as a contract) defined below.
```csharp
public class City : ICityProjection
{
public string Name { get; private set; }public string State { get; private set; }
public long Population { get; private set; }
public int TimeZone { get; private set; }
public void SwitchToSummerTime()
{
TimeZone += 1;
}
}
```
A sample projection interface. An entity can have implement multiple projection interfaces.
```csharp
public interface ICityProjection
{
string Name { get; }string State { get; }
long Population { get; }
}
```Just use the `HasProjections` extension method on the desired entity(-ies). That will automatically find all interfaces with get-only properties.
```csharp
using Dasync.EntityFrameworkCore.Extensions.Projections;public class SampleDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity(e =>
{
// Declare that this entity has projection interfaces.
e.HasProjections();
});
}
}
```Time to query entities using their projection.
```csharp
var smallCities = await dbContext
.Set() // 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 original `City` 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.
```csharp
var json = JsonConvert.SerializeObject(smallCities);
```Deserialization back can be done without involving entities as well (visit the '[samples](samples)' folder to see how `EntityProjectionJsonConverter` is implemented).
```csharp
var cityProjections = JsonConvert.DeserializeObject>(
json, EntityProjectionJsonConverter.Instance);
```## How to start
See the '[samples](samples)' folder, but in a nutshell:
1. Add [Dasync.EntityFrameworkCore.Extensions.Projections NuGet Package](https://www.nuget.org/packages/Dasync.EntityFrameworkCore.Extensions.Projections) to your app
1. Define projection interfaces and implement them on your entities
1. Add `using Dasync.EntityFrameworkCore.Extensions.Projections;` to your code
1. Use `HasProjections` extension method on entities while building your DbContext model
1. Query with projection interfaces as shown above
1. Serialize projections at the API layer