Here is a simplified requirement:
User creates a
Question
with multipleAnswer
s.Question
must have at least oneAnswer
.Clarification: think
Question
andAnswer
as in a test: there is one question, but several answers, where few may be correct. User is the actor who is preparing this test, hence he creates question and answers.
I am trying to model this simple example so to 1) match the real life model 2) to be expressive with the code, so to minimize potential misuse and errors, and to give hints to developers how to use the model.
Question is an entity, while answer is value object. Question holds answers. So far, I have these possible solutions.
[A] Factory inside Question
Instead of creating Answer
manually, we can call:
Answer answer = question.createAnswer()
answer.setText("");
...
That will create an answer and add it to the question. Then we can manipulate answer by setting it's properties. This way, only questions can crete an answer. Also, we prevent to have an answer without a question. Yet, we do not have control over creating answers, as that is hardcoded in the Question
.
There is also one problem with the 'language' of above code. User is the one who creates answers, not the question. Personally, I don't like we create value object and depending on developer to fill it with values – how he can be sure what is required to add?
[B] Factory inside Question, take #2
Some says we should have this kind of method in Question
:
question.addAnswer(String answer, boolean correct, int level....);
Similar to above solution, this method takes mandatory data for the answer and creates one that will be also added to the question.
Problem here is that we duplicate the constructor of the Answer
for no good reason. Also, does question really creates an answer?
[C] Constructor dependencies
Let's be free to create both objects by our selves. Let's also express the dependency right in constructor:
Question q = new Question(...);
Answer a = new Answer(q, ...); // answer can't exist without a question
This gives hints to developer, as answer can't be created without a question. However, we do not see the 'language' that says that answer is 'added' to the question. On the other hand, do we really need to see it?
[D] Constructor dependency, take #2
We can do the opposite:
Answer a1 = new Answer("",...);
Answer a2 = new Answer("",...);
Question q = new Question("", a1, a2);
This is opposite situation of above. Here answers can exist without a question (which does not make sense), but question can not exist without answer (which make sense). Also, the 'language' here is more clear on that question will have the answers.
[E] Common way
This is what I call the common way, the first thing that ppl usually do:
Question q = new Question("",...);
Answer a = new Answer("",...);
q.addAnswer(a);
which is 'loose' version of above two answers, since both answer and question may exist without each other. There is no special hint that you have to bind them together.
[F] Combined
Or should I combine C, D, E – to cover all the ways how relationship can be made, so to help developers to use whatever is best for them.
Question
I know people may choose one of the answers above based on the 'hunch'. But I wonder if any of above variant is better then the other with a good reason for that. Also, please don't think inside the above question, I would like to squeeze here some best practices that could be applied on most cases – and if you agree, most use cases of creation some entities are similar. Also, lets be technology agnostic here, eg. I do not want to think if ORM is going to be used or not. Just want good, expressive mode.
Any wisdom on this?
EDIT
Please ignore other properties of Question
and Answer
, they are not relevant for the question. I edited above text and changed the most of the constructors (where needed): now they accept any of necessary property values needed. That may be just a question string, or map of strings in different languages, statuses etc. – whatever properties are passed, they are not a focus for this 😉 So just assume we are above passing necessary parameters, unless said different. Thanx!
Best Answer
Updated. Clarifications taken into account.
Looks like this is a multiple choice domain, which usually has the following requirements
Based on the above
[A] can't ensure the invariant from point 1, you may end up with a question without any choice
[B] has the same disadvantage as [A]
[C] has the same disadvantage as [A] and [B]
[D] is a valid approach, but it's better to pass the choices as a list rather than passing them individually
[E] has the same disadvantage as [A], [B] and [C]
Hence, I would go for [D] because it allows to ensure domain rules from points 1, 2 and 3 are followed. Even if you say that it's very unlikely for a question to remain without any choice for a long period of time, it's always a good idea to convey domain requirements through the code.
I would also rename the
Answer
toChoice
as it makes more sense to me in this domain.A note. If you make the
Question
entity an aggregate root and theChoice
value object a part of the same aggregate, there's no chances one can store aChoice
without it being assigned to aQuestion
(even though you don't pass a direct reference to theQuestion
as an argument to theChoice
's constructor), because the repositories work with roots only and once you build yourQuestion
you have all your choices assigned to it in the constructor.Hope this helps.
UPDATE
If it really bothers you how the choices are created ahead of their question, there are few tricks you might find useful
1) Rearrange the code so that it looks like they are created after the question or at least at the same time
2) Hide constructors and use a static factory method
3) Use the builder pattern
However, everything depends on your domain. Most of the times the order of objects creation is not important from the problem domain perspective. What is more important is that as soon as you get an instance of your class it is logically complete and ready to use.
Outdated. Everything below is irrelevant to the question after clarifications.
First of all, according to DDD domain model should make sense in the real world. Hence, few points
Based on the above
[A] may contradict point 4 because it's easy to misuse and forget to set the text.
[B] is a valid approach but requires parameters that are optional
[C] may contradict point 4 because it allows an answer with no text
[D] contradicts point 1 and may contradict points 2 and 3
[E] may contradict points 2, 3 and 4
Secondly, we can make use of OOP features to enforce domain logic. Namely we can use constructors for the required parameters and setters for the optional ones.
Thirdly, I would use the ubiquitous language which is supposed to be more natural for the domain.
And finally, we can design it all using DDD patterns like aggregate roots, entities and value objects. We can make the Question a root of its aggregate and the Answer a part of it. This is a logical decision because an answer has no meaning outside of a question's context.
So, all the above boil down to the following design
P.S. Answering to your question I made few assumptions about your domain which might not be correct, so feel free to adjust the above with your specifics.