Architect.EntityFramework.DbContextManagement
1.0.1
See the version list below for details.
dotnet add package Architect.EntityFramework.DbContextManagement --version 1.0.1
NuGet\Install-Package Architect.EntityFramework.DbContextManagement -Version 1.0.1
<PackageReference Include="Architect.EntityFramework.DbContextManagement" Version="1.0.1" />
paket add Architect.EntityFramework.DbContextManagement --version 1.0.1
#r "nuget: Architect.EntityFramework.DbContextManagement, 1.0.1"
// Install Architect.EntityFramework.DbContextManagement as a Cake Addin #addin nuget:?package=Architect.EntityFramework.DbContextManagement&version=1.0.1 // Install Architect.EntityFramework.DbContextManagement as a Cake Tool #tool nuget:?package=Architect.EntityFramework.DbContextManagement&version=1.0.1
Release notes:
1.0.1:
- Now using AmbientContexts 1.1.1, which fixes extremely rare bugs and improves performance.
Description:
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
}
}
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 | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Architect.AmbientContexts (>= 1.1.1)
- Microsoft.EntityFrameworkCore (>= 5.0.0)
- Microsoft.EntityFrameworkCore.Relational (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.