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?

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

1 Answer 1

5

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.

answered Sep 13, 2018 at 6:33
2
  • 1
    Multiplatform Kotlin also gives you the possibility to write the validations only once. Commented 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) Commented Sep 13, 2018 at 13:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.