Тёмный

Using MediatR Pipeline Behaviors For Database Transactions 

Codewrinkles
Подписаться 28 тыс.
Просмотров 3,3 тыс.
50% 1

Опубликовано:

 

15 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 18   
@tapesteer
@tapesteer 2 месяца назад
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.
@arunbm123
@arunbm123 Год назад
❤ this s what I was looking since long time
@pilotboba
@pilotboba Год назад
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.
@alessandrohudson5221
@alessandrohudson5221 Год назад
You could debug the code to show how it works at runtime.
@pandita.express
@pandita.express 7 месяцев назад
is it possible to use with INotificationHandler as well?
@codingwithjunior
@codingwithjunior Год назад
You can try to validate this action when clients run the command process because on the queries process you don't need transactions.
@Codewrinkles
@Codewrinkles Год назад
Totally agree
@frankbanini8884
@frankbanini8884 Год назад
Do you have to register DbTransactionPipeline Handler on the Container?
@Codewrinkles
@Codewrinkles Год назад
Yes. I have covered behavior registraion 2 videos ago.
@lukash188
@lukash188 Год назад
Don't you end up with nested transactions if you call another queries/commands from your handlers?
@Codewrinkles
@Codewrinkles Год назад
That is sometthing you should NEVER do.
@lukash188
@lukash188 Год назад
@@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.
@Codewrinkles
@Codewrinkles Год назад
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
@igorilyichyov6414
@igorilyichyov6414 Год назад
Will this work if there are several repositories inside the handler?
@Codewrinkles
@Codewrinkles Год назад
Yes. I had this also working with repositories in some projects
@vasiliioleinic
@vasiliioleinic Год назад
To be fair, it is a good approach, but I would hesitate to use it if I had to deal with distributed transactions scenarios…
@spirits_
@spirits_ Год назад
Please stop
@Codewrinkles
@Codewrinkles Год назад
Stop what? And why?