Skip to content

Instantly share code, notes, and snippets.

@jholt456
Created April 2, 2012 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jholt456/2287065 to your computer and use it in GitHub Desktop.
Save jholt456/2287065 to your computer and use it in GitHub Desktop.
OpenAccess ORM Xml To Object Converter
using (var context = new MyContext())
{
var userSettings = new UserSettings();
userSettings.WindowHeight = 100;
userSettings.WindowWidth = 400;
userSettings.WindowX = 20;
userSettings.WindowY = 25;
var newObject= new MyClass();
newObject.Settings = userSettings;
context.Add(newObject);
context.SaveChanges();
}
public class MyMetadataSource : FluentMetadataSource
{
protected override Telerik.OpenAccess.Metadata.MetadataContainer CreateModel()
{
var container = base.CreateModel();
container.NameGenerator.UseDefaultMapping = true;
return container;
}
protected override IList<MappingConfiguration> PrepareMapping()
{
List<MappingConfiguration> list = new List<MappingConfiguration>();
MappingConfiguration<MyClass> config = new MappingConfiguration<MyClass>();
config.MapType(x => new
{
Id = x.ID,
Settings = x.Settings
})
.ToTable("MyClasses");
//Wire up the OA mapping to use the new XML converter to store and retrieve USerSettings from the database.
config.HasProperty(x => x.Settings)
.HasColumnType("xml") //The DB column will be XML
.WithConverter<XmlSerializedObjectConverter<UserSettings>>(); //Tell OA to use the new type converter
config.HasProperty(x => x.ID)
.IsIdentity(Telerik.OpenAccess.Metadata.KeyGenerator.Autoinc);
list.Add(config);
return list;
}
}
public class MyClass
{
public int ID { get; set; }
//This will be stored as XML in the database
public UserSettings Settings { get; set; }
}
[Serializable]
public class UserSettings
{
private int windowWidth;
private int windowHeight;
private int windowY;
private int windowX;
public override string ToString()
{
return string.Format("windowWidth: {0}, windowHeight: {1}, windowY: {2}, windowX: {3}, WindowHeight: {4}, WindowWidth: {5}, WindowX: {6}, WindowY: {7}", windowWidth, windowHeight, windowY, windowX, WindowHeight, WindowWidth, WindowX, WindowY);
}
/// <summary>
/// Gets or sets the height of the window.
/// </summary>
/// <value>The height of the window.</value>
public int WindowHeight
{
get
{
return windowHeight;
}
set
{
windowHeight = value;
}
}
/// <summary>
/// Gets or sets the width of the window.
/// </summary>
/// <value>The width of the window.</value>
public int WindowWidth
{
get
{
return windowWidth;
}
set
{
windowWidth = value;
}
}
/// <summary>
/// Gets or sets the window X.
/// </summary>
/// <value>The window X.</value>
public int WindowX
{
get
{
return windowX;
}
set
{
windowX = value;
}
}
/// <summary>
/// Gets or sets the window Y.
/// </summary>
/// <value>The window Y.</value>
public int WindowY
{
get
{
return windowY;
}
set
{
windowY = value;
}
}
public class XmlSerializedObjectConverter<TSerializableEntity> : AdoTypeConverter where TSerializableEntity : class
{
public override Type DefaultType
{
get
{
return typeof(TSerializableEntity);
}
}
public override bool CreateLiteralSql(ref DataHolder holder)
{
if (holder.NoValue)
{
holder.StringValue = "NULL";
return false;
}
else
{
return true; // inidicating that ' are needed around, because it is a character column (VARCHAR)
}
}
public override AdoTypeConverter Initialize(IDataColumn user, Type clr, IAdoTypeConverterRegistry registry, bool secondaryTable)
{
//Make sure this type converter can work with the specified CLR type
if (!DefaultType.IsSerializable)
throw new System.ArgumentException(
typeof(TSerializableEntity).Name +
" is not serializable. Please add the serializable attribute");
if (clr == typeof(TSerializableEntity))
{
return base.Initialize(user, clr, registry, secondaryTable);
}
return null;
}
public override object Read(ref DataHolder holder)
{
bool isNull = holder.Reader.IsDBNull(holder.Position);
holder.NoValue = isNull;
if (isNull)
{
holder.ObjectValue = null;
}
else
{
//rehydrate the object
string value = holder.Reader.GetValue(holder.Position).ToString();
holder.ObjectValue = DeserializeObject<TSerializableEntity>(value);
}
return (holder.Box) ? holder.ObjectValue : null;
}
public override void Write(ref DataHolder holder)
{
holder.Parameter.DbType = System.Data.DbType.String;
if (holder.NoValue)
{
holder.Parameter.Value = null;
}
else
{
//serialize the object to a string, and set the DataHolder's properties
string s = SerializeObject((TSerializableEntity)holder.ObjectValue);
holder.Parameter.Size = s.Length;
holder.Parameter.Value = s;
}
}
//This is a simple method to deserialize an xml string to a clr object. This could be moved to a utility class
private static T DeserializeObject<T>(string xml)
{
StringReader reader = new StringReader(xml);
XmlSerializer xs = new XmlSerializer(typeof(T));
return (T)xs.Deserialize(reader);
}
//This is a simple method to serialize a class to an xml string.
private static string SerializeObject<T>(T obj)
{
try
{
StringWriter outStream = new StringWriter();
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(outStream, obj);
return outStream.ToString();
}
catch
{
return string.Empty;
}
}
}
public class XmlSerializedObjectConverter<TSerializableEntity> : AdoTypeConverter where TSerializableEntity : class
{
public override Type DefaultType
{
get
{
return typeof(TSerializableEntity);
}
}
public override bool CreateLiteralSql(ref DataHolder holder)
{
if (holder.NoValue)
{
holder.StringValue = "NULL";
return false;
}
else
{
return true; // inidicating that ' are needed around, because it is a character column (VARCHAR)
}
}
public override AdoTypeConverter Initialize(IDataColumn user, Type clr, IAdoTypeConverterRegistry registry, bool secondaryTable)
{
//Make sure this type converter can work with the specified CLR type
if (!DefaultType.IsSerializable)
throw new System.ArgumentException(
typeof(TSerializableEntity).Name +
" is not serializable. Please add the serializable attribute");
if (clr == typeof(TSerializableEntity))
{
return base.Initialize(user, clr, registry, secondaryTable);
}
return null;
}
public override object Read(ref DataHolder holder)
{
bool isNull = holder.Reader.IsDBNull(holder.Position);
holder.NoValue = isNull;
if (isNull)
{
holder.ObjectValue = null;
}
else
{
//rehydrate the object
string value = holder.Reader.GetValue(holder.Position).ToString();
holder.ObjectValue = DeserializeObject<TSerializableEntity>(value);
}
return (holder.Box) ? holder.ObjectValue : null;
}
public override void Write(ref DataHolder holder)
{
holder.Parameter.DbType = System.Data.DbType.String;
if (holder.NoValue)
{
holder.Parameter.Value = null;
}
else
{
//serialize the object to a string, and set the DataHolder's properties
string s = SerializeObject((TSerializableEntity)holder.ObjectValue);
holder.Parameter.Size = s.Length;
holder.Parameter.Value = s;
}
}
//This is a simple method to deserialize an xml string to a clr object. This could be moved to a utility class
private static T DeserializeObject<T>(string xml)
{
StringReader reader = new StringReader(xml);
XmlSerializer xs = new XmlSerializer(typeof(T));
return (T)xs.Deserialize(reader);
}
//This is a simple method to serialize a class to an xml string.
private static string SerializeObject<T>(T obj)
{
try
{
StringWriter outStream = new StringWriter();
XmlSerializer xs = new XmlSerializer(typeof(T));
xs.Serialize(outStream, obj);
return outStream.ToString();
}
catch
{
return string.Empty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment