Dasync.EntityFrameworkCore.Extensions.Projections
1.0.0
dotnet add package Dasync.EntityFrameworkCore.Extensions.Projections --version 1.0.0
NuGet\Install-Package Dasync.EntityFrameworkCore.Extensions.Projections -Version 1.0.0
<PackageReference Include="Dasync.EntityFrameworkCore.Extensions.Projections" Version="1.0.0" />
paket add Dasync.EntityFrameworkCore.Extensions.Projections --version 1.0.0
#r "nuget: Dasync.EntityFrameworkCore.Extensions.Projections, 1.0.0"
// Install Dasync.EntityFrameworkCore.Extensions.Projections as a Cake Addin #addin nuget:?package=Dasync.EntityFrameworkCore.Extensions.Projections&version=1.0.0 // Install Dasync.EntityFrameworkCore.Extensions.Projections as a Cake Tool #tool nuget:?package=Dasync.EntityFrameworkCore.Extensions.Projections&version=1.0.0
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.
Example
This is a Domain Entity that contains behaviors and implements a projection interface (as a contract) defined below.
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.
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.
using Dasync.EntityFrameworkCore.Extensions.Projections;
public class SampleDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<City>(e =>
{
// Declare that this entity has projection interfaces.
e.HasProjections();
});
}
}
Time to query entities using their projection.
var smallCities = await dbContext
.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 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.
var json = JsonConvert.SerializeObject(smallCities);
Deserialization back can be done without involving entities as well (visit the 'samples' folder to see how EntityProjectionJsonConverter
is implemented).
var cityProjections = JsonConvert.DeserializeObject<List<ICityProjection>>(
json, EntityProjectionJsonConverter.Instance);
How to start
See the 'samples' folder, but in a nutshell:
- Add this NuGet Package to your app
- Define projection interfaces and implement them on your entities
- Add
using Dasync.EntityFrameworkCore.Extensions.Projections;
to your code - Use
HasProjections
extension method on entities while building your DbContext model - Query with projection interfaces as shown above
- Serialize projections at the API layer
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.EntityFrameworkCore (>= 2.1.4)
- Microsoft.EntityFrameworkCore.Relational (>= 2.1.4)
- System.Reflection.Emit (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 1,406 | 12/3/2018 |