Can you explain Lazy Loading?
Introduction
Lazy loading is a concept where we delay the loading of the object until the point where we need it. Putting in simple words, on demand object loading rather than loading objects unnecessarily.
For example, consider the below example where we have a simple Customer
class and this Customer
class has many Order
objects inside it. Have a close look at the constructor of the Customer
class. When the Customer
object is created it also loads the Order
object at that moment. So even if we need or do not need the Order
object, it’s still loaded.
But how about just loading the Customer
object initially and then on demand basis load the Order
object?
public class Customer { private List<Order> _Orders= null; … … public Customer() { _CustomerName = "Shiv"; _Orders = LoadOrders(); // Loads the order object even though //not needed } private List<Order> LoadOrders() { List<Order> temp = new List<Order>(); Order o = new Order(); o.OrderNumber = "ord1001"; temp.Add(o); o = new Order(); o.OrderNumber = "ord1002"; temp.Add(o); return temp; } }
So let’s consider you have client code which consumes the Customer
class as shown below. So when the Customer
object is created no Order
objects should be loaded at that moment. But as soon as the foreach
loop runs you would like to load the Order
object at that point (on demand object loading).
Customer o = new Customer(); // order object not loaded Console.WriteLine(o.CustomerName); foreach (Order o1 in o.Orders) // Load order object only at this moment { Console.WriteLine(o1.OrderNumber); }
So how do we implement lazy loading?
For the above example if we want to implement lazy loading we will need to make the following changes:
- Remove the
Order
object loading from the constructor. - In the
Order
get property, load theOrder
object only if it’s not loaded.
public class Customer { private List<Order> _Orders= null; … … public Customer() { _CustomerName = "Shiv"; } public List<Order> Orders { get { if (_Orders == null) { _Orders = LoadOrders(); } return _Orders; } }
Now if you run the client code and halt your debugger just before the foreach
loop runs over the Orders
object, you can see the Orders
object is null
(i.e., not loaded). But as soon as the foreach
loop runs over the Order
object it creates the Order
object collection.
Are there any readymade objects in .NET by which we can implement lazy loading?
In .NET we have the Lazy<T>
class which provides automatic support for lazy loading. So let’s say if you want to implement Lazy<>
in the above code, we need to implement two steps:
Create the object of orders using the Lazy
generic class.
private Lazy<List<Order>> _Orders= null;
Attach this Lazy<>
object with the method which will help us load the order’s data.
_Orders = new Lazy<List<Order>>(() => LoadOrders());
Now as soon as any client makes a call to the _Orders
object, it will call the LoadOrders
function to load the data.
You will get the List<orders>
data in the Value
property.
public List<Order> Orders { get { return _Orders.Value; } }
Below goes the full code for this:
public class Customer { private Lazy<List<Order>> _Orders= null; public List<Order> Orders { get { return _Orders.Value; } } public Customer() { // Makes a database trip _CustomerName = "Shiv"; _Orders = new Lazy<List<Order>>(() => LoadOrders()); } }
What are the advantages and disadvantages of lazy loading?
Below are the advantages of lazy loading:
- Minimizes start up time of the application.
- Application consumes less memory because of on-demand loading.
- Unnecessary database SQL execution is avoided.
The only one disadvantage is that the code becomes complicated. As we need to do checks if the loading is needed or not, there is a slight decrease in performance.
But the advantages are far more than the disadvantages.
FYI: The opposite of Lazy Loading is eager loading. So in eager loading we load all the objects in memory as soon as the object is created.
Also have a look at below posted video on Lazy Loading: -
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
原文链接:http://www.codeproject.com/Articles/652556/Can-you-explain-Lazy-Loading
本文地址:http://www.cnblogs.com/Interkey/articles/LazyLoading.html
.NET version 4.0 Framework or later and VS2010 or later is required for the Lazy class.
Lazy<T> Class: http://msdn.microsoft.com/en-us/library/dd642331(v=vs.100).aspx
建议阅读原文后面的 Comments and Discussions .
相关阅读:
http://www.cnblogs.com/Allen-Li/archive/2012/03/15/2398063.html
作者:Cosmic_Spy
出处:http://www.cnblogs.com/Interkey/
有兴趣可以加群【.NET破解|反编译】请注明:博客园,谢谢~
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则肯定是你抄我的。
posted on 2013-10-14 15:41 Cosmic_Spy 阅读(633) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