java - How to handle getOrCreateIfAbsent inside a DDD aggregate? -


i have situation in requirements see if has strong opinion it.

the project i'm working on requires that, given customer, once customer adds product cart, if cart not present, cart has created. @ moment, aggregate customer , contains cart contains product. because system kind of side 1 supports real e-commerce project, creation of customer , cart taken granted once "addproductcommand" received. in situation, both customer , cart have created if not there already.

my current implementation creates customer @ application service if doesn't exist. once created, pass addproductrequest using customer.addproduct(addproductrequest). addproductrequest contains cart id , product id. problem comes here. once in customer aggregate, if cart not there, have create it. basically, don't have addcart in customer cart entity call first call addproduct. need create cart if it's not there inside customer aggregate , add product it. , this, don't create factory don't want have static method complicates testing , can't create new factory inside aggregate either. create cart entity using new operator in protected method override when testing verify i'm doing there.

so question is, valid approach? in head, add product cart, cart should added first customer , fail if it's not there. way need add logic application service first ask customer if has cart id, create otherwise, , add customer before adding product. add domain service given request need inject factory create cart it, when i've read in several places domain service shouldn't injected factory, should work of application service.

i way project more complicated in future there yet layer product added list of vouchers having create customer, cart , product if not there when addvouchercommand consumed. in situation, if don't want create entities inside model, need check @ application/domain service whether each aggregate or entity has necessary entity inside it, don't think ddd friendly, or keep doing i'm doing @ moment. is, each aggregate/entity in charge of create necessary entity before calling addxxxx method on it.

some simplified code explain i'm doing @ moment , i'm going have in future:

public class customerservice {     public void addvoucher(addvoucherrequest addvoucherrequest) {         customer customer = customerrepo.load(customerid);         customer.addvoucher(addvoucherrequest);         customerrepo.save(customer);     } }  public class customer() {     public void addvoucher(addvoucherrequest addvoucherrequest) {         cart cart = getorcreateifabsent(addvoucherrequest.getcartid());         cart.addvoucher(addproductrequest);     }     private cart getorcreateifabsent(long cartid){         optional<cart> cart = carts.stream().filter(cart -> cart.getid() == cartid).findfirst();         return cart.orelseget(() -> {              cart newcart = createcart(cartid);              carts.add(newcart);              return newcart;         }      }      protected cart createcart(long cartid) {          return new cart(cartid);      } } public class cart() {      public void addvoucher(addvoucherrequest addvoucherrequest) {          product product = getorcreateifabsent(addvoucherrequest.getproductid());          product.addvoucher(addvoucherrequest);      }      private void getorcreateifabsent(long productid) {          optional<product> product = products.stream().filter(product -> product.getid() == productid).findfirst();         return product.orelseget(() -> {              product newproduct = createproduct(productid);              products.add(newproduct );              return newproduct ;         }      }      protected product createproduct(long productid) {          return new product(productid);      }  } 

any suggestions?

thanks!

choosing wait customer order create him/her in new system rather importing customers legacy system upfront purely technical decision, has nothing domain imo.

therefore, shouldn't affect way design domain objects in way - understand it, getorcreateifabsent() should behavior in kind of application service, not in 1 of entities.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -