-3

I have 2 domains Customer and Address using using EventSourcing and CQRS in our Monolithic app, below is my query:

Customer Domain:

class Customer {   //Aggregate class
   String custId;
   CustAddress custAddress;
}

class CustAddress {
   String addressId;
   String city;
}

Address Domain:

class Address {   //Aggregate class
  String addressId;
  AddressVO addressVO; 
}

class AddressVO {
  String city;
  String pincode;
}

So, in my Customer Domain, I need a Address pincode in my code, then from below which approach is correct?

Approach 1:

 Use directly AddressVO class inside Customer class and then use like **customer.addressVO.pincode** after modifying Customer class as below:
   class Customer {   //Aggregate class
       String custId;
       CustAddress custAddress;
       AddressVO address;  //from Address Domain
    }

Approach 2:

Step1: Create another field "pincode" inside "CustAddress" class
Step2: CustAddress custAddress = addressDomainReadModelRepo.getById(custAddress.getAddressId()) //Here UnMarshall happens automatically via Jackson jar
Step3: then get pincode value as customer.custAddress.pincode

IMO, approach 2 should be correct. This is actually not opinion based query, instead trying to know what should be the correct approach to implement, esepcially when using eventsourcing and cqrs, in my case its a Monolithic app.

I would not have asked above query incase if we are using Microservices (say Customer and Address microservices), then in this case, we need to follow Approach 2 only.

Please try to understand my query...

4
  • I have been downvoted by 3 people... thats okay, but atleast mention what exactly wrong I've asked.. FYI, I am new to all these ddd, eventsource, cqrs stuff..and I will be very glad, if someone can able to answer my query..
    – john
    Sep 14 '20 at 11:26
  • Does anyone can answer above one pls?
    – john
    Sep 17 '20 at 5:24
  • Could you please confirm that your question is about how to retrieve related data between microservices? Sep 21 '20 at 13:17
  • I would also encourage you to please check this discussion as well. Sep 21 '20 at 13:18
2

When using eventsourcing you can have a projection build by events from both the aggregates that has all the information you need in it, indexed by what you need.

Your domain (write) model should not be changed because of "queries" issues.

4
  • Sorry for late reply.. May I know how to get data from one aggregate into other aggregate? and can I use ReadModelRepository inside write model to get data of other aggregate?
    – john
    Oct 19 '20 at 4:18
  • "May I know how to get data from one aggregate into other aggregate?" You shouldn't need that, that's the correct response :D Main curiosity to have a better response, is, what do you need that for? Is it to do some validation and maintain some invariant during a command execution, or is it because of some UI needs?
    – rascio
    Oct 19 '20 at 15:07
  • From the second question about the ReadModelRepository, I assume you need that value in the write model to do some validation (not to duplicate the data for some UI needs), in this case is it ok to use it, I would only suggest you to decouple the operation from the usage of a repository. Having some kind of interface that express in its contract what you need (so returning just the value you need, or a custom DTO for this use case), in this way you are defining a query in CQRS terms, and implementing that using the repository
    – rascio
    Oct 19 '20 at 15:14
  • In this way you are expressing your need in the domain model (through the interface), but it is decoupled from how the other bounded context is implmented, if tomorrow you want to change how it is stored you have to reimplement how the query is executed, but the interface can be the same
    – rascio
    Oct 19 '20 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.