Segna il video come già completato  
Se vuoi, sarebbe utile un giudizio su questo video    
In questo video vedremo le nuove interfacce di Visual Studio 2010 IObservable e IObserver che sostanzialmente servono a creare un modello di notifica push. Un modello di notifica push è inteso come un ambito in cui una classe provider, che implementa la prima interfaccia IObservable che abbiamo nominato cioè l'interfaccia IObservable invia delle notifiche a tutte le classi reporter che invece implementano un'interfaccia IObserver e sono in ascolto della classe provider. L'oggetto della notifica è rappresentata da una terza classe che la classe che fornisce le informazioni appunto di notifica. Immaginiamoci l'esempio di un semaforo stradale. Come vediamo in questo diagramma di classe tutte le automobili che si avvicinano ad un incrocio devono continuamente controllare la luce della semaforo per capire ad esempio se possono passare oppure se devono fermarsi allo stop. Quando il semaforo cambia colore tutte le auto che sono inattese sono avvisate del cambio di luce e quindi reagiscono di conseguenza: chi è la luce verde passerà chi la luce rossa invece si fermerà. Quindi questa classe semaforo non è altro che il provider che abbiamo appena citato perché notifica a tutte le automobili che sono i reporter, la modifica del colore della luce. Il colore della luce è l'oggetto, la classe che fornisce le informazioni di notifica ed è proprio luce semaforo. Il valore aggiunto di un modello così strutturato il fatto che l'unico collegamento tra il provider e il reporter il meccanismo di sottoscrizione e conseguentemente il meccanismo di rimozione della sottoscrizione. Quindi a reporter alla sottoscrizione automaticamente rimane in ascolto delle notifiche del provider e per tutto il resto del meccanismo per tutta la logica che c'è sotto nella trasmissione di comunicazione tra provider e reporter è responsabile appunto il primo, cioè il provider, ossia il nostro semaforo. Quando infatti sarà necessario notificare agli ascoltatori una nuova informazione sarà proprio il provider per che s'occuperà di verificare dalla propria lista di reporter in ascolto se le informazioni di notifica sono esatte cioè, fa una validazione preventiva delle informazioni di notifica e poi se effettivamente i sottoscrittori sono in grado di ricevere tali informazioni. Vediamo di implementare il nostro modello di notifica push attraverso un esempio che utilizzi appunto tutte queste classi. Allora, cominciamo dal primo elemento che è la struttura semaforo. La struttura semaforo come abbiamo detto precedentemente rappresenta le informazioni che vengono notificate dal semaforo quindi dal provider a tutte le automobili cioè a tutti negli oggetti che sono in ascolto per cui avremo una semplice struttura chiamata luce semaforo con un membro di tipo stringa che rappresenta il colore del semaforo. Alla modifica di questa luce verrà effettuata notifica a tutte le automobili. Andiamo avanti e vediamo che ci si presenta la classe semaforo che è classe provider. La classe provider implementa l'interfaccia IObservable di luce semaforo cioè, traducendo in italiano, è una classe che è osservata e l'oggetto dell'osservazione e rappresentato dalla luce del semaforo. Questa classe Visual Studio 2010 contiene al suo interno una lista. Questa lista e un nero una lista di osservatori su una lista di oggetti che hanno sottoscritto la notifica degli eventi generati dalla classe semaforo. Per cui questa è la dichiarazione della lista di osservatori di luce semaforo. Il costruttore non fa altro che creare questa nuova lista che abbiamo soltanto dichiarato qui e crearla con il nome osservatori non è stato utilizzato il nome automobili direttamente perché la classe semaforo è una classe che non conosce il tipo di osservatore per cui potenzialmente l'osservatore potrebbe essere anche di una classe diversa rispetto ad automobile d'esempio e passanti oppure camion, passante e così via. Andando avanti troviamo la vera propria implementazione e dell'interfaccia IObservable, cioè il metodo Subscribe(), che accetta come parametro un osservatore di luce semaforo. Questo metodo com'è facile intuire permette la sottoscrizione agli eventi del semaforo di una classe automobile di una classe passante di una classe camion. generalmente di una classe osservatore. Ritorna un oggetto per come possono vedere IDisposable perche questo fa parte del processo di cancellazione dalla sottoscrizione sinceramente di questo metodo consiste nell'andare a verificare che la nostra lista osservatori cioè la lista interna di tutti gli osservatori gli eventi del semaforo contiene o meno l'osservatore che deve essere aggiunto. Se esiste già allora si prosegue altrimenti l'osservatore verrà aggiunto alla lista. Il metodo cambia colore accetta un oggetto di tipo luce semaforo quando andiamo manualmente richiamare questo cambio colore passando una nuova luce semaforo viene eseguito un loop tra tutti negli osservatori e se il viene passato un valore non nullo come luce di semaforo, allora l'osservatore viene notificato della modifica della del colore del della luce del semaforo. In caso contrario viene invece generata un'eccezione direttamente nella classe reporter infatti scatenato nel primo caso il metodo OnNext che come vediamo dall'help, fornisce all'osservatore nuove informazioni mentre nel secondo caso viene scatenato il metodo Onerror di notifica invece della presenza di un'anomalia abbiamo poi per chiudere il della classe semaforo il metodo InTrasmission che rappresenta la fine della trasmissione tra il semaforo e tutti gli osservatori per cui viene chiamato per ogni osservatore il metodo OnCompleted e la lista di osservatori viene quindi ripulita. I metodi che sono implementati all'interno di questa classe non sono altro che quelli che abbiamo visto nella classe semaforo c'è stato Subscribe, OnError, OnNext e OnCompleted vediamo infatti ritornando nella classe e semaforo vediamo OnCompleted, vediamo OnNext, il metodo OnError, OnCompleted. Per cui ritornando in basso possiamo vedere che il metodo Subscribe per non fa altro che effettuare la sottoscrizione al provider per che implementa l'interfaccia IObservable. Questo aggiungerà nella lista degli oggetti osservatori dell'era del semaforo questo oggetto automobile. Il metodo OnError è la notifica come abbiamo visto in precedenza di un'anomalia. il metodo OnNext si verifica invece quando viene rilevato un cambio di colore della luce del semaforo mentre OnCompleted è la notifica della fine delle trasmissioni dalla classe provider alla classe reporter. Per cui vediamo adesso nel metodo Main e vediamo di far funzionare questo codice. Nel metodo Main non facciamo altro che creare un nuovo semaforo e creare una serie di a auto ferme al semaforo. Abbiamo l'auto uno che è questo tipo, e che sottoscrive le notifiche degli eventi del semaforo. Abbiamo auto due che anch'essa sottoscrive alle modifiche della classe semaforo. Abbiamo infine auto tre che è di quest'altro tipo e anch'essa sottoscrive gli eventi di semaforo. Questi tre sono i nostri eventi Visual Studio 2010 auto generato cioè richiamiamo il metodo cambia colore della classe semaforo fornendo una luce del semaforo a nostra scelta. Il primo saràgiallo il secondo sarà invece una luce rossa e la terza sarà una luce verde. Per cui lo scopo di questi esempi è che, al cambio del colore del del semaforo, tutte tre le a auto che sono in ascolto di queste modifiche ricevano correttamente la notifica e quindi reagiscono di conseguenza. Possiamo fare una prova perché come vediamo qua sotto quando il colore cambia ad esempio il colore del semaforo diventa giallo allora si dovrebbe scatenare l'evento OnNext, per cui per ogni automobile dovrebbe comparire a video questa questo messaggio la luce del semaforo diventata gialla in questo caso, con il nome dell'auto che ha ricevuto la notifica. Adesso proseguiamo vediamo quest'altro elemento che l'eccezione che viene scatenata il momento in cui viene passato un colore invalido relativo alla luce del semaforo. Passando oltre troviamo finalmente la classe automobile che è classe che implementa l'interfaccia IObserver l'osservatore del semaforo o meglio l'osservatore della luce del colore della luce del semaforo in questa classe ha una proprietà nome e un costruttore che accetta come parametro appunto il nome del là dell'oggetto. Vediamo se siamo stati bravi e se riusciamo quindi a far girare questo codice, questo progetto e smettiamo di punti di interruzione nei punti strategici del metodo Main ie lanciamo quindi progetto. Siamo arrivati in questo punto nel punto dove il colore del semaforo è cambiato di diventato giallo. Quello che vogliamo è che tutte le auto, tutti gli oggetti che sono in ascolto della modifica del cambio di colore, abbiano ricevuto la notifica appunto della cambio di colore del semaforo. Verifichiamo ed è proprio così cioè tutti tre i modelli hanno riconosciuto che il colore del semaforo è diventato giallo. Andiamo avanti premendo un tasto. Siamo arrivati a questo punto qua: la luce del semaforo è diventata rossa vediamo se tutte tre le automobili hanno riconosciuto questo cambio di luce ed è proprio così tutte tre le automobili hanno riconosciuto che colore adesso è rosso. Se andiamo ancora avanti il semaforo è diventato verde, tutti e tre i modelli hanno riconosciuto che la luce è verde andiamo avanti ancora. Siano arrivati qua cioè alla fine delle trasmissioni tutte tre modelli di auto dovrebbero di conoscere che sono state interrotte le trasmissioni dalla classe provider a tutti gli osservatori effettivamente tutte tre i modelli hanno riconosciuto che le trasmissioni sono state concluse. Per completezza potremmo anche aggiungere un altro caso cioè il caso dell'errore cioè quando viene è passato al metodo cambia colore un valore nullo. Proviamo a lasciare soltanto questo codice e vediamo come si comportano le classi in ascolto. Ecco qui: effettivamente tutti gli oggetti in ascolto del semaforo hanno riconosciuto che c'è stata un'anomalia nella trasmissione e non riescono a riconoscere la luce del semaforo. Infine se vogliamo approfondire le interfacce iObservable e IObserver, non ci resta altro che andare sul sito MSDN nella versione di Visual Studio 2010 e visitare questo indirizzo, dove possiamo vedere la versione in inglese della documentazione relativa alla classe IObservable. Troviamo comunque in fondo anche riferimento alla classe IObserer per cui ci possiamo documentare e approfondire a nostro piacimento l'argomento.
In this video we will see the new interface of Visual Studio 2010 and IObservable IObserver that they are designed to create a model for push notification. A model of push notification is intended as an area where a provider class that implements the first interface that we have appointed IObservable IObservable interface that sends notifications to all classes implement an interface instead reporter IObserver and listen to the provider class. The purpose of notification is represented by a third class that the class that provides information just for notification. Imagine the example of a road traffic lights. As we see in this class diagram all cars approaching an intersection must continuously monitor the light from the light to understand, for example if they can go or whether they should stop at the stop sign. When the light changes color all the cars that are unexpected are advised of the change of light and then react accordingly: those who pass the green light instead of the red light will stop. Then this class is nothing but the light provider because we have just mentioned notification to all the cars that are the reporters, changing the color of light. The color of light is the object, the class that provides the notification information and it is light traffic. The added value of a structured model so that the only connection between the provider and the subscription mechanism of the reporter and thus the mechanism for removal of the subscription. Then a reporter to subscribe automatically listens for notifications of provider and for the rest of the mechanism for all the logic that lies beneath the transmission of communication between provider and reporter is responsible for just the first, that the provider, ie our traffic lights. When it is necessary to notify the listeners will be new information for your provider to verify that s'occuperà from its list of reporters to listen if the information in the notification is accurate that is, is a validation of information prior notice and then if you actually Subscribers are able to receive such information. Let us implement our model of push notification through an example that just uses all of these classes. So let's start from the first element is the structure of traffic lights. The traffic light structure as we have said previously is that the information notified by the traffic lights and then by the provider on all cars that all the objects that are listening that we have a simple structure called traffic light with a member of type string that represents the color the traffic lights. In light of this change will be made to notify all cars. Go ahead and see that we present the traffic class is class provider. The provider class implements the interface IObservable of light that is light, translating in Italian, is a class that was observed and the object of observation is represented by the traffic light. Visual Studio 2010 This class contains an internal list. This is a black list and a list of observers on a list of objects that have subscribed to the event notification generated by the class of traffic lights. So this is the declaration of list of observers of light traffic. The manufacturer does is create this new list that we have just stated here and create it as observers has not been used as car traffic directly because the class is a class that does not know the type of observer that the observer could potentially also be of a different class than the example and through car or truck through, and so on. Going forward we find the true IObservable interface and its implementation, namely that of the Subscribe (), which takes as a parameter an observer of light traffic. This method allows easy to see how the signature events of the semaphore of a class car of a class through a class of truck. generally of a class observer. Return an item and how they can see IDisposable because this is part of the process of cancellation of the signing of this method is sincere in going to check our list Observers say the internal list of all observers of the events or not the traffic light contains' observer to be added. If you already have then continues otherwise the viewer will be added to the list. The color change method accepts an object of type light traffic light when we manually call this color change through a new traffic light runs in a loop between all observers and if you pass a non-zero value of traffic light, then the observer is notified of the change of the color of the traffic light. Otherwise is instead thrown directly into the class reporter in fact triggered in the first case we see that as the method OnNext dall'help, provides new information to the observer while the latter is triggered the OnError method of notification rather than the presence of a 'fault then we have to close the traffic light method InTrasmission class that represents the end of the transmission between the lights and all observers that the observer is called for each method OnCompleted and the list of observers is then cleaned. The methods that are implemented within this class are nothing more than what we saw in class, there was traffic lights Subscribe, OnError, OnNext OnCompleted and we see it returning to the classroom and see OnCompleted traffic light, we see OnNext, the OnError method, OnCompleted. So back down we can see that the method does is Subscribe to subscribe to the provider that implements the interface IObservable. This will add to the list of items covered by this observer of the era of automobile traffic. The OnError method is the notification as we have seen above an anomaly. method OnNext not occur when it detects a change in color of the traffic light while OnCompleted is the notification of the end of the transmissions from the provider class to class reporter. So we see now in the Main method, and let's run this code. In the Main method we are doing is creating a new traffic light and create a series of cars waiting at traffic lights. We have a car that is like this, and subscribing to event notifications of traffic lights. We have two cars, which also endorses the amendments to the class of traffic lights. Finally, we have three cars that are of that type and also makes the events of traffic lights. These three events are our Visual Studio 2010 auto-generated recall that the method changes the color of the class by providing a traffic light traffic light at our option. The first saràgiallo the second will be instead a red light and the third will be a green light. So the purpose of these examples is that, to change the color of the light, all three cars that have to listen to these changes properly receive the notification and then react accordingly. We can do a test because, as we see below when the color changes such as the color of the light turns yellow then you should trigger the event OnNext, so for every car should appear in this video, this message became the traffic light yellow in this case, with the name of the car has received the notification. Now we go see this other element that triggered the exception that is the moment when you pass an invalid color on the traffic light. Turning over we finally find the car that class is the class that implements the interface IObserver the observer of light, or rather the observer of the light color of the traffic light in this class has a property name and a constructor that takes as a parameter in fact the name of the object there. Let's see if we were good and if we then run this code, this project and stop breakpoints at strategic points of the Main method and then i launch the project. We arrived here at the point where the color of light has changed to become yellow. What we want is that all cars, all objects that are listening to the change in the color change, just as they receive notification of the change of color of traffic lights. We check and it is so that all three models have recognized that the color of traffic lights and turn yellow. Let us go forward by pressing a button. We arrived at this point here: the traffic light turned red when we see all three cars have recognized this change of light and that is how all three cars have now recognized that color is red. If we further continue the light has turned green, all three models have recognized that the light is green we go again. That have arrived here at the end of the transmissions of all three models of cars should know that the broadcasts were interrupted by the provider class to all observers actually all three models have recognized that the programs have been closed. For completeness, we could add another case that is the case, ie when the error is passed to change the color null. Let's just leave this code and see how they behave in class listening. Here, you actually listen to all the objects in the traffic lights have recognized that there was a fault in the transmission and fail to recognize the traffic light. Finally, if we want to deepen and IObserver iObservable interfaces, there is nothing left to go to the MSDN version of Visual Studio 2010 and visit this address where we can see the English version of the class documentation IObservable. We find, however, also refer to the bottom class IObserer so we can document and investigate the topic at will.