Lazy Loading Design Pattern | Lazy Loading in C# | Lazy Loading Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we'll try to understand what exactly is the concept of lazy loading now lazy loading you can think about it is a design pattern or it's a concept where we delay the loading of the objects until the point where we need it or I'll say in other words on demand object loading rather than loading the objects unnecessarily now to demonstrate lazy loading what I have done is I have created a very simple class here called as a customer class and this customer class has lots of order objects and if you watch the constructor of the customer class in the constructor of the customer class I am loading both the customer data as well as the order data in other words as soon as you create the customer object the customer data will be loaded as well as the order object data will be loaded so you can see that I have a very simple console application over here and this console application I have created the customer class so at this point that means at this point here both the customer plus the order object data will be loaded right so if I run this application very quickly so when you run the application and if you see the ad watch window you can see that the customer data is loaded as well as the composed order objects are loaded so you can see that you know after this line I have put a debug point here so the application is currently waiting here but as soon as the customer object is loaded both the customer data as well as the composed of objects that is order objects is also loaded now this is fine you know but think about that if the compose objects are huge in numbers if they are heavy for example you have orders then you have phones right then you have addresses and etcetera so what you would like to do is you would like to delay this compose object loading in other words for example if you see here you know at this moment you'd probably would like to just load the customer data right but as soon as the for each loop starts that means on demand basis you would like to go and load the order objects this is termed as lazy loading so as the application demands the class loads is it's composed objects so how do we achieve this so the first thing what we need to do is in order to achieve lazy loading is we need to go and remove this code which actually loads the order objects in the constructor right so we need to go and remove this code from here and then when the get is done on the order collection right at that moment what I will do is I will go and make a check here so I'm going to say here if under score orders is equal to null so in other words if it is null then only load the orders object so what will happen here is you know when anyone does a get on the order collection at that time only the order object will be loaded and not during the construction construction of the object itself right so now if I go and run the application so if I go and now do a fi here you can now see that the order collection is null and the customer data is loaded right but as soon as I start the for each loop so when I start the foreach loop there is a get done on the order collection over here at this moment you can see we do a get right so as soon as the first get is done on the orders collection this object will be loaded so if you watch here till these two statements we never needed the order collection right so you can see that the order collection is null and that is good but as soon as there is a demand of the orders collection so as soon as there is a get done so I will just go and move this debug point ahead I'll do a f10 here so as soon as I do a f10 you can see now the count is 2 so at this moment now he has loaded the orders collection so when the for each loop starts the order collection is loaded so this is one way of implementing lazy loading by doing a check here okay so wherever the object demand is needed just before that you do a check and you do the loading of the object now this is nice so the code is working the logic is working but think about a situation you know where your customer class has lots of order objects it has lot of addresses objects it had a lot of phones object and if you put this if check all over your class right your class will become very complicated right so you'd love to see some ready-made functionality which you can get from dotnet framework so that you can minimize this complexity of lazy loading right and the good news is that we have a very nice class provided by dotnet framework called as the lazy class it's a generic class lazy and by using the lazy class you can implement lazy loading and you can minimize the complexity of your class so to implement lazy loading the first thing is you need to go and define this complete object that is the list of order which you want to load lazily by saying lazy of the list of order right and the next thing is we don't need this if condition now anymore so you don't need to write a scattered code across your class of these if conditions and when the data is loaded inside this lazy object right the actual data is loaded inside value the value property so you see orders dot value so this will actually give you you know the the collection of lists of order ok now the next thing you know what this lazy order object needs to know is from where he will get the orders data right so we need to go and we need to tell him here that you will get this data from you will get this list of ordered data from this function that is load orders right so if you see this lazy class here you can see in this easy class I can go and I can pass a function here so I will say here yes you will get this data from this function so load honest the variable name is underscore orders it is underscore orders what so you can see now the next thing in the constructor initialization itself I am telling that you will get the data from this load orders function so two things you need to do when you want to use the Lazy class the first thing is going to find you know the lazy generic object you know for the object which you want to lazy load and second define you know from where he will get the data from okay now what will happen is you know when any call is made to this order's object right any time at any moment of the place he will actually go and make a call to the load orders okay so till then you know this orders object will not be loaded so if I now go and run my poke code here so if I go and run my code here you can see if you watch very closely you can see that this underscore orders here it's saying that is value created is false this is value created property and actually belongs to the Lazy class so he is saying that the value is not created and if you go and see the value is actually null right but as soon as I go and start looping the for each loop so as soon as I make a call to the form E for the for the Ordos object can see the value is still null here but as soon as I do f10 here you can see now the count is 2 right so this is an automatic way of implementing lazy loading by using the ready-made function or else a there is the ready-made generic class the lazy lazy class now there are advantages and disadvantages of everything what we do in programming right so in the same way lazy loading has its own advantages and has his own disadvantages so first thing is don't take it as a writing on the stone saying that implementing lazy loading always is a good practice ok but said and done yes you know lazy loading does have decent advantages like first thing it minimizes the starter time of your application application consumes less memory why because now the objects are loaded on demand basis rather than you know just loading the objects at the start itself and unnecessarily executing SQL statements are avoided because the objects are loaded on demand now the disadvantage is that the code becomes complicated because we need to do a lot of checks you know if the loading has to be done it has to be done or not right so with every call you know the check is done so there can be a slight decrease in performance so you should use lazy loading when then you know the lazy loading should be used in a venue thing that you're composed objects are heavy so for example let's say that you have a customer object and if you have just four or five order objects you know it does not matter right but let's say that you have thousands of order objects you know then yes you know lazy loading is good why because loading all those thousand objects in one go in the memory you know will will not be a good idea right so on demand basis that would be a good idea rather than loading the objects you know unnecessarily so I hope that you enjoyed this video in this video we were trying to understand what is lazy loading we also saw saw that you know how we can use the lazy generate class given by dotnet framework to automate lazy loading and second we talked about the advantages and disadvantages of lazy loading thank you so much you
Info
Channel: Questpond
Views: 99,092
Rating: 4.82266 out of 5
Keywords: c#, .NET, Visual studio, Lazy Loading, c# interview questions with ansers, Lazy generic from c#, Learn Design Patter, Step By Step Design Pattern
Id: 2SrfdAkwmFo
Channel Id: undefined
Length: 9min 50sec (590 seconds)
Published: Fri Sep 13 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.