When I saw the answers to my questions I could not believe that
they were coming from professional people, so I refrained myself
and tried to figure out a way to answered it positively. So here
is my attempt.
First of all, my main professional objective is to develop the
best software I can develop from my clients according to their
needs. My job is not to promote DDD, FDD, SOM, ORM whatever but to
build software.
As such I don't belong to any camp. I do have some conceptual
artifacts that from experience work better then others. But beuty
is in the eye of the beholder.
I came to DDD through my research about NHibernet, Ayende's work
and more importantly Udi (SAO) on his blog. I don't know them
personally but I like what they write in general. Their
brighteness led me here.
I suspect I'm not the only one in this situation.
The two comments suggested that I haven't done my homework. So
I'll try to dissipate any doupts about that.
As far as I see AR especifically introduces the concept of
Components into the Domain Model. Not so much from the point of
view reusability, but from the point of view of encaspulation
(Composition). Nothing new here. Prior to 2004 I had already read
some texts explaining similar if not identical concepts.
If all objects with the exception of Value Object are aggregate
roots, then the concept would be meaningless. But of course this
scenario is not the common in DDD I guess, so I take this out of
the equation.
So focusing on a scenario were Aggregate Roots are rich..
We know that Composition has hierachical structure (an object that
owns-scopes an object that own-scopes, etc). As such it requires
an hierachical view of both behaviour and information, in other
words knowledge. In this structures information flows mainly
downstream with exceptions rising upstream (events). This has both
advantages and disavantages from a design point of view.
1) Knowledge in general is not hierachical concept, as such when
you "force" Composition into a Domain Model one may indeed
interpreting the Ubiquotous Language (since the Model is an
interpretation of that language) in ways that change its meaning
and dynamics. In other words, you end up with a Model that may not
map reality as required.
2) Invariants is a fancy name for theorems nand corolaries but
with more generic meaning. Busineess rules either are axioms,
theorems or corolaries in a logic system. Anyway, the problem from
a design point of view is that the head of the composition is
often seen as the controller of all business rules in that
composition. It knows everything about the system, it can
potential rule enverything in that system, making aggregates
potentially anemicm delegating rules to them that are not
critical. We all have see that this can be bad (OrderManagers,
SecurityManagers and so on) in several instances.
3) ...
I'm not stating that DDD intent is not good, but by not offering
consistemt guidance about how businesss rules should be
distributed within an Aggregate Root i can only conclude that
either it is trivial or the issue is not well understood.
This is in contrast with SOM. Certain individuals commenting
negatively SOM (Streamlined Object Modeling) approach clearly some
have not grasped the concepts and the problems that are being
tackled in that book. Actually missguided business rule
distribution strategies can be veryfied in several different
instances on thread in this forum.
I don't like to be so abstract in my interventions so I will
present an example.
Say that a company has a chain of hotels and wants to build a
peace to manage inventory on their chanin (Inventory System). An
hotel has an entrance, several flours and two elevators. Each
flour has several rooms, each room several compartments. You have
different types of rooms. One may have just two compartments such
as room and bathroom, other may have a kichen too, a living room,
a mezzanine and so on. Each flour has halls, each hall is equiped
with fire extintors and so on and son. Just focusing on the
reality of a bathroom is already overwhelming (You have soap,
toals, shamppo an other items). Also in first class A rooms you
can only use settin sheets in the bed and other business rules. I
could go on an on describing this but elements are so many and the
time is scarse (Just imagine the biggest hotel you in LV).
Each place in the hotel according to DDD does not have global
identy (I understand the principle). A room, halls, bathroms, etc
etc all have local identity.
If we follow the sugestions on this forum of what should be the
aggregate root in the domain described succintely above (there
would be a lot more to say) then I can only conclude that Hotel is
the Aggregate Root of all places in it. As such any interaction
with say a bathroom must through the Hotel interface (A huge
Interface if you imagine the number of items that can be placed).
Putting a towel in a room, or using a sheet on a bed etc etc.
People here have presentéd arguments against Streamlined Object
Modeling, mainly stating that if you follow the patterns presented
you end up with a system that does not represent the Ubiquitous
Language. Obviously this can come only from people that are still
batteling with basic Domain Modeling concepts and I'll explain, or
with other motifs but building software.
One may argue that the Ubiqutous Language does not translate to
asking the Sheet to put itself in the bedroom bed, it is not
natural (The streamlined object modeling way). True, but it also
does not translate to ask the Hotel to put the sheet in the the
bedroom bed (aHotel.PutSheetInBedroomBed(roomid, ... sheet
info ...)). In reality what happens is that some employee in the
hotel, will go to the elevator, goes down the hall, enter the
room, go to the bed and put the bed sheet there directely! The bed
sheet needs to be according to the Room spec (Class A, ClassB etc
etc) but also acording to the bed spec (size). Then the employee
might log the action in some Log. I uses the bed sheet has an
example but I can use a light bulb.
If i'm looking where a bed Sheet is (has it own Identity and
probably is Aggregate Root or part of one (worst)), I can't ask
the Sheet where it is, simply becouse the sheet can't have a
reference to the bedroom as it is part of the Aggregate Root
Hotel. Even though this would be the most logical, efficient and
direct way to achive this goal.
This leads to a large list of methods in the Hotel to support
multiple types of materials, business logic, behaviours etc etc.
Invariants? Honestely, in a structure has complex has this
Invariants is the least of your problems.
If you are like me, I can only understand fully the implications
of something in contrast with others. But also when I see it
written in code.
Udi Hanan has an article entitled: How to create fully
encapsulated Domain Models (http://www.udidahan.com/2008/02/29/how-
to-create-fully-encapsulated-domain-models/).
A very interesting article has it deals precisely with
distributing business rules in a Domain model. To resolve a
business rule he introduces a concept GamesReportedLost to the
Model derived from Ubiqutous Language. This structure is basically
a list of games lost by a Customer. This list was necessary
becouse of a business rule:
"#3 No games can be added to the cart that the customer had
previously reported lost with regards to their rental membership."
Indeed he is not breaking the Aggregate Root concept when he does
customer.GameReportedLost.Contains(game). He is using a reference
to an aggregate in an Aggregate Root but only in the context of
one operation. So all is good right?
But has Ayende pointed out (first commentr) this list can be
HUGE. Udi answer with a complex solution to say the least. Why?
All to keep the Aggregate Root concept solid IMHO. This when there
was a far easier solution directily coming from the Ubiqutous
Language if we forget Aggregate Roots (a technical concern not a
domain language).
An alternative solution is presented in a article of mine
Distributing business rules when face to face with an Aggregate:
http://movablesharp.blogspot.com/search/?q=UDI
The problem is that my solution breaks the Aggregate Root
boundaries of Customer by asking the Product concept if it was
lost by the customer (Becouse LostGame is an object of the
Customer Aggregate Root). To do this I need to hold a reference to
a product that a customer has lost I think. But in the end of the
day that is the most natural and efficient thing to do (Objects
Think!).
One last note about local identity versus global identity and the
reasoning behind defining Aggregate Roots. (local indenty = > part
of aggregate root => can be only identified locally => the
Aggregate mediates the act of localizing an item given its ID).
This is the kind of reasonling I've been seeing in this forum
often, so execuse me If I don't know better.
Take a Web Page, does it have only a local ID? Or does it also
have a global ID?
I see it has having both! The URL of a web page is the global ID!
In the Ubiqutous Language I mean. I think we all agree that say an
Article in a Blog has global Identity.
On another side, putting a web page in say IIS Web Site, you don't
ask IIS to put it there. I go to the folder and put it there. IIS
does not even know that it is there unless it is requested.
Indeed, any aggregate part of an Aggregate Root can be be
potentially have a Global Identifier, although the concept only
has indentity whebn put into context.
This realization is the very same that sustains for instance the
ability to get hold of a room to but a light bulb in and to ask
the bulb to attact itself to the room.
Why, becouse the bedroom has a global identifier in the hotel
system. It is even more important then an hotel (I can have a room
without an hotel, but for sure I can't have an hotel without a
room).
Anyway, this post is getting long, but sometimes people need more
info to realize "trivial matters".
In DDD especially interested on Bounded Contexts, ContextMaps and
other more advanced concepts that in our industry does require
some "formal" insight. Also I'm very interestin in how it relates
to SOA etc etc. I think Aggregate Roots and Value Objects is a
moot point anyway, more the first the the second. I do Domain
Driven Sofware design all the time. I use the Ubiquotous Language
all the time even before the DDD book came out.
Maybe not the DDD camp way (came after the book). So I came in to
learn the DDD way but I have some concerns. So what? Does that
mean I need to learn how to search the internet or I lack reading
skills?
What do you think?
Nuno
------------------------------------