You didn't provide a full explanation here. How do you register the pipeline behavior so that it is used by mediatr for example? If you use blazor and you have a dbContextFactory instead which is Scoped, will this pipeline still work considering that here you set the context instance to be transactional, but each handler in blazor will have it's own context then so doing it here is redundant unless you somehow pass the same context down the pipelines.
I'm not sure if this was contrived to show an example and not how you would really code such a handler, but this is not how I would code that handler, especially if using EF. The EF model should have a navigation property on the payment or the authorization or both so you can create the PaymentAuthorization and relate it to the payment, from one side or the other, up to you. Another option is to use client generated IDs. ULID or GUID so you can assign the FK before making the round trip to the server. This way you have a single unit of work that is saved atomically in a single transaction by default in EF core. Even better, give your handler one responsibility, to create the Payment and raise a PaymentCreatedEvent domain event, then process your events in your savechanges() override. This will also make save changes a single atomic action with its inherent transaction. (once again client generated ids or navigation property needed to do it this way) I think another issue you will have is you will have to add some type of marker interface for your requests because you don't want to use the transaction handler in every mediatr request because not every handler will need a transaction. This is cognitive load for your dev team, to remember to add this marker interface, or use an IDatabaseTransactionRequest instead of IRequest, whatever. Bottom line, magic database, transaction handler, and also event magic SaveChagnes (which I've also seen) are yuck in my opinion.
@@Codewrinkles I agree that we probably should not use commands within the handlers, but without using another queries within handlers we might end up with quite a big code dupplications.
It's not duplication each handler conceptually represents a unique action and therefore a dofferen reason for change. We end up having the same bugs as in regular serices. I have a video called "DRY is bad". Take a look. It might clrify may point here