client/src/Iri.Modernisation.Data/Models/Loader.cs
changeset 35 43bb1b8ed555
parent 34 4d9ebc6fbbe8
child 36 b6df6fce6e5d
equal deleted inserted replaced
34:4d9ebc6fbbe8 35:43bb1b8ed555
    12 using System.IO;
    12 using System.IO;
    13 using System.Collections.Generic;
    13 using System.Collections.Generic;
    14 using System.Threading;
    14 using System.Threading;
    15 namespace Iri.Modernisation.Data.Models
    15 namespace Iri.Modernisation.Data.Models
    16 {
    16 {
       
    17 
       
    18     public interface ILoader
       
    19     {
       
    20         event EventHandler<LoaderEventArgs<ILoader>> LoaderFinished;
       
    21     }
       
    22     
    17     /// <summary>
    23     /// <summary>
    18     /// Classe permettant de charger un XML et de convertir son contenu en un type ReturnType désiré
    24     /// Classe permettant de charger un XML et de convertir son contenu en un type ReturnType désiré
    19     /// </summary>
    25     /// </summary>
    20     /// <typeparam name="ReturnType">Type Désiré</typeparam>
    26     /// <typeparam name="ReturnType">Type Désiré</typeparam>
    21     public class Loader<ResultType>
    27     public class Loader<ResultType> 
    22     {
    28     {
    23 
    29 
    24         public String Path { get; private set; }
    30         private AutoResetEvent Are { get; set; }
       
    31 
       
    32         public String Path { get; set; }
    25         /// <summary>
    33         /// <summary>
    26         /// WebClient qui permet de charger le XML distant
    34         /// WebClient qui permet de charger le XML distant
    27         /// </summary>
    35         /// </summary>
    28         public  WebClient xmlClient {get; private set;}
    36         public  WebClient xmlClient {get; private set;}
    29         /// <summary>
    37         /// <summary>
    43 
    51 
    44         /// <summary>
    52         /// <summary>
    45         /// Téléchargement des données 
    53         /// Téléchargement des données 
    46         /// </summary>
    54         /// </summary>
    47         /// <param name="path">Chemin du fichier</param>
    55         /// <param name="path">Chemin du fichier</param>
    48         public  void Load(String path)
    56         public void Load(String path)
    49         {
    57         {
    50             Path = path;
    58             //Here it's working
       
    59             // Are.Set();
       
    60             this.Path = path;
    51             xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted);
    61             xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted);
       
    62         
    52             xmlClient.DownloadStringAsync(new Uri(path,UriKind.RelativeOrAbsolute));
    63             xmlClient.DownloadStringAsync(new Uri(path,UriKind.RelativeOrAbsolute));
    53        
    64            
       
    65            
       
    66             //Here not
       
    67             //Are.Set();
       
    68           
       
    69           
    54         }
    70         }
    55        
    71        
    56         /// <summary>
    72         /// <summary>
    57         /// Une fois le fichier télécharger, on commence le transtypage
    73         /// Une fois le fichier télécharger, on commence le transtypage
    58         /// </summary>
    74         /// </summary>
    59         /// <param name="sender"></param>
    75         /// <param name="sender"></param>
    60         /// <param name="e"></param>
    76         /// <param name="e"></param>
    61         void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    77         private void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    62         {
    78         {
       
    79          
    63             //On utilise la fonction donnée lors de la construction du Loader pour créer l'objet grâce au fichier XML
    80             //On utilise la fonction donnée lors de la construction du Loader pour créer l'objet grâce au fichier XML
    64             ResultType result = ProcessFunction.Invoke(XDocument.Parse(e.Result));
    81             ResultType result = ProcessFunction.Invoke(XDocument.Parse(e.Result));
    65             if (LoaderFinished != null)
    82             if (LoaderFinished != null)
    66             {
    83             {
    67                 //Une fois le Transtypage Fini, on lance l'évenement indiquant que le chargement est fini et donnant
    84                 //Une fois le Transtypage Fini, on lance l'évenement indiquant que le chargement est fini et donnant
    68                 // en argument l'objet créé.
    85                 // en argument l'objet créé.
       
    86 
    69                 LoaderFinished(this, new LoaderEventArgs<ResultType>(result));
    87                 LoaderFinished(this, new LoaderEventArgs<ResultType>(result));
       
    88              
       
    89                
    70             }
    90             }
    71         }
    91         }
    72 
    92         public void  Execute(Object sender)
       
    93         {
       
    94             Are = (AutoResetEvent)sender;
       
    95             Load(this.Path);
       
    96          
       
    97         }
    73         public event EventHandler<LoaderEventArgs<ResultType>> LoaderFinished;
    98         public event EventHandler<LoaderEventArgs<ResultType>> LoaderFinished;
    74       
    99       
    75        
   100        
    76     }
   101     }
    77     public class LoaderEventArgs<ResultType> : EventArgs
   102     public class LoaderEventArgs<ResultType> :  EventArgs
    78     {
   103     {
    79         public ResultType CreatedObject { get; private set; }
   104         public ResultType CreatedObject { get; private set; }
       
   105         public AutoResetEvent Are { get; private set; }
    80         public LoaderEventArgs(ResultType createdObject)
   106         public LoaderEventArgs(ResultType createdObject)
    81         {
   107         {
    82             CreatedObject = createdObject;
   108             CreatedObject = createdObject;
       
   109             Are = null;
       
   110         }
       
   111         public LoaderEventArgs(ResultType createdObject,AutoResetEvent are)
       
   112         {
       
   113             CreatedObject = createdObject;
       
   114             Are = are;
       
   115 
    83         }
   116         }
    84     }
   117     }
       
   118 
       
   119  
    85     
   120     
    86 }
   121 }