Lazy loading - what's the best approach?

前端 未结 7 906
慢半拍i
慢半拍i 2020-12-31 07:22

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

Given a model class for example:

public class Person
{
    private IList

        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 07:54

    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.

    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.

提交回复
热议问题