0
votes

My domain model objects (generated by entity framework) have validation on it (using attributes and the IValidatableObject interface ) reading through the comments people seem to be against putting validation attributes in the domain model (buddy classes).

I am using specialized ViewModel classes with its own validation attributes to handle client side validation in the UI (ASP.NET MVC), I still need the validation attributes on the domain model as my business layer is exposed to other clients UIs, is this a good approach?

1
Seems like violation of DRY because you are creating validation twice on two different places just to satisfy needs of framework.Ladislav Mrnka
Thanks, What would be the best approach then? note that I need the validation on the domain model because I have other clients using the business layer.Socardo

1 Answers

0
votes

Yes and no. Its a good approach because your are doing validation on the server side. Your domain logic should not assume that the client validates (the client should of course validate). But besides from that, I would not validate a domain by attributes because validation rules are context-specific; they depend on the operation which you are performing. I put validation in the methods representing the different use-cases of the domain (the write operations) such as "create customer", "bill customer", "cancel order", etc. Don't assume that all your validation rules are invariants.

I don't see how you can reuse the server and client validation without exposing your domain entities outside of the boundaries of your service, which is a lot worse.