Creating an ORM in C# - Part 2

Second part of the ORM where we actually map the class data.
May 13 2009 by James Craig

This is another long one, but hopefully it wont completely bore you... So I'm a bit further now with the ORM. It's currently to the point where I'm creating classes, overriding properties, etc. And I've already run into bugs with the code that I've posted thus far. As such I have gone back for some fixes. Specifically I didn't realize that the expression tree would actually contain a node to convert the object from one type to an object type. But that's an easy fix. Past that I've also settled on a name for this thing, HaterAide. I chose this because everyone seems to talk about "hydrating" their objects, I hate dealing with data layers, and I saw one of the new "G" commercials when I was thinking up a name. Plus, everything else I thought up was taken...

Let's move on to some code (I'm going to post the current source code later in the post). Last time we talked about using reflection and expression trees in order to get information about the properties within a class and map them within our system. This time we're adding 3 more mapping types and talking about how to use Reflection.Emit in order to create classes that override properties.

So let's start with the mappings. In the last post, I created an ID and Reference mapping type. ID would be the ID of the object (note that I didn't make it unique as I want to allow composite IDs). Reference on the other hand deals with simple types (string, int, etc. in either a list or a single value). This time we're adding Map, ManyToOne, and ManyToMany to the ClassMapping class. Map deals with situations where it is a one to one mapping of another business object. So let's look at its code.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using HaterAide.Interfaces;

namespace HaterAide
{
public class ClassMapping<T> : IClassMap<T>, IAttributeMap
{
private void AddAttribute(Attribute Property, Expression<Func<T, object\>> expression)
{
string\[\] SplitName = null;
if (expression.Body.NodeType == ExpressionType.Convert)
{
Property.Name = expression.Body.ToString().Replace("Convert(", "").Replace(")", "");
string\[\] Splitter = { "." };
SplitName = Property.Name.Split(Splitter, StringSplitOptions.None);
Property.Name = SplitName\[SplitName.Length - 1\];
UnaryExpression TempUnary = (UnaryExpression)expression.Body;
Property.Type = TempUnary.Operand.Type;
}
else
{
Property.Name = expression.Body.ToString();
string\[\] Splitter = { "." };
SplitName = Property.Name.Split(Splitter, StringSplitOptions.None);
Property.Name = SplitName\[SplitName.Length - 1\];
Property.Type = expression.Body.Type;
}
Properties.Add(Property);
}

#region IClassMap Members

public void ID(Expression<Func<T, object\>> expression)
{
Attribute \_ID = new Attribute();
\_ID.AttributeType = AttributeType.ID;
AddAttribute(\_ID, expression);
}

public void Reference(Expression<Func<T, object\>> expression)
{
Attribute \_ID = new Attribute();
\_ID.AttributeType = AttributeType.Reference;
AddAttribute(\_ID, expression);
}

public void Map(Expression<Func<T, object\>> expression, string MappedProperty, bool Cascade)
{
Attribute \_ID = new Attribute();
\_ID.AttributeType = AttributeType.Map;
\_ID.MappedProperty = MappedProperty;
\_ID.Cascade = Cascade;
AddAttribute(\_ID, expression);
}

public void ManyToOne(Expression<Func<T, object\>> expression, string MappedProperty, bool Cascade)
{
Attribute \_ID = new Attribute();
\_ID.AttributeType = AttributeType.ManyToOne;
\_ID.MappedProperty = MappedProperty;
\_ID.Cascade = Cascade;
AddAttribute(\_ID, expression);
}

public void ManyToMany(Expression<Func<T, object\>> expression, string MappedProperty, bool Cascade)
{
Attribute \_ID = new Attribute();
\_ID.AttributeType = AttributeType.ManyToMany;
\_ID.MappedProperty = MappedProperty;
\_ID.Cascade = Cascade;
AddAttribute(\_ID, expression);
}

#endregion

#region IAttributeMap Members

private List<Attribute> \_Properties = new List<Attribute>();

public List<Attribute> Properties
{
get { return \_Properties; }
set { \_Properties = value; }
}

#endregion
}
}

Map, as you can see above, contains two new values that need to be sent in. One is the MappedProperty variable.  It lets the system know what this object corresponds to on the other object. For instance if we have a class called Dog that contained a property called Owner (of type Person) and the Person class contained a property called MyDog (of type Dog), we need to know that those match up. So, on the Dog class we'd pass in "MyDog" for the MappedProperty variable. Note that if the other class doesn't contain a property that maps back, we'd leave this null. The other variable is Cascade. This determines whether or not to cascade deletes, inserts, updates, etc. when this class is updated. ManyToOne and ManyToMany work in similar fashion, just changing the attribute type to the appropriate value.

