Skip to content
This repository was archived by the owner on May 5, 2026. It is now read-only.

Repository files navigation

UnitOfWork

Scoped unit-of-work implementation that supports Entity Framework Core and NHibernate out of the box.

Usage with NHibernate

Add nuget package

dotnet add package FubarDev.UnitOfWork.NhSession

Configure services

The yourSessionFactory must already contain an instance of ISessionFactory.

varservices=newServiceCollection();services.AddSingleton(yourSessionFactory).AddUnitOfWork<ISession>().Use<NhRepositoryManager>();varserviceProvider=services.BuildServiceProvider();

Create a unit of work

classYourService{privatereadonlyIUnitOfWorkFactory<ISession>_factory;publicYourService(IUnitOfWorkFactory<ISession>factory){_factory=factory;}publicasyncTaskYourTransactionalServiceOperationAsync(CancellationTokencancellationToken){// Create a new unit of work (with transaction)awaitusingvarunitOfWork=await_factory.CreateTransactionalAsync(cancellationToken);// Access the NHibernate session and do something with itvarsession=unitOfWork.Repository;// Commit all changesawaitunitOfWork.CommitAsync(cancellationToken);}}

Usage with Entity Framework Core

Add nuget package

dotnet add package FubarDev.UnitOfWork.EfCore

Configure services

The yourSessionFactory must already contain an instance of ISessionFactory.

varservices=newServiceCollection();services.AddDbContextFactory<YourDbContext>(dcob =>{// Configure EF Core optionsdcob.UseSqlite(_connection);}).AddUnitOfWork<YourDbContext>().Use<EfCoreRepositoryManager<YourDbContext>>();varserviceProvider=services.BuildServiceProvider();

Create a unit of work

classYourService{privatereadonlyIUnitOfWorkFactory<YourDbContext>_factory;publicYourService(IUnitOfWorkFactory<YourDbContext>factory){_factory=factory;}publicasyncTaskYourTransactionalServiceOperationAsync(CancellationTokencancellationToken){// Create a new unit of work (with transaction)awaitusingvarunitOfWork=await_factory.CreateTransactionalAsync(cancellationToken);// Access the DB context and do something with itvardbContext=unitOfWork.Repository;// Commit all changesawaitunitOfWork.CommitAsync(cancellationToken);}}

About

Scoped unit-of-work implementation

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages