oop - How do you represent domain object behaviors using methods when a domain object is acted upon and not the actor? -
in domain driven design, there emphasis of placing behaviors correctly belong. i'm not sure constitutes "correct," though.
consider scenario: "the client withdraws amount of money bank account."
grammatically, have that:
'client' subject
'withdraw' verb
'amount' direct object
'account' affected prepositional object
classically, client execute this: account.withdraw(amount).
this read as: "the client commands bank account withdraw amount of money." however, not grammatically correspond original scenario, since 'bank account' direct object , infinitive removes 'withdraw' , 'amount' main clause primary idea conveyed 'client commands account'.
to adhere ddd, should regard code precisely describing original scenario. implies 'withdraw' method not model behavior performed by account (the account not subject), rather models behavior indirectly affects (the account prepositional object) , renamed 'waswithdrawnfrom'. moreover, method argument represents being affected by client opposed by object (it direct object a client subject, not an object subject).
if extrapolate example, can see methods may represent behaviors affect object (object not subject) , method arguments may represent direct objects of client subject.
however, whether or not methods should represent behaviors performed objects unclear.
so question is: how should use methods model object behavior?
1) methods should strictly represent behaviors affect object. (the object cannot act subject, grammatical direct/indirect/prepositional object)
2) methods may represent both behaviors affect object behaviors performed object. (the object may act subject or grammatical direct/indirect object)
you seems stuck in theoretical thesis. i'm not sure can apply theory works every scenario/project. behaviors appears when start bombarding model use cases. try let cases dictate behaviors. follow example:
actor customer want withdraw money account. way account.withdraw(money amount)
but question "how should use methods model object behavior?" common ddd question. how enrich model behavior? best hints find value objects. value objects comes behavior.
in example account , entity (aggregate root probably). money (value object currency , amount) should withdrawn account. should account single responsible (principle) control account balance , access it. regarding (2) question in end. methods absolutely used change objects inner state. when comes behaviors performed entity/value object customer.order(neworder) or advert.apply(candidatecv) can make entities communicate each other. can create chained behaviors through domain events can not leak business logic our application services. see udi dahan's blog or blog article regarding anemic domain model http://magnusbackeus.wordpress.com/2011/05/31/preventing-anemic-domain-model-where-is-my-model-behaviour/
i hope got ideas. it's difficult give concrete hints when abstract here. /best regards magnus
Comments
Post a Comment