That's all there is to it really for adding those types. We still load the information the same way we just need extra information when it is created. The next portion that I want to show you is the use of Reflection.Emit.

For those of you that don't know what the Reflection.Emit namespace is, it contains classes that let you spit out MSIL (Microsoft Intermediate language) code, metadata, etc. It's usually only used if you're creating something like a scripting engine. However in our case we're going to use it to dynamically create classes that will use the business objects as base classes. In order to do this, we're going to create 4 classes, a class manager (which looks through the assembly, finds the ClassMapping classes, and stores the created Class objects), a class to define/create the classes, a class to override properties, and a class to define fields. So let's take a look at the ClassManager class first:

 /\*
Copyright (c) 2009 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.\*/

#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;
#endregion

namespace HaterAide.Reflection
{
/// <summary>
/// Class manager
/// </summary>
internal class ClassManager
{
#region Constructor

/// <summary>
/// Constructor
/// </summary>
/// <param name="Assembly">Assembly Using</param>
public ClassManager(Assembly Assembly)
{
CreateAssembly();
LoadClasses(Assembly);
foreach (Type Key in Classes.Keys)
{
object CreatedClass = Activator.CreateInstance(this\[Key\].DerivedType);
PropertyInfo Info = this\[Key\].DerivedType.GetProperty("ID");
Info.SetValue(CreatedClass, 1, null);
this\[Key\].DerivedType.InvokeMember("ClearChanged0", BindingFlags.InvokeMethod, null, CreatedClass, null);
}
}

#endregion

#region Private Functions

/// <summary>
/// Creates the assembly builder
/// </summary>
private void CreateAssembly()
{
AssemblyName Name=new AssemblyName("HaterAide0Classes");
AppDomain Domain=Thread.GetDomain();
Builder=Domain.DefineDynamicAssembly(Name, AssemblyBuilderAccess.Run);
Module = Builder.DefineDynamicModule("HaterAide0ClassesM");
}

/// <summary>
/// Loads the classes from the assembly and maps the business object type to
/// the mapping class
/// </summary>
/// <param name="BusinessObjectsAssembly">Assembly containing the business objects</param>
private void LoadClasses(Assembly BusinessObjectsAssembly)
{
Type\[\] Types=BusinessObjectsAssembly.GetTypes();
foreach (Type TempType in Types)
{
Type BaseType = TempType.BaseType;
if (BaseType != null && BaseType.FullName.StartsWith("HaterAide.ClassMapping"))
{
Classes.Add(BaseType.GetGenericArguments()\[0\], new Class(TempType, Module));
}
}
}

#endregion

#region Private Variables

private Dictionary<Type, Reflection.Class> Classes = new Dictionary<Type, HaterAide.Reflection.Class>();
private AssemblyBuilder Builder=null;
private ModuleBuilder Module=null;

#endregion

#region Public Properties

/// <summary>
/// Gets the associated class with the type
/// </summary>
/// <param name="value">Type value</param>
/// <returns>The generated class associated</returns>
public Reflection.Class this\[Type value\]
{
get
{
return Classes\[value\];
}
}

#endregion
}
}

The ClassManager class was shown last time but you'll notice a number of changes. It still takes an assembly as input, but it adds a function (CreateAssembly). CreateAssembly is our first glimpse of Reflection.Emit. Within the function, we create an AssemblyName object, an AssemblyBuilder object, and a ModuleBuilder object. The AssemblyName object simply defines the assembly's name that the generated objects will reside within. The AssemblyBuilder is used to actually create the assembly and the ModuleBuilder is what we will be using to define the module (pretty straightforward considering their names). These objects have to be created prior to anything else, but really the only object that we're going to care about past this is the ModuleBuilder object.

The LoadClasses function looks fairly similar to the past version, but this time we're sending in the ModuleBuilder to the class object. That's really the only changes to the ClassManager class. The Class class though has a number of changes:

 /\*
Copyright (c) 2009 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.\*/

#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;
using System.Reflection;
using HaterAide.Interfaces;
#endregion

