Franz.Common.EntityFramework
1.2.65
dotnet add package Franz.Common.EntityFramework --version 1.2.65
NuGet\Install-Package Franz.Common.EntityFramework -Version 1.2.65
<PackageReference Include="Franz.Common.EntityFramework" Version="1.2.65" />
paket add Franz.Common.EntityFramework --version 1.2.65
#r "nuget: Franz.Common.EntityFramework, 1.2.65"
// Install Franz.Common.EntityFramework as a Cake Addin #addin nuget:?package=Franz.Common.EntityFramework&version=1.2.65 // Install Franz.Common.EntityFramework as a Cake Tool #tool nuget:?package=Franz.Common.EntityFramework&version=1.2.65
Franz.Common.EntityFramework
A comprehensive library within the Franz Framework, designed to extend and simplify the integration of Entity Framework Core in .NET applications. This package provides additional features, abstractions, and utilities for managing relational and NoSQL databases, including support for Cosmos DB and MongoDB.
Features
- Database Configurations:
- Flexible configurations for Cosmos DB (
CosmosDBConfig
) and MongoDB (MongoDBConfig
). - Centralized management of database options (
DatabaseOptions
).
- Flexible configurations for Cosmos DB (
- Repositories (π Separation of Entity and Aggregate Repositories):
EntityRepository<TEntity>
: CRUD operations for standalone entities.AggregateRepository<TAggregateRoot>
: Manages aggregates using event sourcing.ReadRepository<T>
: Read-only data access.
- Multi-Database Context:
- Support for multiple database contexts (
DbContextMultiDatabase
).
- Support for multiple database contexts (
- Behaviors:
PersistenceBehavior
for managing transactional and persistence concerns.
- Conversions:
EnumerationConverter
: Converts enumerations to database-friendly formats.
- Extensions:
MediatorExtensions
: Extensions for MediatR.ModelBuilderExtensions
: Simplify model configuration.ServiceCollectionExtensions
: Streamlined dependency injection setup.
Version Information
- Current Version: 1.2.65
- Part of the private Franz Framework ecosystem.
π Separation of Entity and Aggregate Repositories
As of version 1.2.65, repositories have been split into:
EntityRepository<TEntity>
β Used for CRUD-based operations.AggregateRepository<TAggregateRoot>
β Used for event-sourced aggregates.
1οΈβ£ Entity Repository (CRUD Operations)
For simple standalone entities that donβt require event sourcing:
public class EntityRepository<TDbContext, TEntity> : IEntityRepository<TEntity>
where TDbContext : DbContext
where TEntity : class, IEntity
{
protected readonly TDbContext DbContext;
public EntityRepository(TDbContext dbContext)
{
DbContext = dbContext;
}
public async Task<TEntity> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
{
return await DbContext.Set<TEntity>().FindAsync(new object?[] { id }, cancellationToken);
}
public async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default)
{
await DbContext.Set<TEntity>().AddAsync(entity, cancellationToken);
await DbContext.SaveChangesAsync(cancellationToken);
}
public async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
{
DbContext.Set<TEntity>().Update(entity);
await DbContext.SaveChangesAsync(cancellationToken);
}
public async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
{
DbContext.Set<TEntity>().Remove(entity);
await DbContext.SaveChangesAsync(cancellationToken);
}
}
β Best for:
- Entities that donβt require consistency across multiple objects.
- Standard CRUD operations (Create, Read, Update, Delete).
2οΈβ£ Aggregate Repository (Event-Sourced Aggregates)
For managing aggregates using event sourcing:
public abstract class AggregateRepository<TDbContext, TAggregateRoot, TEvent>
where TDbContext : DbContext
where TAggregateRoot : EventSourcedAggregateRoot<TEvent>
where TEvent : BaseEvent
{
protected readonly TDbContext DbContext;
private readonly IEventStore _eventStore;
public AggregateRepository(TDbContext dbContext, IEventStore eventStore)
{
DbContext = dbContext;
_eventStore = eventStore;
}
public async Task<TAggregateRoot> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
{
var aggregate = await DbContext.Set<TAggregateRoot>().FindAsync(new object[] { id }, cancellationToken);
if (aggregate == null) return null;
var events = await _eventStore.GetEventsByAggregateIdAsync(id);
aggregate.ReplayEvents(events);
return aggregate;
}
public async Task SaveAsync(TAggregateRoot aggregate, CancellationToken cancellationToken = default)
{
var uncommittedChanges = aggregate.GetUncommittedChanges();
foreach (var @event in uncommittedChanges)
{
await _eventStore.SaveEventAsync(@event);
}
aggregate.MarkChangesAsCommitted();
await DbContext.SaveChangesAsync(cancellationToken);
}
}
β Best for:
- Aggregates that require transactional consistency.
- Systems using event sourcing.
- Complex business rules that enforce domain logic.
π 3οΈβ£ Choosing the Right Repository
Feature | EntityRepository | AggregateRepository |
---|---|---|
Use Case | Standalone entities | Event-sourced aggregates |
CRUD Support? | β Yes | β No (modifications via events) |
Supports Event Sourcing? | β No | β Yes |
Direct Entity Access? | β Yes | β No (changes happen via root) |
Usage Examples
1οΈβ£ CRUD-Based Repository (For Entities)
public class OrderRepository : EntityRepository<AppDbContext, Order>
{
public OrderRepository(AppDbContext dbContext) : base(dbContext) { }
}
2οΈβ£ Event-Sourced Repository (For Aggregates)
public class ProductRepository : AggregateRepository<AppDbContext, Product, ProductEvent>
{
public ProductRepository(AppDbContext dbContext, IEventStore eventStore)
: base(dbContext, eventStore) { }
}
Installation
From Private Azure Feed
dotnet nuget add source "https://your-private-feed-url" \
--name "AzurePrivateFeed" \
--username "YourAzureUsername" \
--password "YourAzurePassword" \
--store-password-in-clear-text
Install the package:
dotnet add package Franz.Common.EntityFramework --Version 1.2.65
Integration with Franz Framework
The Franz.Common.EntityFramework library integrates seamlessly with:
- Franz.Common.Business: Enables DDD and CQRS patterns.
- Franz.Common.DependencyInjection: Simplifies DI setup for repositories.
- Franz.Common.Errors: Provides standardized error handling.
Contributing
This package is part of a private framework. Contributions are limited to the internal development team.
- Clone the repository. @ GitHub - Franz.Common
- Create a feature branch.
- Submit a pull request for review.
License
This library is licensed under the MIT License.
Changelog
Version 1.2.65
- Introduced explicit separation between Entity and Aggregate Repositories.
- Implemented event-based aggregate repository structure.
- Upgraded to .NET 9.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net9.0
- Franz.Common.Business (>= 1.2.65)
- Franz.Common.DependencyInjection (>= 1.2.65)
- Franz.Common.Errors (>= 1.2.65)
- Franz.Common.MultiTenancy (>= 1.2.65)
- Franz.Common.Reflection (>= 1.2.65)
- Microsoft.EntityFrameworkCore (>= 9.0.2)
- Microsoft.EntityFrameworkCore.Cosmos (>= 9.0.2)
- Microsoft.EntityFrameworkCore.Relational (>= 9.0.2)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.2)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.2)
- MongoDB.Driver (>= 3.2.1)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on Franz.Common.EntityFramework:
Package | Downloads |
---|---|
Franz.Common.EntityFramework.MariaDB
Shared utility library for the Franz Framework. |
|
Franz.Common.EntityFramework.SQLServer
Shared utility library for the Franz Framework. |
|
Franz.Common.MongoDB
Shared utility library for the Franz Framework. |
|
Franz.Common.EntityFramework.PostGres
Shared utility library for the Franz Framework. |
|
Franz.Common.SSO
Shared utility library for the Franz Framework. |
GitHub repositories
This package is not used by any popular GitHub repositories.