Segna il video come già completato  
Se vuoi, sarebbe utile un giudizio su questo video    
In questo video vedremo come Visual Studio 2010 ci permette di far uso lazy loading per il caricamento di oggetti che sono particolarmente pesanti che quindi sarebbe opportuno richiamare al volo proprio nel momento cioè in cui dovranno essere effettivamente utilizzati infatti il lazy loading è un pattern comune in tutto il mondo della programmazione consente di posticipare il caricamento di un oggetto particolarmente pesante ad esempio una classe fino al momento in cui effettivamente si renderà necessaria l'utilizzo di tale oggetto. L'opposto delle lazy loading è l'eager loading ossia la tendenza ad anticipare gli strumenti che poi serviranno sicuramente nel corso della nostra applicazione quindi il cantiere immediatamente gli oggetti che verranno usati poco più in là oppure molto più avanti nel nostro codice esistono varie forme di inizializzazione ritardata così potremmo tradurre in italiano il termine di lazy loading. La forma di inizializzazione più utilizzata consiste nel dichiarare un oggetto e definire immediatamente settandolo a un valore nullo. Successivamente quando quindi sorgerà nel corso del nostro codice è l'efficienza concreta di valorizzare l'oggetto lo si andrà quindi a valorizzare modo definitivo assegnandogli il valore reale che dovrà avere. E quindi creare realmente l'oggetto di cui finora abbiamo ritardato il caricamento. Possiamo fare un esempio di inizializzazione ritardata in Visual Studio 2010 con una semplice stringa ci basta aggiungere un nuovo progetto di tipo applicazione console lo chiamiamo lo chiamiamo Lazy Loading con una stringa e creiamo il nostro oggetto molto pesante forma di stringa e gli diamo il valore nullo. A questo punto ci creiamo una un nostro metodo in Visual Studio 2010 che valorizzerà nell'oggetto assegnandogli il valore reale. Assegnamo il valore reale e richiamiamo il metodo dalla Main della nostra della nostra classe. Ecco che questo è un esempio di lazy loading utilizzando una semplice stringa. Naturalmente questa tecnica di inizializzazione ritardata può essere applicata anche ad ambiti sicuramente più strutturati di quello che abbiamo appena visto cioe l'utilizzo di una stringa infatti nell'esempio che è già stato preparato qua all'interno di ogni questa soluzione di progetti e vediamo che abbiamo ad esempio la classe bicicletta con un'altra proprietà marca una lista di biciclette interna alla classe il costruttore che definisce la marca infine il metodo ordine bicicletta questo metodo consente di verificare se la bicicletta di una determinata marca è già presente nel magazzino oppure essi deve essere registrata e quindi aggiunta alla lista che abbiamo appena visto. Lo scopo di quest'anno di questo esempio è quello e di verificare che effettivamente il punto in cui viene creato l'oggetto bicicletta e ritardato rispetto al inizializzazione del programma cioè l'oggetto bicicletta viene creato nel momento in cui effettivamente deve essere utilizzato e deve essere e dunque valorizzato. Vediamo di seguire insieme il flusso del programma mettiamo un punto di nterruzione nella prima posizione il lo scopo di queste righe e quella da quello di tornare la posizione della bicicletta all'interno di questa lista nel caso in cui sia già presente,altrimenti verrà inserita appunto nella lista avviamo il debug ed eccoci agganciati al nostro codice. Premiamo F11 per andare avanti. Viene verificato che nella lista delle biciclette sia presente la bicicletta che abbiamo preso in considerazione cioè il questa Bottecchia, andiamo avanti con F11 possiamo vedere che effettivamente la bicicletta non è nella lista per cui ne crea una al momento e quindi questa è l'effetto del lazy loading cioè la bicicletta viene creata soltanto nel momento in cui effettivamente serve crearla, andiamo avanti e quindi viene aggiunta alla lista. Viene infine ritornato l'indice della lista completa di biciclette, della bicicletta che lo prese in considerazione. La stessa cosa avviene per la bicicletta successiva viene verificata la presenza nella lista e se è presente allora viene ritornato come in questo caso il l'indice della bicicletta altrimenti viene creata al volo la bicicletta e viene restituito l'indice della lista. Lo scopo di questo debug è quello di capire cos'è il inizializzazione ritardato o lazy loading. Il .Net Framework 4 di Visual Studio 2010 introduce la classe Lazy che non è altro che una classe wrapper per l'oggetto di cui vogliamo ritardare l'inizializzazione e che quindi riesce gestire per noi tutti i meccanismi con i quali si concretizza il ritardo di inizializzazione dell'oggetto. Per cui ad esempio per creare il nostro wrapper possiamo scrivere una cosa del genere Lazy del nostro oggetto l'oggetto da creare new Lazy dell'oggetto quando scriviamo questa riga di codice viene insultato e inizializzati all'oggetto non viene valorizzato. Soltanto nel momento in cui successivamente andremo a scrivere oggetto da creare value cioè quando andremo interrogare la proprietà quello del nostro oggetto istanziato in ritardo solo in quel momento avverrà l'inizializzazione reale dell'oggetto. Abbiamo poi a disposizione anche un'importante proprietà cioè IsValueCreated, che ci permette di determinare se il valore è pronto per l'uso quindi è pronto per essere letto oppure ancora non può essere interrogato. Vediamo un uso concreto della classe Lazy del .Net Framework 4 di Visual Studio 2010 abbiamo già il progetto pronto questa riga di codice è quella che abbiamo visto nell'esempio precedente cioè questa qua, che viene inizializzato l'oggetto ancora non viene valorizzato se non viene chiamato ancora il costruttore della nostra classe bicicletta che possiamo vedere qua sotto. Questa è classe bicicletta. Quindi in questa la prima riga di codice non verrà istanziato il nostro oggetto bicicletta soltanto nel momento in cui si accederà alla proprietà value di bicicletta allora solo in quel momento verrà effettuato il richiamo della Sub New() del costruttore della nostra classe. Possiamo fare una prova concreta mettendo un punto d'interruzione sulla prima riga e sul costruttore vediamo se il costruttore viene richiamato da questa riga oppure dalla riga successiva. Avviamo il debug ed eccoci in prima posizione a questo punto premiamo F11 per passare alla riga successiva e vediamo che effettivamente non siamo passati dalla costruttore bicicletta. Dal costruttore bicicletta possiamo soltanto nel momento in cui accediamo la proprietà value. Ecco qua. Questo esmepio sviluppato in Visual Studio 2010 è valido vedere se il costruttore della nostra classe pesante è senza parametri infatti abbiamo chiamato l'esempio Lazy senza parametri per avere però maggior controllo sulla costruttore della classe cui poter utilizzare anche dei costruttore con dei parametri dobbiamo utilizzare un altro costruttore della classe Lazy che utilizza come parametro un delegato che punta al costruttore della nostra classe pesante, questa volta utilizzando dei parametri per cui modificando leggermente questo codice agiungiamo al codice di Lazy con parametri che punta al nostro era costruttore della nostra classe con i due parametri per cui anche in questo caso possiamo effettuare una prova per vedere se effettivamente il costruttore viene richiamato da questa funzione oppure nel momento in cui accediamo le proprietà value. Siamo ancora sul progetto precedente. Assegnamo la proprietà di Startup Project a questo nuovo progetto andiamo avanti con F11. Il costruttore non è stato richiamato ed in questo momento invece il costruttore viene richiamato.
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.