4

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?

  • 7
    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. – Robert Harvey Sep 12 '18 at 18:28
  • 2
    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. – Robert Harvey Sep 12 '18 at 18:29
  • @robertharvey Thought more about the 2nd question and I agree with your comment. I am going to remove that part. – CodingYoshi Sep 12 '18 at 18:31
  • @robertharvey Are there any best practices to minimize the duplication? Any examples? – CodingYoshi Sep 12 '18 at 18:45
  • No. I already stated that such duplication is unavoidable. – Robert Harvey Sep 12 '18 at 19:05
4

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.

  • 1
    Multiplatform Kotlin also gives you the possibility to write the validations only once. – Andy Sep 13 '18 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 Sep 13 '18 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.