Answer the question
In order to leave comments, you need to log in
How to properly validate entity attributes?
Faced a contradiction in the implementation of entities in the framework of DDD. On the one hand, we must ensure that it is not possible to get an entity in an invalid state, and on the other hand, show in the user interface which values are incorrect and why. What if the error is in several entity attributes? I don't want to duplicate the validation on the client and server. In the examples that I found, an exception is thrown immediately when trying to set an incorrect attribute value. What if we pass an object to the constructor in which errors will be added, and if errors occur in the object's constructor, they are added there and an exception is thrown at the very end? Maybe I'm inventing a bicycle and there is already an established solution?
Answer the question
In order to leave comments, you need to log in
Why can't you validate the data before passing it to the entity?
If you look at the same Symfony framework, the validation component is not tied to other parts of the system at all.
Hey!
In fact, from the point of view of DDD, there are just no contradictions. The domain model exists on its own, and makes no assumptions about its clients, which can be graphical interfaces for desktop and web applications, and not only...
This cannot be because the business entity protects its invariants and must always be in a valid state, so if you try to set at least one attribute with an invalid value, an exception will be thrown immediately.
You can duplicate validation if it is well-known, for example, full name or email address, why transfer these values to the server when you can perform full validation in the browser, which will increase the UI response time. And if the logic is specific, which happens more often, then the business rules should "lie" at the domain level, and not higher.
In general, I know of two options for creating entities: using only a public constructor, or using a factory. If I understood you correctly, then you have a form with fields to fill in with their values, which are attributes of some entity?
There can be two scenarios here, the first one: when an entity, or rather an AGGREGATE, is retrieved from the repository for the purpose of editing, then everything is as you described above: the attribute value is incorrect - we throw an exception.
And the second scenario is entity creation. Nowhere can an invalid entity created with a statement be accessed new DomainObject()
except in a factory. Therefore, create a factory that allows you to build your entity, or rather the AGGREGATE, step by step. To do this, apply the Builder design pattern . At the end, call the Build() method , which will return you a valid AGGREGATE.
As for the intermediate results of "construction", then to obtain them, use the design pattern SHOT ( Memento ), for example like this:var snapshot = domainObjectFactory.GetSnapshot();
, where snapshot is a regular DTO box that contains entity attribute values and error lists, validity, etc. Well, then see for yourself how to be, if you pass ViewModel-s to the client, then using snapshot create view models and then them to the user's UI.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question