object - DDD - Aggregate Root - Example Order and OrderLine -
i trying hands dirty learning ddd (by developing sample ecommerce site entities order
, orderlines
, product
, categories
etc). perceive aggregate root concept thought order
class should aggregate root orderline
.
things went fine far, confused when define create order flow ui. when want add order line order object, how should get/create instance of orderline
object:
- should hardcode new
orderline()
statement in ui/service class - should define method parameters
productid
,quantity
etc inorder
class?
also, if want remove hardcoded instantiations ui or order
class using di. best approach this?
from perceive aggregate root concept thought order class should aggreagrte root orderline.
yes, orderline's should under order root, since orderline's make no sense outside of parent order.
should hardcode new orderline() statement in ui/service class
probably not, though how happens , made work. problem, see it, object construction happens in different contexts, , validation constraints differ depending on context.
should define method parameters productid,quantity etc in order class?
as in:
public orderline addorderline(product product, int quantity ... )
this 1 way it. notice used product class instead of productid. 1 preferable other. find use both lot various reasons - have id , there's no reason pull aggregate root, need other root validate operation.
another way implement custom collection children.
so have:
order.orderlines.add(product, quantity);
this feels little more natural or oo, , in particular if entity root has many child collections avoids clutter.
order.addorderline()
, order.addxxx()
, order.addyyy()
, order.addzzz()
versus
order.orderlines.add()
, order.zzzs.add()
, order.yyys.add()
also, if want remove hardcoded instantiations ui or order class using di. best approach this?
this textbook case factory pattern. inject such factory custom collections support instantiation in add()
methods.
Comments
Post a Comment