简体   繁体   中英

Loading /Lazy loading of related entities

Scenario:

I have a (major) design problem. I have DTO classes to fill data from DB and use in the UI. The scenario I have is that:

I have a HouseObject which has TenantObject (one to many) with each tenant has AccountObject (one to many again) and so on (Example Scenario Only)

Problem:

Now my issue is, while retrieving data from DB for HouseObject, Should I get list of all TenantObjects and inturn list of all AccountObjects and so on? because of the one to many relationship, for one HouseObject potentially we are retrieving huge data for Tenants, Accounts and so on.

Should we just retrieve just HouseObject and fire off individual dependent queries per dependency? or should I get all data at once in single call and bind it on screen. Which is the desired solution?

Please advice.

If you looking for performance, and this is what I think you're looking for, you have to think in broad terms, not just Lazy/not lazy . You have to think, how much data you have, and how often it gets updated; where is it stored? Where application runs, how it is utilized, etc.

I see few scenarios:

  1. Lazy load of small chunks. I like this one because it is useful when your data is modified often. You have fresh data all the time.

  2. Caching in application layer. Here can be 2 sub-scenarios

    a. Caching ready-models. You prepare your models, fully initialized.

    b. Caching separate segments of data (houses, tenants, accounts) and query it in memory.

  3. Prepare your denormalized joined data and store it in Materialized (Oracle) or Indexed (Sql Server) view. You still will have a trip to DB but it will be more efficient than join data each time, or make multiple calls each time.

  4. Combination of #1 and #2.b and I like it the most. It requires more coding but gets you best results in performance and concurrency. Even if your data is mutating, you can have a mechanism to dump the cache. And if your Account changes, you don't need to dump Tenant.

And one more thing, if you need to update your data, remember - use different models. Your display models should be only ViewModel while you should have separate model for your Save . For example, your Save model may have field updatedBy while your View model doesn't.

You really don't have a "major" problem. It is normal daily problem for developers. You need to think of all aspects of your system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM