Are Persistence-Ignorant objects able to implement lazy loading?

Persistence Ignorance is an application of single responsibility principle, which in practice means that Domain Objects (DO) shouldn’t contain code related to persistence, instead they should only contain domain logic.

a) I assume this means that the code which contacts lower layers ( ie persistence layers ) lives outside of the domain model in other classes ( OC ) of a business logic layer?

b) If my assumption under a) is correct, then DO, say Customer, never contains methods such as GetCustomers or GetCustomerByID?

c) If my assumptions under a) and b) are correct, and assuming Customer domain object uses lazy loading for some of its properties, then at some point Customer‘s internal logic must contact OC, which in turn retrieves deffered data. But if Customer needs to contact OC to receive deffered data, then we can’t really claim that Domain Objects don’t contain logic related to persistence?!

Thank you

REPLYING TO jkohlhepp

1) I assume OrderProvider and CustomerProvider classes are contained within business logic layer?

2) I gather from your reply that my assumptions under b) are correct?

3)

… I would check to see if some private orders field was populated or
if it was null. If it is null …

But as far as I can tell, as soon as domain code needs to check whether private order field was populated, and if it isn’t,contacting OrderProvider, we are already violating PI principle?!

I believe you are correct in your assumptions A and B around persistence ignorance.

How you would best accomplish lazy loading of database objects is greatly dependent on your particular problem and implementation. However, I will attempt a generic answer to how to do lazy loading while still maintaining separation of concerns between persistence and domain logic classes.

I tend to implement persistence ignorance using the following classes:

  • Domain classes – e.g. Customer
  • Provider / repository classes – e.g. CustomerProvider
  • Generic database querying classes – e.g. DatabaseQuery

The DatabaseQuery class would be responsible for using the database driver to query the database and assemble the resulting data into a generic result set such as a DataTable. The CustomerProvider would be responsible for using the DatabaseQuery class to execute SQL against the database and use the results of that SQL to assemble Customer instances. Customer would be “pure” domain object that contained data and logic related to customers.

As for whether the provider classes should be in the business tier or the data tier, I don’t have a strong opinion. I can see a case for both. The important part is that you separate the responsibilities across classes.

So now let’s discuss lazy loading. Let’s say I wanted Customer to have a collection of Orders, but I don’t want to pull Orders out of the database unless the consumer tries to access them. I would create a property on Customer called Orders. In the getter of that property, I would check to see if some private orders field was populated or if it was null. If it is null, load the orders from the database using an OrderProvider. Otherwise, just return the collection that was already loaded.

In my opinion, the need for Customer to contact OrderProvider does not violate PI. Customer still doesn’t know how it gets orders. It just knows that it gets them from OrderProvider. There might be other reasons to decouple Customer from OrderProvider, but I don’t think PI is a problem here.

This assumes that you are doing persistence ignorance manually. If you are a using an ORM framework such as Entity Framework or Hibernate, then those frameworks generally have features for supporting lazy loading automagically.

4

You just have some wireup class that populates the domain objects (say, something called “repository”). You can implement lazy loading or whatever kind of cache coherence scheme you want and the domain objects are none the wiser. You’re separating the responsibility of populating domain objects from being domain objects.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật