https://github.com/rebus-org/rebus.unitofwork
:bus: Unit of work helper for Rebus
https://github.com/rebus-org/rebus.unitofwork
rebus unitofwork
Last synced: 27 days ago
JSON representation
:bus: Unit of work helper for Rebus
- Host: GitHub
- URL: https://github.com/rebus-org/rebus.unitofwork
- Owner: rebus-org
- License: other
- Created: 2016-08-31T10:07:01.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-08-09T22:00:54.000Z (9 months ago)
- Last Synced: 2025-04-02T20:11:49.139Z (about 2 months ago)
- Topics: rebus, unitofwork
- Language: C#
- Homepage: https://mookid.dk/category/rebus
- Size: 2.93 MB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Rebus.UnitOfWork
[](https://www.nuget.org/packages/Rebus.UnitOfWork)
Provides a unit of work helper for [Rebus](https://github.com/rebus-org/Rebus).

---
The unit of work helper works with C# generics and lets you represent your unit of work as anything that makes sense to you.
You configure it like this:
```csharp
Configure.With(activator)
.Transport(t => t.Use(...))
.Options(o => o.EnableUnitOfWork(...))
.Start();
```for the synchronous version, or
```csharp
Configure.With(activator)
.Transport(t => t.Use(...))
.Options(o => o.EnableAsyncUnitOfWork(...))
.Start();
```if you want a unit of work that supports asynchronous creation, completion, etc.
An example could be an Entity Framework database context, `MyDbContext`, which you then manage like this:
```csharp
Configure.With(activator)
.Transport(t => t.Use(...))
.Options(o => o.EnableAsyncUnitOfWork(
create: async context => new MyDbContext(),
commit: async (context, uow) => await uow.SaveChangesAsync(),
dispose: async (context, uow) => uow.Dispose()
))
.Start();
```By the power of C# generics, `uow` passed to the `commit` and `dispose` functions above will have the same type as
the one returned from the `create` method.`context` will be the current `IMessageContext`, which is also statically accessible via `MessageContext.Current`,
this way enabling injection of your unit of work by using the message context to share it:
```csharp
Configure.With(activator)
.Transport(t => t.Use(...))
.Options(o => o.EnableAsyncUnitOfWork(
create: async context =>
{
var uow = new MyDbContext();
context.TransactionContext.Items["current-uow"] = uow;
return uow;
},
commit: async (context, uow) => await uow.SaveChangesAsync(),
dispose: async (context, uow) => uow.Dispose()
))
.Start();
```
and then you can configure your IoC container to be able to inject `MyDbContext` - e.g. with Microsoft Extensions Dependency Injection like this:```csharp
services.AddScoped(p =>
{
var context = p.GetService()
?? throw new InvalidOperationException("Cannot resolve db context outside of Rebus handler, sorry");return context.TransactionContext.Items.TryGetValue("current-uow", out var result)
? (MyDbContext)result
: throw new ArgumentException("Didn't find db context under 'current-uow' key in current context");});
```