简体   繁体   中英

Implicit Lazy Loading vs Explicit Lazy Loading

I've been reading Entity Framework and people were crying over why there was not implicit lazy loading or something. Basically I've been searching things about Lazy Loading and now I know what it is : It is a design pattern which allows us to load objects when they are really needed.

But what is the difference between Explicit Lazy Loading and Implicit Lazy Loading.

Thanks in advance...

If you eg have an entity "OrderRow" and another entity "Order", there will be a navigational property on the OrderRow that points to the Order it belongs to.

Currently Entity Framework supports only Explicit Lazy Load which means that if you have retreived a number of OrderRows and want to check something on the Order you need to:

// or is an OrderRow
if(!or.Order.IsLoaded)
    or.Order.Load()

or.Order.Cancel();

However if you have implicit lazy loading you don't need the IsLoaded check, it will be done automatically, you can do or.Order.Cancel() directly and the Order will be loaded automatically if needed. This is how linq-to-sql works and it saves some typing and some risk for mistakes. On the other hand it makes it less clear exactly when and how database access will be performed. With implicit load it is easy to write inefficient code that makes one DB roundtrip for each line to be fetched from a table.

Explicit means you explicitly wrote your code to lazy load.

Implicit means that the framework (in this case EF) does lazy loading itself, whether you intended to or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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