Dapper Outbox
Usage
The Dapper Outbox allows integration between Dapper and Brighter's outbox support. The configuration is described in Basic Configuration.
For this we will need the Outbox package for Dapper. Packages for Dapper exist for the following RDBMS: MSSQL, MYSQL, and Sqlite. Packages have the naming convention:
Paramore.Brighter.{DB}.Dapper
In addition, you will need the Outbox package for the relevant RDBMS:
Paramore.Brighter.Outbox.{DB}
Obviously, {DB} should match. In the example below we use MySql, so we would need the following packages:
Paramore.Brighter.MySql.Dapper
Paramore.Brighter.Outbox.MySql
Paramore.Brighter.MySql.Dapper will pull in another two packages:
Paramore.Brighter.MySql
Paramore.Brighter.Dapper
As described in Basic Configuration, we configure Brighter to use an outbox with the Use{DB}Outbox method call.
As we want to use Dapper, we also call: Use{DB}TransactionConnectionProvider so that we can share your transaction scope when persisting messages to the outbox.
In our handler we take a dependency on Brighter's Dapper Unit of Work. We explicitly start a transaction within the handler on the Database within the Unit of Work. Dapper provides extension methods on a DbConnection for typical CRUD operations. Our Unit of Work wraps that DbConnection, and allows you to create a DB transaction associated with that DbConnection. You must use our method, and not create the transaction directly via the connection, because we cannot obtain that transaction. Sharing that transaction allows us to insert a message into the Outbox within the same transaction.
We call DepositPostAsync within that transaction to write the message to the Outbox. Once the transaction has closed we can call ClearOutboxAsync to immediately clear, or we can rely on the Outbox Sweeper, if we have configured one to clear for us. (There are equivalent synchronous versions of these APIs).x
Brighter Unit of Work without Dapper
Because the Brighter Unit of Work just wraps a DbConnection and is's associated transaction, it can be used to provide a DbTransaction that works with the outbox whenever you want to use DbConnection to interface with a database. Whilst Dapper adds value on top of DbConnection, it just a set of extension methods, and our unit of work does not depend upon Dapper itself.
Last updated