Creating an ORM in C# Revisited – Part 1

5/18/2011

This is a bit long but I feel that a bit of backstory is warranted here. For those unaware, a while ago I wrote my own ORM. It wasn't meant to really compete with the likes of nhibernate, etc. It was simply a learning project (what goes into an ORM, etc.). A while ago I started redoing parts and pieces to get better stability and speed (fixed a number of bugs, etc.). Once it got rather stable, I started to use it in some of my projects. At first it worked well, cut down on turn around time, etc. However I kept running into issues (mainly speed related). Eventually, on some of my larger projects, I had to strip out parts and write things by hand... I tried some of the other ORMs (nhibernate, subsonic, etc.) out there and ran into the same issues (although to a lesser extent as they were actually written by individuals that knew what they were doing). One thing was a constant though, no matter what I tried or which library I plugged in the speed issue would raise its ugly head. Eventually though, I ran into Massive.

Massive, written by Rob Conery, is what is called a micro ORM. It's very small (about 364 lines as of this writing) and rather fast (in my experience much faster than the other ORMs when I was testing it out). So back in February/March I start testing it out and instantly ran into a brick wall. It uses expando objects (which is a .Net 4 feature) and I'm stuck on .Net 3.5 for a number of my projects. So I start looking around for an alternative and couldn't find one. Instantly I thought, I know, I'll create my own! I have enough experience writing my first ORM, and since Massive is open source I can just look at that if I get into a corner in the design process. It will be simple! And then Zeus looked down from Olympus and said "Listen here you wanker, I'll show you what happens to people with too much hubris". By the way, I don't know why Zeus is British (maybe he's American but just likes watching a lot of BBC America, I don't know). It doesn't really matter, because the important part of the story is it was at this point that he sent down a lightening bolt and destroyed my laptop... Last laugh was on him as I needed a new one anyway so this just gave me an excuse.

So time passed, I got a new laptop, I loaded it up, etc. However I just, sort of, took a break from programming and just played some games on my 360 (replayed Dragon Age: Origins, played Dragon Age 2 [which was a bit of a disappointment in some ways], some expansions for Mass Effect 2 [which I actually liked more than the original], etc.). It was a rather peaceful break. Eventually though I decided to get going on this project. So I start and instantly find that someone already came out with a micro ORM that can do what I need... Dapper. Dapper, another micro ORM, was written by Sam Saffron and came in at about 1038 lines of code (as of this writing). Supposedly he created it for the same reasons that I had (speed issues), although part of me thinks it was to piss me off (even though I'm certain that he has no idea who I am or about my previous plan to write my own micro ORM since I didn't tell anyone else about it). All I could think at the time was how dare Mr. Saffron write a fantastic library that you should totally try out. So I decided even with his library being out there, I was going to still create my own because 1) I didn't learn anything from my encounter with Zeus and 2) Screw you Sam, screw you.

Unlike my previous series, this is probably going to be shorter for a couple of reasons. But basically it comes down to the fact that there are a number of things that we no longer need with this system (caching, etc.). Instead we just need a simple mapping mechanism (database to object and vice versa) and some way to communicate with the database. That's it, just two things (however I've broken it down into a couple classes, helper functions, etc. to keep things on the smaller side and hopefully simpler to follow). In fact it's so simple that I'm adding this directly to my utility library. Anyway, today I'm just going to talk about the mapping mechanism.

A while back I saw a project called AutoMapper. It seemed a bit of overkill to me at the time, but in this situation it was exactly the type of thing that I needed (basically something that would allow me to automatically copy from one location to another). However since I'm adding this directly to my utility library, I can't add AutoMapper (my number one rule with my utility library is I can't have outside dependencies other than .Net/windows libs. I'm even iffy about adding in any XNA/DirectX stuff). So I set out to recreate my own version of the code (and actually found a very easy way to do it).

