Architect.EntityFramework.DbContextManagement 1.0.0

Manage your DbContexts the right way.

https://github.com/TheArchitectDev/Architect.EntityFramework.DbContextManagement

The persistence layer or infrastructure layer uses the DbContext (e.g. from a repository). Controlling its scope and transaction lifetime, however, is ideally the reponsibility of the orchestrating layer (e.g. from an application service). This package adds that ability to Entity Framework Core 5.0.0 and up.

// Register
public void ConfigureServices(IServiceCollection services)
{
 services.AddPooledDbContextFactory<MyDbContext>(context =>
   context.UseSqlServer(connectionString, sqlServer => sqlServer.EnableRetryOnFailure()));

 services.AddDbContextScope<MyDbContext>();
}

// Consume
public class MyRepository : IMyRepository
{
 // Accesses the DbContext instance currently provided by the orchestrating layer
 private MyDbContext DbContext => this.DbContextAccessor.CurrentDbContext;

 private IDbContextAccessor<MyDbContext> DbContextAccessor { get; }

 public OrderRepo(IDbContextAccessor<MyDbContext> dbContextAccessor)
 {
   this.DbContextAccessor = dbContextAccessor ?? throw new ArgumentNullException(nameof(dbContextAccessor));
 }

 public Task<Order> GetOrderById(long id)
 {
   return this.DbContext.Orders.SingleOrDefaultAsync(o.Id == id);
 }
}

// Orchestrate
public class MyApplicationService
{
 private IDbContextProvider<MyDbContext> DbContextProvider { get; }
 private IMyRepository MyRepository { get; }

 public MyApplicationService(IDbContextProvider<MyDbContext> dbContextProvider, IMyRepository myRepository)
 {
   this.DbContextProvider = dbContextProvider ?? throw new ArgumentNullException(nameof(dbContextProvider));      
   this.MyRepository = myRepository ?? throw new ArgumentNullException(nameof(myRepository));
 }

 public async Task PerformSomeUnitOfWork()
 {
   // Provide a DbContext and execute a block of code within its scope
   await this.DbContextProvider.ExecuteInDbContextScopeAsync(async executionScope =>
   {
     // Until the end of this block, IDbContextAccessor can access the scoped DbContext
     // It can do so from any number of invocations deep (not shown here)
     await this.MyRepository.AddOrder(new Order());

     // If we have made modifications, we should save them
     // We could save here or as part of the repository methods, depending on our preference
     await executionScope.DbContext.SaveChangesAsync();
   }); // If no exceptions occurred and this scope was not nested in another, the transaction is committed asynchronously here
 }
}

Install-Package Architect.EntityFramework.DbContextManagement -Version 1.0.0
dotnet add package Architect.EntityFramework.DbContextManagement --version 1.0.0
<PackageReference Include="Architect.EntityFramework.DbContextManagement" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Architect.EntityFramework.DbContextManagement --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Architect.EntityFramework.DbContextManagement, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Architect.EntityFramework.DbContextManagement as a Cake Addin
#addin nuget:?package=Architect.EntityFramework.DbContextManagement&version=1.0.0

// Install Architect.EntityFramework.DbContextManagement as a Cake Tool
#tool nuget:?package=Architect.EntityFramework.DbContextManagement&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 339 12/18/2020