Unit of Work question

Hi all,

I was looking through your documentation for unit of work and I came across this:

Can you folks elaborate on a scenario where I would want to use this?

We are using your approach outlined here:

Entity Framework Core integration with SQL Persistence • Sql Persistence Samples • Particular Docs?

to enroll our EF Context in a transaction and support transactionality and unit of work for database operations. My understanding is that all outgoing messages inside a single handler are automatically enrolled in the same transaction and, while I am now questioning myself, I’m 99% sure I have seen and tested that exact situation…

So I’m not clear on what problem the code below is meant to solve?

var unitOfWork = endpointConfiguration.UnitOfWork();

I have also seen and tested that multiple event handlers in the same service would all be enrolled in a single transaction.

Can you assit us with a more detailed explanation of the use case for this?

Hi @schilm, thanks for posting.

The WrapHandlersInATransactionScope API wraps the entire message pipeline in a TransactionScope allowing other APIs that support it to enlist and become part of the unit of work.

For transports that natively support TransactionScope, such as SqlServer, the WrapHandlersInATransactionScope API is redundant and will throw an error when it detects an open TransactionScope from the transport. Furthermore, the API is not required if the unit of work is being implemented without the use of a TransactionScope, such as with connection/transaction sharing with EF.

The API is used when a TransactionScope is desired but with a transport that does not support it, such as the RabbitMQ transport.