What is Lazy Loading?

2019-01-07 03:19发布

What is Lazy Loading?

[Edit after reading a few answers] Why do people use this term so often?

Say you just use a ASP/ADO recordset and load it with data or ADO.NET Datasource for a gridview.

I guess I should have asked why people use the term Lazy Loading, what "other" types are their?

13条回答
smile是对你的礼貌
2楼-- · 2019-01-07 03:32

Lazy<T> is now part of c# 4.0 - there is a nice page on MSDN which explains the concept.

查看更多
小情绪 Triste *
3楼-- · 2019-01-07 03:34

is a Design pattern.

Lazy loading: Untill your code require some operation done by a particular object, object is not initilaized, and once it's initialized it doesn't re-initialize the object but uses the previously initialized object.

This makes your code much more efficient and helps managing memory usage.

Example Applications of Lazy loading:

Ghost Lazy initialization Value holder

查看更多
做个烂人
4楼-- · 2019-01-07 03:42

Lazy loading is a concept where we delay the loading of the object unit the point where we need it. Putting in simple words on demand object loading rather than loading the objects unnecessarily. For instance if you have a "Customer" class which has "Orders" object aggregated. So you like to load the customer data but the orders objects you would like to delay until your application needs it.

Below is a youtube video which demonstrates how to use lazy loading , how we can implement lazy loading and advantages and disadvantages of the same.

http://www.youtube.com/watch?v=2SrfdAkwmFo

查看更多
太酷不给撩
5楼-- · 2019-01-07 03:43

Here's an example from some actual Python code I wrote:

class Item(Model):
    ...
    @property
    def total(self):
        if not hasattr(self, "_total"):
            self._total = self.quantity \
                  + sum(bi.quantity for bi in self.borroweditem_set.all())
        return self._total

Basically, I have an Item class which represents an item in our inventory. The total number of items we have is the number that we own plus the sum of all of the items that we're borrowing from various sources. These numbers are all stored in our database, and it would be pointless to calculate this until the total is actually requested (since often Items will be used without the total being requested).

So the total property checks whether the _total field exists. If it doesn't, then the property code queries the database and computes it, then stores the value in the _total field so that it need not be recomputed the next time it's requested.

查看更多
在下西门庆
6楼-- · 2019-01-07 03:43

An example of Lazy Loading would be a grid or table with lots of data on a webpage to view where the application only loads what the users browser viewpoint size is at that time. When they scroll down to want to view more content or data, more data would be loaded into view at that moment.

This is becoming more of a common visual/interaction design pattern as well via ajax or jQuery.

And as mentioned above, opposite would be Eager Loading where you don't take client into consideration thus potentially having a performance hit.

查看更多
乱世女痞
7楼-- · 2019-01-07 03:44

The term lazy loading is usually used when talking about object relational mappers. If you use ADO.NET directly you always get eager loading (ie it always loads just what you specify).

OR-mappers like nHibernate support returning proxy objects that get "filled in" with the right data only when you access the data. That way you only load the data that you really use. This is a usefull feature when you specify a lot of relations between objects that can get loaded from the database, you don't want the OR-mapper to load all the related objects and the objects related to the related objects and so on. That can result in your whole database getting loaded.

This problem can be prevented by carefull design of your object model too. (using aggregates and only loading aggregate roots like in domain driven design is a way to get around this without using lazy loading).

Lazy loading can result in the or mapper doing lots of small database accesses instead of retrieving all the data you need once. This can result in performance problems too.

查看更多
登录 后发表回答