Imagine we have a simple rule:
A member must be at least 18 years old to join.
Imagine we have a C# class for Member
and it is our domain model. The rule is enforced in this class.
Now we create a web app with a view to create a new member. This view has a datepicker for entering a date for date of birth. It makes no sense to allow the user to select a date which is not valid i.e. last year. To enforce this, we need to write JavaScript. However, now the business rule has been duplicated.
This is a simple example to illustrate the question but in a real application, there will be many such duplications.
Is there any way to avoid this?
1 Answer 1
No, duplication of input validation is unavoidable in an application that is split into a front-end and a back-end and where the front- and back-end communicate over an untrusted communication channel like a network.
Not doing the validation twice means that either you give the user a very bad UX by giving very late feedback on validation errors, or you open up the back-end to receive invalid data from actors that don't use the official front-end (and those actors will exist).
The only mitigation against having to write the validation logic multiple times is to write it in a language that is used by both the front- and back-end. For web-applications you are then effectively restricted to using JavaScript or languages that can be compiled into JavaScript for both ends.
-
1Multiplatform Kotlin also gives you the possibility to write the validations only once.Andy– Andy2018年09月13日 12:57:13 +00:00Commented Sep 13, 2018 at 12:57
-
Depending on the scale of your application, you could also build tooling to translate your validation logic from your server language to your client language - for simple rules at least. IIRC ASP.Net webforms included simple validation rules for both back- and frontend (C#/CIL and Javascript respectively)cwap– cwap2018年09月13日 13:50:31 +00:00Commented Sep 13, 2018 at 13:50
Explore related questions
See similar questions with these tags.
Is there any way to avoid this?
-- No. Validation is performed on the client for convenience reasons. Validation is performed on the server for data integrity reasons.If there is no way to avoid this, then is DDD even useful for an application which only has a web interface?
-- That seems like a non-sequitur. Uselessness of DDD doesn't follow from code duplication.