ORM中去除反射,添加Expression

时间:2024-04-29 03:09:23

之前接触了别人的ORM框架,感觉牛掰到不行,然后试着自己来写自己的ORM。

最初从园子里找到其他人写的反射的例子:

     List<PropertyInfo> pis = typeof(T).GetProperties().ToList<PropertyInfo>()
while (dr.Read())
{
T model = Activator.CreateInstance<T>(); foreach (PropertyInfo propertyInfo in pis)
{
propertyInfo.SetValue(model,dr[propertyInfo.Name], null);
}
list.Add(model);
}

基本满足需求,但是性能和Dapper相比,完全被打趴下了啊。
         偶然发现了Expression,好吧,不试怎么知道好用?

         public Action<T, IDataRecord> SetValueToEntity<T>(int index, string ProPertyName, Type FieldType)
{
Type datareader = typeof(IDataRecord);
//获取调用方法
System.Reflection.MethodInfo Method = GetMethod(FieldType, datareader);
ParameterExpression e = Expression.Parameter(typeof(T), "e");
ParameterExpression r = Expression.Parameter(datareader, "r");
//常数表达式
ConstantExpression i = Expression.Constant(index);
MemberExpression ep = Expression.PropertyOrField(e, ProPertyName);
MethodCallExpression call = Expression.Call(r, Method, i);
BinaryExpression assignExpression = Expression.Assign(ep, call);
Expression<Action<T, IDataRecord>> resultEx = Expression.Lambda<Action<T, IDataRecord>>(assignExpression, e, r);
Action<T, IDataRecord> result = resultEx.Compile();
return result;
} public static MethodInfo GetMethod(Type FieldType, Type datareader)
{
switch (FieldType.FullName)
{
case "System.Int16":
return datareader.GetMethod("GetInt16"); case "System.Int32":
return datareader.GetMethod("GetInt32"); case "System.Int64":
return datareader.GetMethod("GetInt64"); case "Double":
return datareader.GetMethod("GetDouble"); case "System.String":
return datareader.GetMethod("GetString"); case "Boolean":
return datareader.GetMethod("GetBoolean"); case "Char":
return datareader.GetMethod("GetChar"); case "System.Guid":
return datareader.GetMethod("GetGuid"); case "Single":
return datareader.GetMethod("GetFloat"); case "Decimal":
return datareader.GetMethod("GetDecimal"); case "System.DateTime":
return datareader.GetMethod("GetDateTime");
case "System.":
return datareader.GetMethod("GetDateTime");
}
return null;
} List<Action<T, IDataReader>> actionDics = new List<Action<T, IDataReader>>();
//数据实体类型
var perDic = typeof(T).GetProperties().ToDictionary(p => p.Name);
//生成表头
for (int i = ; i < dr.FieldCount; i++)
{
//获取列名
string colName = dr.GetName(i);
Type DataType = dr.GetFieldType(i);
if (perDic.ContainsKey(colName))
{
actionDics.Add(SetValueToEntity<T>(i, colName, DataType));
}
}
while (dr.Read())
{
T objT = Activator.CreateInstance<T>();
//填充属性值
actionDics.ForEach(p => p.Invoke(objT, dr));
list.Add(objT);
}

结果还是很可人滴,和Dapper不相上下。

下篇希望可以实现增、删、改。希望园友提供下建议。