In this video we will see how Visual Studio 2010 allows us to use lazy loading to load objects that are particularly heavy so it would be appropriate to recall the flight at a moment when that will actually be used because lazy loading is a common pattern in the whole world of programming allows you to postpone the loading of a very heavy object such as a class until they actually will be necessary to use that object. The opposite of lazy loading is eager loading the tendency to anticipate the tools that will surely then in the yard of our application and then immediately used the objects a little further on, or much later in our code is available in several forms initialization delayed so we could put into the Italian term for lazy loading. The most widely used form of initialization is to declare an object and define immediately setting it to null. Later, when then arise in the course of our code is to enhance the efficiency of the concrete object it will then permanently assigning a value to the real value that should have. And then actually create the object of which we have so far delayed loading. We give an example of initializing proxies in Visual Studio 2010 with a simple string we just add a new project of type Console Application call it call it Lazy Loading with a string and create our very heavy object as a string and give the value zero . At this point, we create our own method in a Visual Studio 2010 that will enhance the object by assigning real value. We assign the real value and we call the Main method of our of our class. Here is that this is an example of lazy loading using a simple string. Of course this technique of delayed initialization can also be applied to areas definitely more structured than what we have just seen the use of a string that is in fact the example that has already been prepared here in this solution for each project and we see that For example, we have the class make a bicycle with another property list of bikes inside the class constructor that defines the brand then the method order bike this method allows you to check if the bicycle of a certain brand is already present in the warehouse or they must be recorded and then added to the list that we just saw. The purpose of this example is that of this year and to verify that indeed the point at which the object is created bicycle and delayed compared to the object that initialization of the program cycle is created when actually be used and must and thus be enhanced. Let's follow the flow of the program we put together a point in the first position nterruzione the purpose of these lines and that from that position to return the bicycle within this list if not already exist, otherwise it will be inserted just we start debugging on the list and we were hooked up to our code. Press F11 to continue. Checks that the list of bikes is the bike that we took this into consideration that the Bottecchia this, let's go ahead with F11 we can see that actually the bike is not listed in that creates a moment and then this is the effect of lazy loading the bike that is created only when they actually need to create it, let's go ahead and then added to the list. Finally, returning the index of the complete list of bicycles, bicycle that took him into consideration. The same goes for the next cycle it checks the list and if it is present, then it is returned as in this case the index is created otherwise the bicycle to the bicycle route and returns the index of the list. The purpose of this debugging is to understand what the delayed initialization or lazy loading. The. Net Framework 4 Visual Studio 2010 introduces the Lazy class that is just a wrapper class for the object we want to delay the initialization, and then we can handle all of the mechanisms by which the delay is realized initialization.
So for example to create our wrapper we can write such a thing in our Lazy object the object to create new Lazy object when we write this line of code is insulted and initialized object is not valued. Only later when we're going to write that object to create value when we examine the properties of the instantiated object behind our only then will the real object initialization. We then also an important property that is IsValueCreated, which allows us to determine if the value is ready for use so it is ready to be read or still can not be questioned.
We see a real use of the class of Lazy. Net Framework 4 Visual Studio 2010 we have the draft ready this line of code is the one we saw in the previous example that this here, which is not yet initialized the object is enhanced if is still called the bicycle manufacturer in our class we can see below. This class is the bicycle. So in this first line of code will not be instantiated object our bike only when you access the value property of bike then only at the time of the call will be made Sub New () constructor of our class. We can make a practical test by putting a breakpoint on the first line on the manufacturer and see if the manufacturer is called from this line or on the next line. We start debugging and here we are in first place at this stage we press F11 to go to the next line and see what we actually passed by bicycle manufacturer. From the builder bike when we can only access the property value. Here you go. This esmepio developed in Visual Studio 2010 is valuable to see if the manufacturer of our class is heavy with no parameters so we have called the Lazy example with no parameters, however, to have greater control over the constructor of the class where you can also use the constructor with parameters we must use another constructor of the class that uses Lazy as a parameter a delegate that points to the constructor of our class heavy, this time using the parameters for which small changes to the code of this code agiungiamo Lazy with parameters pointing to our manufacturer was of our class with the two parameters that in this case we can perform a test to see if indeed the constructor is called by this function or when they access the property value. We are still on the previous project. We assign ownership of Startup Project to go forward with this new project F11. The manufacturer has not been called and this time instead of the constructor is invoked.