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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Franz.Common.EntityFramework" Version="1.2.65" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Franz.Common.EntityFramework --version 1.2.65                
#r "nuget: Franz.Common.EntityFramework, 1.2.65"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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).
  • 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).
  • 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.

  1. Clone the repository. @ GitHub - Franz.Common
  2. Create a feature branch.
  3. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.2.65 66 3/3/2025
1.2.64 125 1/29/2025
1.2.63 144 1/27/2025
1.2.62 131 1/8/2025