延迟加载 - 什么是最好的方法呢? [英] Lazy loading - what's the best approach?

查看:130
本文介绍了延迟加载 - 什么是最好的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过懒加载无数的例子 - 有什么是你的选择。

I have seen numerous examples of lazy loading - what's your choice?

鉴于比如一个模型类:

public class Person
{
    private IList<Child> _children;
    public IList<Child> Children
    {
        get {
            if (_children == null)
                LoadChildren();
            return _children;
        }
    }
}

Person类不应该知道它是如何儿童装东西....还是应该呢?当然,它应该控制在属性填充,还是不行?

The Person class should not know anything about how it's children are loaded .... or should it? Surely it should control when properties are populated, or not?

你有或者你会使用不同的方法,夫妻与其子集合在一起的人的仓库,如使用<一个href=\"http://stackoverflow.com/questions/375881/lazy-loading-and-lazy-execution-patterns\">lazyload一流的 - 即使是这样,我不想lazyload类在我的模型架构模糊

Would you have a repository that couples a Person together with its children collection or would you use a different approach, such as using a lazyload class - even then, I don't want a lazyload class blurring in my model architecture.

您将如何处理性能,如果第一个请求人,然后将其子女(在这种情况下,即不延迟加载),或以某种方式延迟加载。

How would you handle performance if first requesting a Person and then its Children (i.e. not lazy loading in this instance) or somehow lazy loading.

难道这一切归结为个人的选择?

Does all this boil down to personal choice?

推荐答案

最好的懒加载是避免它;)线程安全是你必须处理迫在眉睫的问题。我没有的我如何经常看到的生产系统与8个CPU内核数运行延迟加载的每一个的延迟加载使用模式8倍。至少在服务器创公司所有服务器芯具有在相同的地方结束的倾向。

The best lazy loading is avoiding it ;) Thread safety is an immediate problem you'll have to handle. I have no count of how often I have seen production systems with 8 cpu cores run lazy loading 8 times for every single lazy loading pattern in use. At least on server startups all server cores have a tendency to end up in the same places.

让一个DI框架构建它,而不是你,如果你能。如果你不能,我还是preFER明确的建设。所以AOP神奇种种根本不跟我砍它,去在类外显式构造。不要把它的人类中,只是使构建以适当方式对象的服务。

Let a DI framework construct it for you instead, if you can. And if you cannot, I still prefer explicit construction. So all sorts of AOP magic simply do not cut it with me, go for explicit construction outside the class. Don't put it inside the person class, just make a service that constructs the objects in the proper manner.

介绍或多或少透明地做这些事情的看起来的是一个不错的主意,但我还没有遇到过没有不可预见的和有问题的后果,实现神奇的层次。

Introducing "magic" layers that more or less transparently do these things seem like a nice idea, but I have yet to come across implementations that do not have unforseen and problematic consequences.

这篇关于延迟加载 - 什么是最好的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