Invest in people before investing in tools

Il blog di Matteo Baglini
posts - 118, comments - 95, trackbacks - 697

[Spring.NET #15] Spring.Core, Type Conversion

Il .NET Framework per convertire un oggetto da un tipo ad un'altro utilizza i TypeConverter. Lo stesso sistema viene utilizzato da Spring.NET per convertire i valori stringa inseriti nel file di configurazione in tipi concreti. Il framework Spring.NET fornisce già un'insieme di custom TypeConverter (a questa pagina potete trovare l'elenco completo) come il FileInfoConvert che serve a convertire una stringa in una istanza di System.IO.FileInfo. Se abbiamo la necessità possiamo creare un nostro custom TypeConverter, per maggiori informazioni leggete questo articolo How to: Implement a Type Converter. Come esempio modifichiamo la classe Person vista in alcuni post precedenti aggiungendo un riferimento ad una nuova calsse AddressInfo:

   1: public class Person
   2: {
   3:     private string _firstName;
   4:     private short _age;
   5:     private AddressInfo _addressInfo;
   6:  
   7:     public AddressInfo AddressInfo
   8:     {
   9:         get { return _addressInfo; }
  10:         set { _addressInfo = value; }
  11:     }
  12:  
  13:     public string FirstName
  14:     {
  15:         get { return _firstName; }
  16:         set { _firstName = value; }
  17:     }
  18:  
  19:     public short Age
  20:     {
  21:         get { return _age; }
  22:         set { _age = value; }
  23:     }
  24:  
  25:     public Person()
  26:     {
  27:         _addressInfo = new AddressInfo();
  28:     }
  29: }
   1: public class AddressInfo
   2: {
   3:     private string _address = string.Empty;
   4:  
   5:     public string Address
   6:     {
   7:         get { return _address; }
   8:         set { _address = value; }
   9:     }
  10:     private string _postalCode = string.Empty;
  11:  
  12:     public string PostalCode
  13:     {
  14:         get { return _postalCode; }
  15:         set { _postalCode = value; }
  16:     }
  17:     private string _city = string.Empty;
  18:  
  19:     public string City
  20:     {
  21:         get { return _city; }
  22:         set { _city = value; }
  23:     }
  24:  
  25:     public AddressInfo()
  26:     {}
  27:  
  28:     public AddressInfo(string address, string postalCode, string city)
  29:     {
  30:         _address = address;
  31:         _postalCode = postalCode;
  32:         _city = city;
  33:     }
  34: }

adesso definiamo i valori separati dalla virgola per la nuova propietà dell'oggetto Person:

<object id="Matteo" 
        type="SpringSeries.Core.CustomTypeConverter.Person, 13.CustomTypeConverter">        
  <property name="FirstName" value="Matteo" />
  <property name="Age" value="24" />
  <property name="AddressInfo" value="Mio Indirizzo,57100,Livorno" />
</object>

qui entra in gioco il TypeConverter che permetterà di trasformare la stringa in un'istanza di AddressInfo con tutte le property settate, quindi procediamo creando un Custom TypeConverter:

   1: public class AddressInfoConverter : TypeConverter
   2: {
   3:     public override bool CanConvertFrom(ITypeDescriptorContext context,
   4:                                         Type sourceType)
   5:     {
   6:         if (sourceType == typeof(string))
   7:         {
   8:             return true;
   9:         }
  10:         return base.CanConvertFrom(context, sourceType);
  11:     }
  12:  
  13:     public override object ConvertFrom(ITypeDescriptorContext context,
  14:                                         CultureInfo culture, object value)
  15:     {
  16:         if (value is string)
  17:         {
  18:             string[] v = ((string)value).Split(new char[] { ',' });
  19:             return new AddressInfo(v[0], v[1], v[2]);
  20:         }
  21:         return base.ConvertFrom(context, culture, value);
  22:     }
  23: }

in fine registriamo quest'ultimo nel file di configurazione:

<object id="customConverterConfigurer"
        type="Spring.Objects.Factory.Config.CustomConverterConfigurer, Spring.Core">
  <property name="CustomConverters">
    <dictionary>
      <entry key="SpringSeries.Core.CustomTypeConverter.AddressInfo">
        <object type="SpringSeries.Core.CustomTypeConverter.AddressInfoConverter"/>
      </entry>
    </dictionary>
  </property>
</object>

non ci resta che avviare l'applicazione di esempio e visualizzare le proprietà di AddressInfo per vedere il risultato:

   1: IApplicationContext _ctx = ContextRegistry.GetContext();
   2:  
   3: Person _person = (Person)_ctx.GetObject("Matteo");
   4: Console.WriteLine(_person.FirstName);
   5: Console.WriteLine(_person.Age);
   6: Console.WriteLine(_person.AddressInfo.Address);
   7: Console.WriteLine(_person.AddressInfo.PostalCode);
   8: Console.WriteLine(_person.AddressInfo.City);
Technorati Tags:

Print | posted on venerdì 5 ottobre 2007 04:43 | Filed Under [ .NET OpenSource Spring.NET ]

Powered by:
Powered By Subtext Powered By ASP.NET