简体   繁体   English

加载/延迟加载相关实体

[英]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. 我有DTO类来填充数据库中的数据并在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) 我有一个HouseObject,其中有TenantObject(一对多),而每个租户都有AccountObject(又是一对多),依此类推(仅示例场景)

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? 现在我的问题是,当从DB for HouseObject检索数据时,我是否应该获取所有TenantObjects的列表以及所有AccountObjects的反过来的列表等等? because of the one to many relationship, for one HouseObject potentially we are retrieving huge data for Tenants, Accounts and so on. 由于一对多的关系,对于一个HouseObject,我们可能正在为租户,帐户等检索大量数据。

Should we just retrieve just HouseObject and fire off individual dependent queries per dependency? 我们是否应该只检索HouseObject并为每个依赖项触发单个依赖项查询? 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 . 如果您正在寻找性能,而这正是我想要的,那么您就必须从广义上考虑,而不仅仅是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 这可以是2个子场景

    a. 一种。 Caching ready-models. 缓存就绪模型。 You prepare your models, fully initialized. 您准备了完全初始化的模型。

    b. 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. 准备您的非规范化联接数据,并将其存储在实例化 (Oracle)或索引 (Sql Server)视图中。 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. #1和#2.b的组合,我最喜欢。 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 . 显示模型应仅为ViewModelSave则应具有单独的模型。 For example, your Save model may have field updatedBy while your View model doesn't. 例如,您的保存模型的字段可能已updatedBy视图模型的字段未更新。

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. 您需要考虑系统的各个方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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