Lazy Loading design pattern


In this video will try to understand what exactly is the concept of Lazy loading Lazy loading is a design pattern or a concept where we delay the loading of the objects until the point where we need it On demand object loading rather than loading the objects unnecessarily We have created a very simple class here called as a customer class and this customer class has lots of order objects In the constructor of the customer class we are loading both the customer data as well as the order data As soon as we create the customer object the customer data will be loaded as well as the order object data will be loaded We have a very simple console application over here and in this console application we have created the customer class Both the customer + the order object data will be loaded If we run this application customer data is loaded as well as the composed order objects are loaded After this line put a debug point here the application currently waiting here As soon as the customer object is loaded both the customer data as well as the composed object that is order objects is also loaded At this moment we probably would like to just load the customer data As soon as the foreach loop starts that means on demand basis you would like to go and load the order objects This is termed as lazy loading As the application demands the class loads is it’s composed objects In order to achieve lazy loading We need to remove this code which actually loads the order objects in the constructor then when the get is done on the order collection, that moment we will make a check here If it is null then only load the orders object When any one does a get on the order collection at that time only the order object will be loaded and not during the construction construction object itself The order collection is null and the customer data is loaded As soon as we start the foreach loop there is a get done on the order collection As soon as the first get is done on the orders collection this object will be loaded Till this two statements we never needed the order collection As soon as there is demand of the order collection as soon as there is a get done As soon as we do a F10 now the count is 2 When the foreach loop starts the orders collection loaded This is one way of implementing lazy loading by doing a check Wherever the object demand is needed just before that do a check and he do loading of the object The logic is working If we put this if check all over the class the class will become complicated We have a very nice class provided by .NET framework called as the lazy class, it’s a generic class lazy and by using a lazy class we can implement lazy loading and you can minimize the complexity of your class To implement the lazy loading define this complete object that is list of order which we want to load lazly by saying lazy of the list of order The next thing is we don’t need this if condition now any more we do need to write a scatered code across the class of this if conditions When the data is loaded inside this lazy object right the actual data is loaded inside value what this lazy order object needs to know is where he will get the orders data In this lazy class we can paas the function The variable name is _order In the constructor initialization itself we will get the data from this load orders function To use the lazy class first thing is define the lazy generic object the object which we want to lazy load and second define from where he will get the data from when any call is made to this orders object any time at any moment of the place he will actually go and make a call to the load orders This orders object will not be loaded If we run the code Is value is created property in actual belong to the lazy class If we see the value it is null As soon as I we start looping the foreach loop, as soon as we make a call for the orders object the value is still null When we do a F10 the count is 2 This is the automatica way of implementing lazy loading by using the readymade function the readymade generic class There are advantages and disadvantages of everything what we do in programming. In the same way lazy loading has is own advantages and has its owned disadvantages Implementing lazy loading alwaysis a good practice lazy loading does have descent advantages like it minimizes the start up time of the application Application consumes less memory because now the objects are loaded on demand basis rather than just loading the objects at the start itself and unnecessarily executing SQL statement are avoided because the objects are loaded in demand The disadvantages that the code becomes complicated we need to do lot of checks it has to be done not With every call the check is done There can be slight decrease in performance We have customer object if we have just four or five order objects is does not matter If we have thousands of orders object then lazy loading is good why because loading all those thousand objects in one go in a memory will not be a good idea On demand basis there would be good idea rather than loading the object In this video we were trying to understand what is lazy loading, saw that how we can use the lazy generic class given by .NET framework to automate lazy loading and and talked about advantages or disadvantages of lazy loading Thank you

21 Comments

Add a Comment

Your email address will not be published. Required fields are marked *