Basically an object to object mapper is nothing more than a bunch of gets and sets saved away and then run when called. That's it. So the easiest way that I could think of to do this was to use Funcs and Actions (Func for the getting of the value, Action for the setting of the value). And since I didn't want to force the coder to supply a Func and Action for every property, I needed a way using Expression trees to create the appropriate items on the fly:

   1:  
   2:         /// <summary>
   3:         /// Gets a lambda expression that calls a specific property's getter function
   4:         /// </summary>
   5:         /// <typeparam name="ClassType">Class type</typeparam>
   6:         /// <typeparam name="DataType">Data type expecting</typeparam>
   7:         /// <param name="PropertyName">Property name</param>
   8:         /// <returns>A lambda expression that calls a specific property's getter function</returns>
   9:         public static Expression<Func<ClassType,DataType>> GetPropertyGetter<ClassType,DataType>(string PropertyName)
  10:         {
  11:             if (string.IsNullOrEmpty(PropertyName))
  12:                 throw new ArgumentNullException("PropertyName");
  13:             string[] SplitName = PropertyName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  14:             PropertyInfo Property = Utilities.Reflection.Reflection.GetProperty<ClassType>(SplitName[0]);
  15:             ParameterExpression ObjectInstance = Expression.Parameter(Property.DeclaringType, "x");
  16:             MemberExpression PropertyGet = Expression.Property(ObjectInstance, Property);
  17:             for (int x = 1; x < SplitName.Length; ++x)
  18:             {
  19:                 Property = Utilities.Reflection.Reflection.GetProperty(Property.PropertyType, SplitName[x]);
  20:                 PropertyGet = Expression.Property(PropertyGet, Property);
  21:             }
  22:             if (Property.PropertyType != typeof(DataType))
  23:             {
  24:                 UnaryExpression Convert = Expression.Convert(PropertyGet, typeof(DataType));
  25:                 return Expression.Lambda<Func<ClassType, DataType>>(Convert, ObjectInstance);
  26:             }
  27:             return Expression.Lambda<Func<ClassType, DataType>>(PropertyGet, ObjectInstance);
  28:         }
  29:  
  30:         /// <summary>
  31:         /// Gets a lambda expression that calls a specific property's getter function
  32:         /// </summary>
  33:         /// <typeparam name="ClassType">Class type</typeparam>
  34:         /// <typeparam name="DataType">Data type expecting</typeparam>
  35:         /// <param name="Property">Property</param>
  36:         /// <returns>A lambda expression that calls a specific property's getter function</returns>
  37:         public static Expression<Func<ClassType, DataType>> GetPropertyGetter<ClassType, DataType>(PropertyInfo Property)
  38:         {
  39:             if (!IsOfType(Property.PropertyType, typeof(DataType)))
  40:                 throw new ArgumentException("Property is not of the type specified");
  41:             if (!IsOfType(Property.DeclaringType, typeof(ClassType)))
  42:                 throw new ArgumentException("Property is not from the declaring class type specified");
  43:             ParameterExpression ObjectInstance = Expression.Parameter(Property.DeclaringType, "x");
  44:             MemberExpression PropertyGet = Expression.Property(ObjectInstance, Property);
  45:             if(Property.PropertyType!=typeof(DataType))
  46:             {
  47:                 UnaryExpression Convert=Expression.Convert(PropertyGet,typeof(DataType));
  48:                 return Expression.Lambda<Func<ClassType, DataType>>(Convert, ObjectInstance);
  49:             }
  50:             return Expression.Lambda<Func<ClassType, DataType>>(PropertyGet, ObjectInstance);
  51:         }
  52:  
  53:         /// <summary>
  54:         /// Gets a lambda expression that calls a specific property's getter function
  55:         /// </summary>
  56:         /// <typeparam name="ClassType">Class type</typeparam>
  57:         /// <param name="PropertyName">Property name</param>
  58:         /// <returns>A lambda expression that calls a specific property's getter function</returns>
  59:         public static Expression<Func<ClassType, object>> GetPropertyGetter<ClassType>(string PropertyName)
  60:         {
  61:             return GetPropertyGetter<ClassType, object>(PropertyName);
  62:         }
  63:  
  64:         /// <summary>
  65:         /// Gets a lambda expression that calls a specific property's getter function
  66:         /// </summary>
  67:         /// <typeparam name="ClassType">Class type</typeparam>
  68:         /// <param name="Property">Property</param>
  69:         /// <returns>A lambda expression that calls a specific property's getter function</returns>
  70:         public static Expression<Func<ClassType, object>> GetPropertyGetter<ClassType>(PropertyInfo Property)
  71:         {
  72:             return GetPropertyGetter<ClassType, object>(Property);
  73:         }
   1:  
   2:         /// <summary>
   3:         /// Gets a lambda expression that calls a specific property's setter function
   4:         /// </summary>
   5:         /// <typeparam name="ClassType">Class type</typeparam>
   6:         /// <typeparam name="DataType">Data type expecting</typeparam>
   7:         /// <param name="PropertyName">Property name</param>
   8:         /// <returns>A lambda expression that calls a specific property's setter function</returns>
   9:         public static Expression<Action<ClassType, DataType>> GetPropertySetter<ClassType,DataType>(string PropertyName)
  10:         {
  11:             if (string.IsNullOrEmpty(PropertyName))
  12:                 throw new ArgumentNullException("PropertyName");
  13:             string[] SplitName = PropertyName.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
  14:             PropertyInfo Property = Utilities.Reflection.Reflection.GetProperty<ClassType>(SplitName[0]);
  15:             ParameterExpression ObjectInstance = Expression.Parameter(Property.DeclaringType, "x");
  16:             ParameterExpression PropertySet = Expression.Parameter(typeof(DataType), "y");
  17:             MethodCallExpression SetterCall = null;
  18:             MemberExpression PropertyGet = null;
  19:             if (SplitName.Length > 1)
  20:             {
  21:                 PropertyGet = Expression.Property(ObjectInstance, Property);
  22:                 for (int x = 1; x < SplitName.Length - 1; ++x)
  23:                 {
  24:                     Property = Utilities.Reflection.Reflection.GetProperty(Property.PropertyType, SplitName[x]);
  25:                     PropertyGet = Expression.Property(PropertyGet, Property);
  26:                 }
  27:                 Property = Utilities.Reflection.Reflection.GetProperty(Property.PropertyType, SplitName[SplitName.Length - 1]);
  28:             }
  29:             if (Property.PropertyType != typeof(DataType))
  30:             {
  31:                 UnaryExpression Convert = Expression.Convert(PropertySet, Property.PropertyType);
  32:                 if (PropertyGet == null)
  33:                     SetterCall = Expression.Call(ObjectInstance, Property.GetSetMethod(), Convert);
  34:                 else
  35:                     SetterCall = Expression.Call(PropertyGet, Property.GetSetMethod(), Convert);
  36:                 return Expression.Lambda<Action<ClassType, DataType>>(SetterCall, ObjectInstance, PropertySet);
  37:             }
  38:             if (PropertyGet == null)
  39:                 SetterCall = Expression.Call(ObjectInstance, Property.GetSetMethod(), PropertySet);
  40:             else
  41:                 SetterCall = Expression.Call(PropertyGet, Property.GetSetMethod(), PropertySet);
  42:             return Expression.Lambda<Action<ClassType, DataType>>(SetterCall, ObjectInstance, PropertySet);
  43:         }
  44:  
  45:         /// <summary>
  46:         /// Gets a lambda expression that calls a specific property's setter function
  47:         /// </summary>
  48:         /// <typeparam name="ClassType">Class type</typeparam>
  49:         /// <typeparam name="DataType">Data type expecting</typeparam>
  50:         /// <param name="PropertyName">Property name</param>
  51:         /// <returns>A lambda expression that calls a specific property's setter function</returns>
  52:         public static Expression<Action<ClassType, DataType>> GetPropertySetter<ClassType, DataType>(PropertyInfo Property)
  53:         {
  54:             if (!IsOfType(Property.PropertyType, typeof(DataType)))
  55:                 throw new ArgumentException("Property is not of the type specified");
  56:             if (!IsOfType(Property.DeclaringType, typeof(ClassType)))
  57:                 throw new ArgumentException("Property is not from the declaring class type specified");
  58:  
  59:             ParameterExpression ObjectInstance = Expression.Parameter(Property.DeclaringType, "x");
  60:             ParameterExpression PropertySet = Expression.Parameter(typeof(DataType), "y");
  61:             MethodCallExpression SetterCall = null;
  62:             if(Property.PropertyType!=typeof(DataType))
  63:             {
  64:                 
  65:                 UnaryExpression Convert=Expression.Convert(PropertySet,Property.PropertyType);
  66:                 SetterCall = Expression.Call(ObjectInstance, Property.GetSetMethod(), Convert);
  67:                 return Expression.Lambda<Action<ClassType, DataType>>(SetterCall, ObjectInstance, PropertySet);
  68:             }
  69:             SetterCall = Expression.Call(ObjectInstance, Property.GetSetMethod(), PropertySet);
  70:             return Expression.Lambda<Action<ClassType, DataType>>(SetterCall, ObjectInstance, PropertySet);
  71:         }
  72:  
  73:         /// <summary>
  74:         /// Gets a lambda expression that calls a specific property's setter function
  75:         /// </summary>
  76:         /// <typeparam name="ClassType">Class type</typeparam>
  77:         /// <param name="PropertyName">Property name</param>
  78:         /// <returns>A lambda expression that calls a specific property's setter function</returns>
  79:         public static Expression<Action<ClassType,object>> GetPropertySetter<ClassType>(string PropertyName)
  80:         {
  81:             return GetPropertySetter<ClassType, object>(PropertyName);
  82:         }
  83:  
  84:         /// <summary>
  85:         /// Gets a lambda expression that calls a specific property's setter function
  86:         /// </summary>
  87:         /// <typeparam name="ClassType">Class type</typeparam>
  88:         /// <param name="PropertyName">Property name</param>
  89:         /// <returns>A lambda expression that calls a specific property's setter function</returns>
  90:         public static Expression<Action<ClassType, object>> GetPropertySetter<ClassType>(PropertyInfo Property)
  91:         {
  92:             return GetPropertySetter<ClassType, object>(Property);
  93:         }

I don't have object creation or null checking in there, but it does do type conversion, etc. So not perfect, but it will do for our purposes. But basically I can use one of my other functions in my utility library:

   1: /// <summary>
   2: /// Gets the name of the property held within the expression
   3: /// </summary>
   4: /// <typeparam name="T">The type of object used in the expression</typeparam>
   5: /// <param name="Expression">The expression</param>
   6: /// <returns>A string containing the name of the property</returns>
   7: public static string GetPropertyName<T>(Expression<Func<T, object>> Expression)
   8: {
   9:     if (Expression == null)
  10:         return "";
  11:     string Name = "";
  12:     if (Expression.Body.NodeType == ExpressionType.Convert)
  13:     {
  14:         Name = Expression.Body.ToString().Replace("Convert(", "").Replace(")", "");
  15:         Name = Name.Remove(0, Name.IndexOf(".") + 1);
  16:     }
  17:     else
  18:     {
  19:         Name = Expression.Body.ToString();
  20:         Name = Name.Remove(0, Name.IndexOf(".") + 1);
  21:     }
  22:     return Name;
  23: }
  24:  
  25: /// <summary>
  26: /// Gets a property name
  27: /// </summary>
  28: /// <typeparam name="ClassType">Class type</typeparam>
  29: /// <typeparam name="DataType">Data type of the property</typeparam>
  30: /// <param name="Expression">LINQ expression</param>
  31: /// <returns>The name of the property</returns>
  32: public static string GetPropertyName<ClassType, DataType>(Expression<Func<ClassType, DataType>> Expression)
  33: {
  34:     if (!(Expression.Body is MemberExpression))
  35:         throw new ArgumentException("Expression.Body is not a MemberExpression");
  36:     return GetPropertyName(((MemberExpression)Expression.Body).Expression) + ((MemberExpression)Expression.Body).Member.Name;
  37: }
  38:  
  39: private static string GetPropertyName(Expression expression)
  40: {
  41:     if (!(expression is MemberExpression))
  42:         return "";
  43:     return GetPropertyName(((MemberExpression)expression).Expression) + ((MemberExpression)expression).Member.Name + ".";
  44: }

And send that to GetPropertyGetter/Setter and you have the Expression trees that you need for getting/setting your properties. So let's look at how we can use this:

   1: /// <summary>
   2: /// Mapping class
   3: /// </summary>
   4: /// <typeparam name="Left">Left type</typeparam>
   5: /// <typeparam name="Right">Right type</typeparam>
   6: public class Mapping<Left, Right>
   7: {
   8:     #region Constructors
   9:  
  10:     /// <summary>
  11:     /// Constructor
  12:     /// </summary>
  13:     /// <param name="LeftExpression">Left expression</param>
  14:     /// <param name="RightExpression">Right expression</param>
  15:     public Mapping(Expression<Func<Left, object>> LeftExpression, Expression<Func<Right, object>> RightExpression)
  16:     {
  17:         string Name = Utilities.Reflection.Reflection.GetPropertyName(LeftExpression);
  18:         LeftGet = Utilities.Reflection.Reflection.GetPropertyGetter<Left>(Name).Compile();
  19:         LeftSet = Utilities.Reflection.Reflection.GetPropertySetter<Left>(Name).Compile();
  20:         Name = Utilities.Reflection.Reflection.GetPropertyName(RightExpression);
  21:         RightGet = Utilities.Reflection.Reflection.GetPropertyGetter<Right>(Name).Compile();
  22:         RightSet = Utilities.Reflection.Reflection.GetPropertySetter<Right>(Name).Compile();
  23:     }
  24:  
  25:     /// <summary>
  26:     /// Constructor
  27:     /// </summary>
  28:     /// <param name="LeftGet">Left get function</param>
  29:     /// <param name="LeftSet">Left set action</param>
  30:     /// <param name="RightExpression">Right expression</param>
  31:     public Mapping(Func<Left, object> LeftGet, Action<Left, object> LeftSet, Expression<Func<Right, object>> RightExpression)
  32:     {
  33:         this.LeftGet = LeftGet;
  34:         this.LeftSet = LeftSet;
  35:         string Name = Utilities.Reflection.Reflection.GetPropertyName(RightExpression);
  36:         RightGet = Utilities.Reflection.Reflection.GetPropertyGetter<Right>(Name).Compile();
  37:         RightSet = Utilities.Reflection.Reflection.GetPropertySetter<Right>(Name).Compile();
  38:     }
  39:  
  40:     /// <summary>
  41:     /// Constructor
  42:     /// </summary>
  43:     /// <param name="LeftExpression">Left expression</param>
  44:     /// <param name="RightGet">Right get function</param>
  45:     /// <param name="RightSet">Right set function</param>
  46:     public Mapping(Expression<Func<Left, object>> LeftExpression, Func<Right, object> RightGet, Action<Right, object> RightSet)
  47:     {
  48:         string Name = Utilities.Reflection.Reflection.GetPropertyName(LeftExpression);
  49:         LeftGet = Utilities.Reflection.Reflection.GetPropertyGetter<Left>(Name).Compile();
  50:         LeftSet = Utilities.Reflection.Reflection.GetPropertySetter<Left>(Name).Compile();
  51:         this.RightGet = RightGet;
  52:         this.RightSet = RightSet;
  53:     }
  54:  
  55:     /// <summary>
  56:     /// Constructor
  57:     /// </summary>
  58:     /// <param name="LeftGet">Left get function</param>
  59:     /// <param name="LeftSet">Left set function</param>
  60:     /// <param name="RightGet">Right get function</param>
  61:     /// <param name="RightSet">Right set function</param>
  62:     public Mapping(Func<Left, object> LeftGet, Action<Left, object> LeftSet, Func<Right, object> RightGet, Action<Right, object> RightSet)
  63:     {
  64:         this.LeftGet = LeftGet;
  65:         this.LeftSet = LeftSet;
  66:         this.RightGet = RightGet;
  67:         this.RightSet = RightSet;
  68:     }
  69:  
  70:     #endregion
  71:  
  72:     #region Properties
  73:  
  74:     /// <summary>
  75:     /// Left get function
  76:     /// </summary>
  77:     protected virtual Func<Left, object> LeftGet { get; set; }
  78:  
  79:     /// <summary>
  80:     /// Right get function
  81:     /// </summary>
  82:     protected virtual Func<Right, object> RightGet { get; set; }
  83:  
  84:     /// <summary>
  85:     /// Left set function
  86:     /// </summary>
  87:     protected virtual Action<Left, object> LeftSet { get; set; }
  88:  
  89:     /// <summary>
  90:     /// Right set function
  91:     /// </summary>
  92:     protected virtual Action<Right, object> RightSet { get; set; }
  93:  
  94:     #endregion
  95:  
  96:     #region Functions
  97:  
  98:     /// <summary>
  99:     /// Copies the source to the destination
 100:     /// </summary>
 101:     /// <param name="Source">Source object</param>
 102:     /// <param name="Destination">Destination object</param>
 103:     public virtual void Copy(Left Source, Right Destination)
 104:     {
 105:         RightSet(Destination, LeftGet(Source));
 106:     }
 107:  
 108:     /// <summary>
 109:     /// Copies the source to the destination
 110:     /// </summary>
 111:     /// <param name="Source">Source object</param>
 112:     /// <param name="Destination">Destination object</param>
 113:     public virtual void Copy(Right Source, Left Destination)
 114:     {
 115:         LeftSet(Destination, RightGet(Source));
 116:     }
 117:  
 118:     /// <summary>
 119:     /// Copies from the source to the destination (used in 
 120:     /// instances when both Left and Right are the same type
 121:     /// and thus Copy is ambiguous)
 122:     /// </summary>
 123:     /// <param name="Source">Source object</param>
 124:     /// <param name="Destination">Destination object</param>
 125:     public virtual void CopyLeftToRight(Left Source, Right Destination)
 126:     {
 127:         RightSet(Destination, LeftGet(Source));
 128:     }
 129:  
 130:     #endregion
 131: }

