Krzysztof Radzimski

Pasja tworzenia

Tworzenie konwerterów XPO

13 styczeń
Opublikował Krzysztof Radzimski 13. stycznia 2009 15:40

Z całą pewnością tworzenie obiektów za pomocą standardowych typów bazodanowych w programowaniu ORM, może być nużące. W standardowym polu varchar(x) można przechować najróżniejsze dane i niekoniecznie wykorzystywać do tego szczególne typy danych danego środowiska bazodanowego. Większość ORM nie wykorzystuje zbyt wielu szczególnych funkcji baz danych, co nikogo nie dziwi.

Nic nie stoi na przeszkodzie, aby programowo zaimplementować mapowanie prostych typów danych na złożone obiekty np. XElement lub XDocument. Poniżej przedstawiam przykład utworzenia takiego konwertera oraz przykład jego implementacji.

Rozpocznijmy od utworzenia konwertera. Przyjmijmy, że chcemy w polu tekstowym (np. varchar(max) lub odpowiednim dla innego niż MSSQL typu) zachować dokument XML. Utwórzmy klasę XElementConverter. Klasa ta musi dziedziczyć po DevExpress.Xpo.Metadata.ValueConverter.

/// <summary>
///     Klasa <see cref="XElementConverter"/> pełni funkcję konwertera wartości 
///     tekstowej na obiekt klasy <see cref="XElement"/>.
/// </summary>
public sealed class XElementConverter : ValueConverter
{
}


Należy przeciążyć trzy metody:

  • ConvertToStorageType – konwertuje obiekt XElement na String;
  • ConvertFromStorageType – konwertuje String na XElement;
  • StorageType – wskazuje jawnie typ danych pola bazodanowego.

Poniżej kod całej klasy XElementConverter.

using System;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using DevExpress.Xpo.Metadata;


/// <summary>
///   
Klasa<see cref="XElementConverter"/>pełni funkcję konwertera
///    wartości tekstowej na obiekt klasy<see cref="XElement"/>.
/// </summary>
public sealed class XElementConverter : ValueConverter
{
   public override object ConvertToStorageType(object value)
     {
         if (value == null)
         {
             return null;
         }
         else
       
{
             try
           
{
                 XElement xe = value asXElement;
                 if (xe != null)
                     returnxe.ToString();
                 else
                     return
String.Empty;
             }
             catch (Exception)
             {
                 returnString.Empty;
             }
         }
     }

   
public override object ConvertFromStorageType(object value)
     {
         if (value == null)
         {
             return null;
         }
         else
       
{
             try
           
{
                 returnXElement.Parse(value.ToString());
             }
             catch (Exception)
             {
                 return null;
             }
         }
     }

   
public override Type StorageType
     {
         get { return typeof(string); }
     }
}

Gdy nasz konwerter jest gotowy, można przystąpić do jego użycia w klasie XPO.

[DisplayName("Dokument XML")]
[Size(SizeAttribute.Unlimited)]
[Persistent("XMLDOC")]
[ValueConverter(typeof(XElementConverter))]
public System.Xml.Linq.XElement XmlData
{
    get { return GetPropertyValue<System.Xml.Linq.XElement>("XmlData"); }
    set { SetPropertyValue("XmlData", value); }
}


Przykładowe użycie jest chyba oczywiste:

Document doc = new Document(Session.DefaultSession);
doc.XmlData = XElement.Load(filename);

Tagi: | Kategorie: Programowanie | DevExpress

Wyślij link na adres e-mail | Link do tego postu | RSSRSS comment feed