amantinband / clean-architecture

The ultimate clean architecture template for .NET applications 💪
MIT License
1.4k stars 221 forks source link

Application Layer model validation #19

Closed Risthart closed 6 months ago

Risthart commented 6 months ago

Hey, Amichai.

This is not an issue, but a question.

For some period of time we've been developing our applications following pretty much the same design. We really like it so far, it answers most of our questions we'd had with the other approaches. But there's one thing bothering me, it's validating commands and queries on the Application Layer side.

On the first glance there's nothing wrong with it, we're making sure that nothing gets processed in the application core without any input validation. Good stuff. But let's think how we handle the validation errors. Usually we throw/return them to the Presentation layer and then further to the client. And here's where the problem lies.

You see, between the Presentation and Application Layers we usually need to map this two worlds, one to another. REST world and CQRS world. And in some cases they do not match one-to-one. That leads us to some frustration on the client side. Let's say, sending an invalid property with the name "A" returns a validation error with the property "B" (if you need a real-world example, I can try to come up with something).

Of course, if a human reads the message, in 9 of 10 cases it should be clear what exactly went wrong. But what if we want to bind some kind of a handler on the front-end side? Make a fancy highlighting for the invalid textbox and whatnot.

So the questions are: In this case, should the client know the Application Layer contracts structure? Or should we copy-paste the whole validation to the Presentation Layer (kinda error prone and ugly, to be honest)? Also moving the whole validation to the Presentation Layer is an option too. Less ugly than the second option, but no validation in the Application feels wrong too.

Please let me know what you think. Thank you for your time and the contribution to the community.

amantinband commented 6 months ago

Hey @Risthart! You can definitely move the model validation logic to the presentation layer, and perform the model validation on the incoming requests, rather than the commands/queries.

Regarding duplications, I think it depends more on the project and team. If the project isn't too big and the team is well aligned on the architecture, I think one check early up stream is enough. Otherwise, I would duplicate important checks failures later downstream would be treated as unexpected rather than bad request.

Btw the same debate regarding duplication is common when it comes to duplicating presentation/application validations in the domain layer. I take the exact same approach above in this case as well.

Let me know if this makes sense or if you have further questions 🙂

Risthart commented 6 months ago

Hey @amantinband.

Totally makes sense. And yes, we already validating most of the stuff in the domain layer too. It feels pretty natural, when it comes to DDD. Most of the input gets validated inside ValueObjects (like User Name, Date Of Birth etc.).

Thank you for your time. I guess, we can close the issue then.