实现ORM的四种方案

      正如ORM名称所指示的,实现ORM的关键点在于解决“对象--关系”之间的映射,例如,如何将一个DataRow转换为一个Entity Object,又如何将一个对某Entity Object的操作映射到一个IDbCommand,等等。我们以DataRabbit为例,在DataRabbit中,使用IORMapping接口来抽象这些映射:

    public   interface  IORMapping < TEntity >
    {
        TEntity GetEntityFrom(DataRow row ,
bool  withBlob);
        
        
///   <summary>
        
///  FillParameterValue  使用entity的内容填充command中的各个IDbDataParameter的参数值。
        
///   </summary>       
         void  FillParameterValue(IDbCommand command, TEntity entity);
     }

     关于如何实现IORMapping接口,至少有四种方案。我们以实现GetEntityFrom方法为例为例来讲述这四种方案,这个方法是将一个DataRow转换为一个Entity Object。

1.代码生成器

     现在有很多代码生成器可以直接生成实现了ORM功能的DAL层,其背后的原理是,根据数据表的大纲(如有哪些列、每个列的类型等信息)来生成对应的实现了IORMapping接口的类。代码生成器是在编译之前就完成了这些工作的。

2.反射

     将一个DataRow转换为一个Entity Object,如果粒度更细一点,我们实际要解决的是如何将DataRow的一列的值赋值给Entity Object对应的属性,我们可以使用反射在运行时给Entity Object的属性赋值,如:

entityType.InvokeMember(columnName, BindingFlags.Public  |  BindingFlags.IgnoreCase  |
                        BindingFlags.Instance 
|  BindingFlags.SetProperty,  null , entity, row[columnName]);

     这行代码的含义是将row中【columnName】列的值赋值给entity对象的【columnName】属性。

3.Emit

     我们可以在运行时根据每个Entity类型动态发射对应的实现了IORMapping接口的类型,这些动态类型发射成功后,便可以被实例化然后被使用。比如我们要发射实现GetEntityFrom方法的代码:

private   void  EmitGetEntityFromMethod(TypeBuilder typeBuilder, MethodInfo baseMethod, Type entityType, DataSchema dataSchema)
        {
            MethodBuilder methodBuilder 
=  typeBuilder.DefineMethod( " GetEntityFrom " , baseMethod.Attributes  &   ~ MethodAttributes.Abstract, baseMethod.CallingConvention, baseMethod.ReturnType, EmitHelper.GetParametersType(baseMethod));
            ILGenerator ilGenerator 
=  methodBuilder.GetILGenerator();
            Label retLabel 
=  ilGenerator.DefineLabel();
            ilGenerator.DeclareLocal(entityType); 
// Member member = null ;
            ilGenerator.Emit(OpCodes.Nop);
            ilGenerator.Emit(OpCodes.Newobj, entityType.GetConstructor(
new  Type[] { }));
            ilGenerator.Emit(OpCodes.Stloc_0); 
// member = new Member() ;

            IList
< PropertyInfo >  blobList  =   new  List < PropertyInfo > ();
            
#region  为非blob属性赋值
            
foreach  (PropertyInfo property  in  entityType.GetProperties())
            {
                ColumnSchema columnSchema 
=  dataSchema.GetColumnSchema(property.Name);
                
if  (columnSchema  ==   null )
                {
                    
continue ;
                }

                
if  ((property.PropertyType  ==   typeof ( byte []))  &&  ( ! columnSchema.IsTimestamp))
                {
                    blobList.Add(property);
                    
continue ;
                }

                EmitSetProperty(entityType, ilGenerator, property);
            }
            
#endregion

            
if  (blobList.Count  >   0 )
            {
                ilGenerator.Emit(OpCodes.Ldarg_2);
                ilGenerator.Emit(OpCodes.Brfalse, retLabel);

                
#region  为blob属性赋值
                
foreach  (PropertyInfo property  in  blobList)
                {
                    EmitSetProperty(entityType, ilGenerator, property);
                }
                
#endregion
            }

            ilGenerator.MarkLabel(retLabel);
            ilGenerator.Emit(OpCodes.Nop);
            ilGenerator.Emit(OpCodes.Ldloc_0);
            ilGenerator.Emit(OpCodes.Ret);

            typeBuilder.DefineMethodOverride(methodBuilder, baseMethod);    
//  定义方法重载
        }

 

4.使用Lamda表达式

     如果是在.NET3.5上,我们可以使用动态生成的Lamda表达式来完成Entity Object属性的赋值操作,关键点是要如何生成用于赋值的动态委托。比如:

 

  private  Action < TEntity,  object >  CreateFunctionOfSetProperty < TEntity > (MethodInfo setPropertyMethod, Type columnType)
        {
            ParameterExpression paramEntityObj 
=  Expression.Parameter( typeof (TEntity),  " entity " );
            ParameterExpression paramProVal 
=  Expression.Parameter( typeof ( object ),  " propertyVal " );
            UnaryExpression paramProVal2 
=  Expression.Convert(paramProVal, columnType);
            MethodCallExpression body 
=  Expression.Call(paramEntityObj, setPropertyMethod, paramProVal2);
            Expression
< Action < TEntity,  object >>  setPropertyExpression  =  Expression.Lambda < Action < TEntity,  object >> (body, paramEntityObj, paramProVal);

            Action
< TEntity,  object >  setPropertyAction  =  setPropertyExpression.Compile();

            
return  (entity, propertyVal)  =>  { setPropertyAction(entity, propertyVal); };
        } 

     这个方法返回一个委托,返回的委托接收两个参数--Entity Object 和要赋的属性值,调用这个委托便可以为Entity Object的某个属性进行赋值。

 

     好了,四种方案已经简单介绍完毕,下面我们来比较一下。

(1)除了第一种方案是在编译期完成外,后面三种方案都是在运行期完成的。

(2)第一种方案的效率是最高的,但是所需的手工操作也是最多的(比如每次修改了表结构都需要重新生成DAL层)。第二种方案的效率是最低的,反射使效率的折损非常之大。后两种方案的效率都不错(几乎接近第一种方案)。

(3)Emit方案的实现难度应当是最大的,其次是Lamda表达式方案。

(4)Lamda表达式方案在.NET3.5及以上平台才可使用。

     DataRabbit 3.x及之前版本采用的是Emit方案,后续的4.0及以上版本则对Emit方案和Lamda表达式方案都支持(默认为Emit方案,可以修改配置选项使之采用Lamda表达式方案)。

 

 

 

 

 

 

     

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值