3
votes

I would like to ask about lazy loading. I have read often that we should deny it, but why should I load data that maybe never used?

As an example for discussion let's use a Customer:

-Id
-Title
-FormOfAddress
-FirstName
-LastName
-Picture*
-DOB
-Phone
-Mobile
-Address*/Billing Address*
    -Id
    -Street
    -Number
    -Country*
        -Id
        -Name
        -Zipcode
-Bankdetails*
    -Id
    -AccountHolder
    -AccountNumber
    -Bank*
        -Id
        -Name
        -BankCode
    -IBAN

The * marks the Object's which I would Lazy load according to the motto "load only what you need".

Edit

Ok it seems my question isn't clear enough, so here is maybe a better formulation of what I want to know:

I want to know why mostly people dissuade from lazy load, is it because they can't use it or does it have really bad disadvantages?

2
and what to you want to know now? if you want the "*" lazy loaded make them Lazy<T> in your viewmodelblindmeis
@blindmeis i want to know why mostly people dissuasion lazy load is it because they can't use it or dies it have really bad disadvantages i missWiiMaxx
Its up to you to make lazy loading useful or turn it into disaster. Just like with any other pattern out there. From what little information you gave no1 will tell you if you should use it or not. What i can say, is that this pattern definetely can be used for good.Nikita B
@wiimaxx i would say it depends if you have a view for your customer and there is a button which open bankdetails , then lazy bankdetails would be good. but if the view contains the bankdetails anyway - the lazy would have no effectblindmeis

2 Answers

5
votes

It is common to use Lazy Load as a tuning mechanism, for example if you're loading all the data and are not noticing any performance issues, then truth is you probably don't need to worry yourself with the (slightly) added complexity of the Lazy Load Pattern.

However if you are noticing performance issues then you should take a look at the pattern, identify the most commonly used data (as you have noted above the data marked with *) as you have done, and only load that data. You will need some performance analysis to determine whether the times when you do need the extra data (fields NOT marked by *) decreases the performance of the system/slows down any other operations in the same domain (ones that depend on or are dependant on the operation you are describing)

If you are using .NET 4.0 (and above), the Lazy<T> can help reduce some complexity on implementing Lazy Load (it takes care of thread safety for you too!) This Code Project article can help with the implementation

0
votes

There is a reason for lazy loading. If you know you wont use your specific data a lot it is just about right to load it lazy. You should avoid it if your not completely familiar with lazy loading as you might cause errors. Mostly thats dedicated to specific technologies, i am just thinking of jpa hibernate. In case of Lazy Init you should make sure you dont want to reload your data too often as you could run into performance problems, but i guess thats it.