I apologize for so many questions, but I felt that they make the most sense only if they are considered as a unit
Note. All quotes from DDD: heart complexity in software (pages 250 and 251)
1)
Operations can be divided into two categories: teams and queries.
...
Operations that return results without side effects called functions. The function can be called several times and returns the same value each time.
...
Obviously, you cannot escape commands on most software systems, but the problem can be mitigated in two ways. Firstly, you can keep commands and queries strictly isolated in different operations. Ensure that the methods that cause the changes do not return domain data and are kept as simple as possible. Perform all queries and calculations in methods that do not cause observable side effects.
a) The author implies that the request is a function because it does not create side effects. He also notes that the function will always return the same value by which I assume that it means that for the same input we will always get the same result?
b) Suppose we have a QandC(int entityId) method that requests a specific domain object from which it extracts specific values, which, in turn, are used to initialize a new Value object, and this VO is then returned to the caller. Does the function above QandC phrase, since it does not change any state?
c) But the author also claims that for the same input the function will always produce the same result, which does not apply to QandC , since if we place several calls on QandC , this will lead to different results, assuming that in time between two calls this object has been modified or even deleted. Thus, how can we state that QandC is a function?
d)
Make sure that the methods causing the changes do not return domain data ...
The reason is that the status of returned non-VOs may be changed in some future operations, and therefore the side effects of such methods are unpredictable?
e)
Make sure that the methods causing the changes do not return domain data ...
Is a query method that returns an object that is still considered a function, even if it does not change state?
2)
VALUE OBJECTS are immutable, which means that, except for initiators called only at creation time, all their operations are functions.
...
An operation that mixes logic or computation with a state change must be reorganized into two separate operations. But by definition, this separation of side effects only into simple command methods applies to ENTITIES. After you finish refactoring the change from the query, consider a second refactoring to move the responsibility for complex calculations into the VALUE object. a side effect can often be completely eliminated by obtaining the VALUE OBJECT value instead of changing the existing state or by moving the entire responsibility in the VALUE OBJECT.
a)
VALUE OBJECTS are immutable, which means that, except for initiators called only during creation, all their operations are functions ... But by definition, this is a segregation of side effects into simple command methods that apply only to ENTITIES.
I think that the author says that all methods defined in VO are functions, which does not make sense, because although the method defined in VO cannot change its own state, it can still change the state of other, -VO objects ?!
b) Assuming that a method defined in essence does not change any state, do we consider such a method as a function, even if it is defined in essence?
c)
... consider the second refactoring to shift responsibility for complex calculations into a VALUE object.
Why does the author offer us only refactoring from objects that perform complex calculations? Why should we not reorganize simpler functions instead?
d)
... consider the second refactoring to shift responsibility for complex calculations into a VALUE object.
In any case, why does the author suggest that we reorganize functions from entities and place them inside VO? Just because the client makes it more obvious that this operation can be a function?
e)
A side effect can often be completely eliminated by obtaining the VALUE OBJECT value instead of changing the existing state or by moving the entire responsibility in the VALUE OBJECT.
This does not make sense, because, apparently, the author claims that if we move the team (i.e. the operation that changes the state) to VO, then we will essentially eliminate any side effects, even if the team changes the state. So, any ideas what the author was really trying to say?
UPDATE:
1b)
It depends on the perspective. A database query does not change state and, therefore, has no side effects, however, it is not deterministic in nature, since, as you indicate, the data can change. In the book, the author refers to functions associated with the value of the object and entity, which themselves do not perform external calls. Therefore, the rules do not apply to QandC.
Thus, the author described only functions that do not perform external calls, and as such QandC not the type of function that the author described?
1c)
QandC itself does not change state - there are no side effects. ground state can be changed out of band. In this regard, it is not a pure function.
But is this also not a Side-Effect-Free feature in the sense in which the author defined them?
1d)
Again, this is based on CQS.
I know that I am repeating myself, but I believe that the discussion in the book is based on CQS, and CQS does not consider QandC as a side effect function for free due to the probability of returning a QandC object that has its state changed (by some other operation) in the future ?
1e)
The query is considered from the point of view of CQRS, but it cannot be called a function in the sense that a pure function on VO is due to the lack of determinism.
I don’t quite understand what you were trying to say (the mixing part is in bold). It is possible that while QandC is considered a request, it is not considered a function due to the return of the object and such side effects are unpredictable, which makes QandC non-deterministic in nature
Thus, the author makes only these statements (see the citation in 1e ) under the implicit assumption that no operation defined in VO will ever try to change the state of objects other than VO?
2d)
Given that VOs are immutable, they are suitable for accommodating pure functions. This is another step towards freeing domain knowledge from technical limitations.
I don’t understand why moving a function from an object to VO would help free domain knowledge from technical limitations (I’m also not quite sure what you mean by technical - technical, like technological or ...)?
I assume that another reason for placing a function in VO is because it is much more obvious (to the client) that this is a function?
2e)
I see this as a clue to the source of the events. Instead of changing an existing state, you add a new event that represents the change. There is still a net side effect, however, the existing condition remains stable.
I must admit, I do not know anything about programming using a non-standard source, since I would like to wrap my head around DDD first. In any case, the author did not mean that simply moving the command to VO would automatically eliminate side effects, but instead it would be necessary to take additional actions (for example, implementing event-sourcing), only he “forgot” to mention that part
SECOND UPDATE:
2d)
One of the defining characteristics of an object is its identification .... Having placed business logic in VO, you can consider it outside the context of entity identity. This makes it easy to verify this logic, by the way.
I understand what you are doing (thinking of the concept from a distance), but on the other hand, I really do not. Why function inside an object under the influence of the identity of this object (if this function is a pure function, in other words, it does not change state and is determined)?
2e)
Yes, this is my understanding of this - there is still a pure “side effect”. However, there are different ways to achieve a side effect. One way is to change an existing state. Another way is to make the state change explicitly with the object representing this change.
I - To be sure ... From your answer, which I understood, this author did not mean that side effects would be eliminated simply by moving the command to VO?
II - Well, if I understand you correctly, we can move the command to VO (although VO should not change the state of anything and as such should not cause side effects), and this command is still allowed to create some kind of side effects inside VO but is this side effect somehow more acceptable (OR MORE CONTROLS) by making an explicit state change (which I interpret as the thing that was changed, is returned to the caller as VO)?
3) I have to say that I still do not quite understand why the SC state change method should not return domain objects. Perhaps because non-VO may be changed in some future operations, and therefore the side effects of SC are very unpredictable?
THIRD UPDATE:
The transfer of state control of the essence and the introduction of behavior in HE creates certain advantages. One main division of responsibilities.
a) You say that although a method describes the behavior of an object (and thus the object containing this method adheres to SRP) and, as such, belongs to the entity, it may still be a good idea to move it to VO? Thus, in essence, would we divide the responsibility of the subject into two even smaller responsibilities?
b) But will it not move the behavior in VO, basically turn this object into a simple data container (I understand that the object will still manage its state, but still ...)?
Thank you