简体   繁体   中英

MediatR CQRS command handler validation

I am using MediatR CQRS in my project.

Using DDD. So I have an application layer.

All the mediator command present in this layer.

Suppose I have command which is saving data to database. So it should be command. Now before saving I need to check some key exist or not against database also some price amount validation. If it exists I need to send back validation message to api.

Is this responsibility coming Inside the command, as it is a read operation?

This kind of validation handled at pipeline behaviour and calling repository directly when validation behaviour get invoked. Is it the right approach to be followed in CQRS?

This might be a bit better over on the Software Engineering StackExchange as it is a bit opinion based. However, personally, I put all of my command/query pre-validation logic directly in its own dedicated validator object that's picked up by a pipeline behavior object. I modeled my approach after Jason Taylor's Clean Architecture demo , and I find that performing all of the complex validation up front massively reduces the complexity of my commands and queries.

Many developers will argue that such functionality is a bit too complex for the standard property validators, but we've had great success with it. If you want to separate out your property validators (the validators ensuring strings are not empty or null, integers are within range, etc.) into a separate validator that lives alongside your data-integrity validators, that can be an option as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM