Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Few confusing things about passing references to non-root entities to external objects

Root entity can pass transient references to internal entities to external objects, but under the condition that external objects don't hold on to that reference after an operation is completed

1)

a) Why is it acceptable for external object having a reference ( to an internal entity ) for the duration of a single operation, but not acceptable for it to hold on to that reference for a duration of two operations? My point being, if it's bad to hold on to a reference for the duration of two operations, then it's probably equally bad to hold on to it for a duration of a single operation?!

b) Assuming SomeRootEnt Aggregate root passes transient reference of internal entity SomeIntEnt to external object, how should external object request SomeIntEnt? By invoking a particular method on a root – e.g. SomeRootEnt.BorrowMeIntEnt(...) - or should root directly expose internal entity as its property ( e.g. SomeRootEnt.SomeIntEnt )?

2)

a) Assuming SomeRootEnt root passes a reference to internal entity SomeIntEnt to external object, which in turn makes some modifications on SomeIntEnt, then doesn't this mean that root has no way of applying the appropriate invariant logic on those modifications ( ie root can't check for the integrity of modified SomeIntEnt?

b) Similarly, to my understanding at least, root also has no way to force the external object to drop a reference to internal entity after the completion of a single operation?

Thank you

UPDATE:

2a)

That is correct, which is why it is best to ensure that the passed object isn't modified, but is used in an immutable way. Moreover, the passed entity can still maintain a degree of integrity on its own.

Would it be primarily the responsibility of Aggregate root (and partly by passed entity) or of an external object ( which receives the transient reference ) to ensure that passed entity isn't modified? If the latter, then isn't the consistency of this aggregate really at the mercy of whoever developed the external object?

2b)

Correct and it is your responsibility to ensure this. Just like you have to ensure that a give value object is immutable (if needed) you have to consider the integrity of passed references.

I assume in most cases it would be the responsibility of external object to get rid of the reference as soon as an operation is completed?

like image 282
user437291 Avatar asked Oct 21 '22 19:10

user437291


1 Answers

1a) A reference to an entity may be needed to support a domain operation, however that reference should be transient in that it isn't held after the operation. It is only held only for the duration of the operation, not after it and therefore it does not follow by induction that it can hold for two operations. The point of this is to ensure that the aggregate, which passed the reference to an external entity, can maintain control of its constituents. You don't want its internal entities to be taken over by some other aggregate because then it is more difficult to reason about behavior.

1b) It can go either way, depending on the use case. A property is just a method in disguise.

2a) That is correct, which is why it is best to ensure that the passed object isn't modified, but is used in an immutable way. Moreover, the passed entity can still maintain a degree of integrity on its own.

2b) Correct and it is your responsibility to ensure this. Just like you have to ensure that a give value object is immutable (if needed) you have to consider the integrity of passed references.

Most of this is a general guideline because it results in a "well-behaved", easy to reason about, and easy to make consistent aggregates.

UPDATE

2a) Given the limitations of programming languages, there are limits to how well an aggregate can protect itself. As a result, "human intervention" is required, especially in more complicated scenarios like this one. It is true that the aggregate may come to be at the mercy of another, which is why these guidelines are in place.

2b) Yes. The external object can make use of an internal entity of another aggregate, however it reference should be transient - meaning that it is not persisted.

like image 66
eulerfx Avatar answered Oct 27 '22 10:10

eulerfx