namespace HaterAide.Reflection
{
/// <summary>
/// Used to override a class
/// </summary>
internal class Class
{
public Class(Type Class, ModuleBuilder Module)
{
Type BaseType = Class.BaseType;
Type GenericType = BaseType.GetGenericArguments()\[0\];
TypeBuilder TypeBuilder = Module.DefineType("HaterAide0ClassesM." + GenericType.Name + "Derived", TypeAttributes.Public, GenericType);
FieldBuilder ChangedField = CreateChangedFields(TypeBuilder);
CreateChangedClearMethod(TypeBuilder, ChangedField);
CreateConstructor(ChangedField, TypeBuilder, GenericType);
OverrideProperties(ChangedField, TypeBuilder, Class);

\_DerivedType = TypeBuilder.CreateType();
}

private void CreateChangedClearMethod(TypeBuilder TypeBuilder, FieldBuilder ChangedField)
{
MethodBuilder Method = TypeBuilder.DefineMethod("ClearChanged0", MethodAttributes.Public, typeof(void), new Type\[0\]);
ILGenerator Generator = Method.GetILGenerator();
Generator.Emit(OpCodes.Ldarg\_0);
Generator.Emit(OpCodes.Ldfld, ChangedField);
Generator.Emit(OpCodes.Callvirt, typeof(List<string\>).GetMethod("Clear"));
Generator.Emit(OpCodes.Ret);
}

private void OverrideProperties(FieldBuilder ChangedField, TypeBuilder TypeBuilder, Type ClassMapper)
{
IAttributeMap Item = (IAttributeMap)Activator.CreateInstance(ClassMapper);
foreach (Attribute Property in Item.Properties)
{
if (Property.AttributeType == AttributeType.ID || Property.AttributeType == AttributeType.Reference)
{
Properties.Add(new Property(Property, TypeBuilder, ChangedField));
}
}
}

private FieldBuilder CreateChangedFields(TypeBuilder TypeBuilder)
{
FieldBuilder ValueField = TypeBuilder.DefineField("\_ChangedList0", typeof(List<string\>), FieldAttributes.Private);
PropertyBuilder Property = TypeBuilder.DefineProperty("ChangedList0", PropertyAttributes.None, typeof(List<string\>), null);

MethodAttributes GetSetAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;

MethodBuilder ValuePropertyGet = TypeBuilder.DefineMethod("get\_ChangedList0", GetSetAttributes, typeof(List<string\>), Type.EmptyTypes);
ILGenerator Generator = ValuePropertyGet.GetILGenerator();
Generator.Emit(OpCodes.Ldarg\_0);
Generator.Emit(OpCodes.Ldfld, ValueField);
Generator.Emit(OpCodes.Ret);

MethodBuilder ValuePropertySet = TypeBuilder.DefineMethod("set\_ChangedList0", GetSetAttributes, null, new Type\[\] { typeof(List<string\>) });
Generator = ValuePropertySet.GetILGenerator();
Generator.Emit(OpCodes.Ldarg\_0);
Generator.Emit(OpCodes.Ldarg\_1);
Generator.Emit(OpCodes.Stfld, ValueField);
Generator.Emit(OpCodes.Ret);

Property.SetGetMethod(ValuePropertyGet);
Property.SetSetMethod(ValuePropertySet);

return ValueField;
}

private void CreateConstructor(FieldBuilder ChangedField, TypeBuilder TypeBuilder,Type GenericType)
{
ConstructorBuilder Constructor = TypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type\[0\]);
ILGenerator Generator = Constructor.GetILGenerator();
Generator.Emit(OpCodes.Ldarg\_0);
Generator.Emit(OpCodes.Call, GenericType.GetConstructor(new Type\[0\]));
Generator.Emit(OpCodes.Ldarg\_0);
Generator.Emit(OpCodes.Newobj, typeof(List<string\>).GetConstructor(new Type\[0\]));
Generator.Emit(OpCodes.Stfld, ChangedField);
Generator.Emit(OpCodes.Ret);
}

private List<Property> \_Properties = new List<Property>();

private Type \_DerivedType = null;

public Type DerivedType
{
get { return \_DerivedType; }
}

public List<Property> Properties
{
get { return \_Properties; }
}
}
}

You'll notice that the constructor has a couple of new functions and it creates a TypeBuilder object. The TypeBuilder is the class that actually defines the class. In our case we want a public class that derives from the business object (the GenericType object is the business object type). After the type is initially defined, it isn't created. We still have to define constructors, functions, etc. prior to calling the CreateType function.

Before we do anything, we need to define a Changed field, which will tell us if a property is assigned to (this is done in the CreateChangedFields function).  When defining your class, you can create Fields, Properties, and Methods (there are other things but that's what we care about). In the CreateChangedFields function, we start by creating a field (private list of strings to hold our list of changed variables). We also define a property and then have to define the properties get and set methods. Note that these methods (when defined by the compiler) will almost always start with get_ or set_ and then end with the name of the property. Anyway, within the get/set methods we get our first glimpse of op codes and IL generation.

IL is really nothing more than a series of op codes. Each op code does something (except of course nop). These op codes are generated by something called an ILGenerator. Every time you call Emit, you feed it an op code and the needed variables. In the ChangedList0 property all we're doing is setting/getting the list. So in our get function, we start with the Ldarg_0 op code. This op code loads whatever is at index 0 (usually it's the this variable). We then call Ldfld and feed in our field that we created. Ldfld simply loads a field and puts it on the evaluation stack. We then call Ret. Ret simply takes whatever is on top of the evaluation stack and returns it (in our case the field).

Now before you get worried about having to go and figure out every op code that you'll need for a function, there's a way to get that information from the system itself. First create a project, create a function/class/property that does the action that you want it to do. Then call ILDasm. ILDasm can be found here: C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\ildasm.exe. You can even set it up to run from within Visual Studio. Simply create an external tool entry that takes the location of ILDasm as the command and "$(TargetPath)" as the arguments. That's all there is to it. So no worries on creating functions with Reflection.Emit, it's actually quite easy.

Anyway, the next function that is called is CreateChangedClearMethod. This just allows the system to clear the changed list. Next is the constructor. The reason that isn't created first is the fact that we have to actually create the changed list. The field, when defined, will default to null. So we need to set it in the constructor and as such we need to define the field first before we can instantiate it. The constructor first calls the base class's (our business object's) constructor, then creates a new list of strings, and finally stores it in the changed list field.

After the constructor we move on to the individual properties. We simply create an instance of the ClassMap and pull down the information about the properties. We then create Property objects and store them for later. The Property class is here:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;
using HaterAide.Interfaces;
using System.Reflection;

namespace HaterAide.Reflection
{
internal class Property
{
public Property()
{
}

public Property(Attribute Attribute, TypeBuilder TypeBuilder, FieldBuilder ChangedField)
{
Field = new Field(Attribute, TypeBuilder);
MethodAttributes GetSetAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual;
MethodBuilder ValuePropertyGet = TypeBuilder.DefineMethod("get\_" + Attribute.Name, GetSetAttributes, Attribute.Type, Type.EmptyTypes);
ILGenerator Generator = ValuePropertyGet.GetILGenerator();
Generator.Emit(OpCodes.Ldarg\_0);
Generator.Emit(OpCodes.Ldfld, Field.FieldBuilder);
Generator.Emit(OpCodes.Ret);


MethodBuilder ValuePropertySet = TypeBuilder.DefineMethod("set\_" + Attribute.Name, GetSetAttributes, null, new Type\[\] { Attribute.Type });

Generator = ValuePropertySet.GetILGenerator();

Generator.Emit(OpCodes.Ldarg\_0);
Generator.Emit(OpCodes.Ldarg\_1);
Generator.Emit(OpCodes.Stfld, Field.FieldBuilder);
Generator.Emit(OpCodes.Ldarg\_0);
Generator.Emit(OpCodes.Ldfld, ChangedField);
Generator.Emit(OpCodes.Ldstr, Attribute.Name);
Generator.Emit(OpCodes.Callvirt, typeof(List<string\>).GetMethod("Add"));
Generator.Emit(OpCodes.Ret);

\_Attribute = Attribute;
}

private Field Field = null;

private Attribute \_Attribute;

public Attribute Attribute
{
get { return \_Attribute; }
set { \_Attribute = value; }
}
}
}

This one is similar to the Class object in that we're using ILGenerator to define methods. You will notice one change though. Namely we aren't defining a property. I talked about this issue before, but when you're overriding a property, you're really only overriding the get/set methods. Creating a property with the same name would cause the app to think that there were two properties with the same name, confuse it, and crash our program. Not what we want. Past that, everything should look familiar (with the exception of some op codes that were not used previously). One thing that may be tricky at first can be found in the set function. After storing the information in the field (which is actually created by the Field class) for the property, it goes on to call the Add function on the changed list. It starts off by loading the this variable, then the field, then the name of the property, prior to calling Add. The this variable has to be loaded first because otherwise it does not know from which object the field should be taken from. If it were a local variable, we wouldn't have to worry about it but it's a field so you have to load this first. That annoyed the heck out of me the first time that bug popped up.

And that's all there is to the Reflection portion of the ORM. We now have our classes, they can be created. We can save/get information from them (assuming we had the SQL portion set up). There are a couple minor changes/additions that will need to be added but since I'm going for something basic I'm happy. So download the code, leave feedback, and happy coding.