java - How to integrate Repository with DDD and Spring -
i want create app following ddd aproach using spring. suposse have business model class foo , interface foorepository.
ddd tells implementation of foorepository should include in infrastructure layer.
i want use curdrepository if define in domain layer:
public interface foorepository extends crudrepository<foo, long>{ // methods } i break core concept domain layer (foorepository interface) must not know infrastructure layer (crudrepository).
i'm reading domain driven design few months ago haven't found framework supports purely.
how can right way?
in layered architecture have 3 layers: application, domain , infrastructure.
infrastructure
here put implementation of repository. in case implementation of crudrepository implement directly in concrete classes, without use of intermediate interface. make no whatsoever assumption how single object in warehouse behave, put them there , retrieve them efficiently. way have no knowledge of domain. offer domain interface interact with: set of public methods of warehouserepository.
public class warehouserepository implements crudrepository<foo, long> { ... } domain
here various part of model interact warehouserepository when inside unitofwork/transaction. in method adjustquantityplus se domain logic not interesting application , need not known @ infrastructure level.
public class saleorder { public adjustquantityplus(lineitemid lineitemid, warehouserepository warehouserepository) { this.lineitems.get(lineitemid).addone(); //<-- add 1 order product product = warehouserepository.findbylineitem(lineitem); product.minusonefromstock(); //<-- decrease 1 stock } } application
here start , stop transactions (uowork) manipulates many domain objects. every business method correspond business use case.
public class customereventsmanager { @inject warehouserepository warehouserepository; @inject saleorderrepository saleorderrepository; @transactional public wantsonemoreof(productid productid, saleorderid saleorderid) { saleorder saleorder = saleorderrepository.findbyid(saleorderid) saleorder.adjustquantityplus(producttolineitem(productid), warehouserepository); //<-- add product webpage.showpromodiscount(); //<-- show promotional advertisement } } the above code transaction, if system couldn’t add product order don’t want give discount customer. adjustquantityplus in turn inner “transaction” domain logic, hidden application layer.
Comments
Post a Comment