This class is a simple class for copying A to B or vice versa (and it's generic enough that you can pass in your own Funcs/Actions (which will be important in the later stages). We have one special case if Left and Right are the same type and thus the special forms of copy but that's it. However this only copies one property to another, we still need something that copies multiple properties to their appropriate destinations:

   1: /// <summary>
   2: /// Maps two types together
   3: /// </summary>
   4: /// <typeparam name="Left">Left type</typeparam>
   5: /// <typeparam name="Right">Right type</typeparam>
   6: public class TypeMapping<Left, Right>
   7: {
   8:     #region Constructor
   9:  
  10:     /// <summary>
  11:     /// Constructor
  12:     /// </summary>
  13:     public TypeMapping() { Mappings = new List<Mapping<Left, Right>>(); }
  14:  
  15:     #endregion
  16:  
  17:     #region Properties
  18:  
  19:     /// <summary>
  20:     /// List of mappings
  21:     /// </summary>
  22:     protected virtual List<Mapping<Left, Right>> Mappings { get; set; }
  23:  
  24:     #endregion
  25:  
  26:     #region Functions
  27:  
  28:     /// <summary>
  29:     /// Adds a mapping
  30:     /// </summary>
  31:     /// <param name="LeftExpression">Left expression</param>
  32:     /// <param name="RightExpression">Right expression</param>
  33:     public virtual void AddMapping(Expression<Func<Left, object>> LeftExpression, Expression<Func<Right, object>> RightExpression)
  34:     {
  35:         Mappings.Add(new Mapping<Left, Right>(LeftExpression, RightExpression));
  36:     }
  37:  
  38:     /// <summary>
  39:     /// Adds a mapping
  40:     /// </summary>
  41:     /// <param name="LeftGet">Left get function</param>
  42:     /// <param name="LeftSet">Left set action</param>
  43:     /// <param name="RightExpression">Right expression</param>
  44:     public virtual void AddMapping(Func<Left, object> LeftGet, Action<Left, object> LeftSet, Expression<Func<Right, object>> RightExpression)
  45:     {
  46:         Mappings.Add(new Mapping<Left, Right>(LeftGet, LeftSet, RightExpression));
  47:     }
  48:  
  49:     /// <summary>
  50:     /// Adds a mapping
  51:     /// </summary>
  52:     /// <param name="LeftExpression">Left expression</param>
  53:     /// <param name="RightGet">Right get function</param>
  54:     /// <param name="RightSet">Right set function</param>
  55:     public virtual void AddMapping(Expression<Func<Left, object>> LeftExpression, Func<Right, object> RightGet, Action<Right, object> RightSet)
  56:     {
  57:         Mappings.Add(new Mapping<Left, Right>(LeftExpression, RightGet, RightSet));
  58:     }
  59:  
  60:     /// <summary>
  61:     /// Adds a mapping
  62:     /// </summary>
  63:     /// <param name="LeftGet">Left get function</param>
  64:     /// <param name="LeftSet">Left set function</param>
  65:     /// <param name="RightGet">Right get function</param>
  66:     /// <param name="RightSet">Right set function</param>
  67:     public virtual void AddMapping(Func<Left, object> LeftGet, Action<Left, object> LeftSet, Func<Right, object> RightGet, Action<Right, object> RightSet)
  68:     {
  69:         Mappings.Add(new Mapping<Left, Right>(LeftGet, LeftSet, RightGet, RightSet));
  70:     }
  71:  
  72:     /// <summary>
  73:     /// Copies from the source to the destination
  74:     /// </summary>
  75:     /// <param name="Source">Source object</param>
  76:     /// <param name="Destination">Destination object</param>
  77:     public void Copy(Left Source, Right Destination)
  78:     {
  79:         foreach (Mapping<Left, Right> Mapping in Mappings)
  80:         {
  81:             Mapping.Copy(Source, Destination);
  82:         }
  83:     }
  84:  
  85:     /// <summary>
  86:     /// Copies from the source to the destination
  87:     /// </summary>
  88:     /// <param name="Source">Source object</param>
  89:     /// <param name="Destination">Destination object</param>
  90:     public void Copy(Right Source, Left Destination)
  91:     {
  92:         foreach (Mapping<Left, Right> Mapping in Mappings)
  93:         {
  94:             Mapping.Copy(Source, Destination);
  95:         }
  96:     }
  97:  
  98:     /// <summary>
  99:     /// Copies from the source to the destination (used in 
 100:     /// instances when both Left and Right are the same type
 101:     /// and thus Copy is ambiguous)
 102:     /// </summary>
 103:     /// <param name="Source">Source</param>
 104:     /// <param name="Destination">Destination</param>
 105:     public void CopyLeftToRight(Left Source, Right Destination)
 106:     {
 107:         foreach (Mapping<Left, Right> Mapping in Mappings)
 108:         {
 109:             Mapping.CopyLeftToRight(Source, Destination);
 110:         }
 111:     }
 112:  
 113:     #endregion
 114: }

This class simple acts as a list type, allowing you to bundle up multiple mappings. But that's all that we need. We could go one more step up and have a manager that saves the various type mappings, but for the ORM it isn't needed. With this we have a simple way to set up some rather fast functions for copying data back and forth in our system. Next post I'll talk about the wrapper for the database connections and then in the last post, I'll show the actual ORM. So for now, take a look (or look ahead at my utility library as my code is pretty much finished and in there), and happy coding.



Comments