Sunday, 18 March 2018

Domain Driven Design Lazy Loading

Lazy loading sounds straightforward, but DDD manages to complicate things. DDD states that entities should not hold references to services or repositories. That is really not debatable, and people that have even the most basic understanding of DDD will follow this rule without questioning it. So if that is the case, then how do you lazy load, because the entity has to call something to get the data. This is a discussion I had with my good friend Daniel Mouritsen and it seems that a friend of his even stalked poor Eric Evans to get an answer. Now I can't promise that Eric Evans will be happy with my solution, so it's upto you to decide. 


An example

Let's take a very simple example. We have a User entity and a Wallet entity. The users can have multiple wallets. A user entity can look for an user by id, but we don't want to load the wallets eagerly, we want to load the wallets only if something actually tries to access them.



Functional to the rescue

While we're not allowed to hold references to other services or repositories in our entities, how about a Java 8's Supplier? This will not create a direct dependency between the User and the Wallet repository, the User class has no idea that the Wallet Repository even exists. So the User changes slightly:


What that means is that we can set the wallets as a lambda expression and most importantly, that lambda expression is not called unless we are accessing the wallets. And here's what we need to do in the repository:

Pretty straightforward, right? The full example is available here.
If you liked this, don't forget to share!




27 comments:

  1. Packers and Movers Bangalore as a Services providing company can make all the difference to your ###Home Relocation experience. Bangalore based Company which offers versatile solutions, Right team that easily reduce the stress associated with a ✔✔✔ Household Shifting, ***Vehicle Transportation. we help things run smoothly and reduce breakages and offer you seamless, Affordable, Reliable Shifting Services, Compare Shifting Charges @

    Packers And Movers Bangalore

    ReplyDelete

  2. ان الرائد تقدم افضل الخدمات النزلية في المدينة المنورة بارخص الاسعار يمكنك زيارة التالي للمزيد من المعلومات :

    افضل شركة تنظيف منازل بالمدينة المنورة افضل شركة تنظيف منازل بالمدينة المنورة
    شركة مكافحة حشرات بالمدينة المنورة شركة مكافحة حشرات بالمدينة المنورة
    شركة تنظيف مجالس بالمدينة المنورة شركة تنظيف مجالس بالمدينة المنورة
    شركة تنظيف بالمدينة المنورة شركة تنظيف بالمدينة المنورة

    ReplyDelete
  3. Packers and Movers Pune Provide High Quality ***Household Shifting, Home/Office Relocation, Insurance, Packing, Loading, ###Car Transportation Service Pune and High experiences, Top Rated, Safe and Reliable, Best and Secure Packers and Movers Pune Team List. Get ✔✔✔ Affordable Rate Charts and Compare Quotation and Save Money and Time .
    Packers And Movers Pune

    ReplyDelete
  4. Which Best Foot Massager To Buy – Which Foot Massager Is Right For You? If you ask which Foot Massager model is the finest this year, certainly I would recommend going through this guide to know which Best Foot Massager is actually market leaders.

    ReplyDelete
  5. Packers and Movers Chennai Give Safe and Reliable ***Household Shifting Services in Chennai with Reasonable ###Packers and Movers Price Quotation. We Provide Household Shifting, Office Relocation, ✔✔✔Local and Domestic Transportation Services, Affordable and Reliable Shifting Service Charges @
    Packers And Movers Chennai

    ReplyDelete
  6. Packers and Movers Pune Provide High Quality ***Household Shifting, Home/Office Relocation, Insurance, Packing, Loading, ###Car Transportation Service Pune and High experiences, Top Rated, Safe and Reliable, Best and Secure Packers and Movers Pune Team List. Get ✔✔✔ Affordable Rate Charts and Compare Quotation and Save Money and Time .
    Packers And Movers Pune

    ReplyDelete
  7. Packers and Movers Pune Provide High Quality ***Household Shifting, Home/Office Relocation, Insurance, Packing, Loading, ###Car Transportation Service Pune and High experiences, Top Rated, Safe and Reliable, Best and Secure Packers and Movers Pune Team List. Get ✔✔✔ Affordable Rate Charts and Compare Quotation and Save Money and Time .
    Packers And Movers Pune

    ReplyDelete
  8. How to Use a Clothes Steamer A garment steamer is a great way to remove wrinkles from clothes. Steamers work by heating water until it becomes steam.

    ReplyDelete
  9. Hire Best Packers And Movers Mumbai for hassle-free Household Shifting, ***Office Relocation, ###Car Transporation, Loading Unloading, packing Unpacking at affordable ✔✔✔ Price Quotation. Top Rated, Safe and Secure Service Providers who can help you with 24x7 and make sure a Untroubled Relocation Services at Cheapest/Lowest Rate
    Packers And Movers Mumbai

    ReplyDelete
  10. Get Shifting/Relocation Quotation from ###Packers and Movers Delhi. Packers and Movers Delhi 100% Affordable and Reliable ***Household Shifting Services. Compare Transportation Charges and Save Time, Verified and Trusted Packers and Movers in Delhi, Cheap and Safe Local, Domestic House Shifting @
    Packers and Movers Delhi

    ReplyDelete
  11. Packers and Movers Hyderabad Give Certified and Verified Service Providers, Cheap and Best ###Office Relocation Charges, ***Home Shifting, ✔ ✔ ✔ Goods Insurance worth Rs. 10,000, Assurance for Local and Domestic House Shifting. Safe and Reliable Household Shifting Services in Hyderabad with Reasonable Packers and Movers Price Quotation @
    Packers And Movers Hyderabad

    ReplyDelete
  12. Get Packers and Movers Jaipur List of Top Reliable, 100% Affordable, Verified and Secured Service Provider. Get Free ###Packers and Movers Jaipur Price Quotation instantly and Save Cost and Time. Packers and Movers Jaipur ✔✔✔Reviews and Compare Charges for household Shifting, Home/Office Relocation, ***Car Transportation, Pet Relocation, Bike SHifting @
    Packers And Movers Jaipur

    ReplyDelete
  13. Hire Best Packers And Movers Mumbai for hassle-free Household Shifting, ***Office Relocation, ###Car Transporation, Loading Unloading, packing Unpacking at affordable ✔✔✔ Price Quotation. Top Rated, Safe and Secure Service Providers who can help you with 24x7 and make sure a Untroubled Relocation Services at Cheapest/Lowest Rate
    Packers And Movers Mumbai

    ReplyDelete
  14. Packers and Movers Gurgaon Provide Reliable, Safe and Certified Service Provider list, Get Free ***Best Price Quotaition and Compare Charges. ✔✔✔ Hassle free Household Shifting Services, High Quality packing Material, Office Relocation, Car Transportaion, ###Local and Domestic Shifting Service @
    Packers And Movers Gurgaon

    ReplyDelete
  15. Local Packers and Movers Bangalore List, Get Best Price Quotes, Compare Movers and packers Charges, Top, Local Household Shifting Services
    Packers And Movers Bangalore

    ReplyDelete
  16. Packers and Movers Ahmedabad - We Provide ***Best Service Providers, Safe, Reliable, Affordable, Trusted ###Movers and Packers in Ahmedabad List, Household Shifting, Office Relocation: Choose Top Verified Packers and Movers Ahmedabad Compare ✔✔✔Shifting Service Chrages, Price Quotation, Rate List Charts and Save Money and Time @
    Packers And Movers in Ahmedabad

    ReplyDelete
  17. Hire Best Packers And Movers Mumbai for hassle-free Household Shifting, ***Office Relocation, ###Car Transporation, Loading Unloading, packing Unpacking at affordable ✔✔✔ Price Quotation. Top Rated, Safe and Secure Service Providers who can help you with 24x7 and make sure a Untroubled Relocation Services at Cheapest/Lowest Rate
    Packers And Movers Mumbai

    ReplyDelete