您的位置:首页 > 其它

自制简单的.Net ORM框架 (一) 简介

2016-02-18 17:35 363 查看
在自己研究ORM之前,也使用过几个成熟的ORM方案,例如:EntityFramework,PetaPoco,Dapper等,用是很好用,但是对自己来说总是不那么方便,EF比较笨重,Dapper要自定义扩展等等,所以萌发了开发适合自己的ORM的想法,因为未起名字,所以下文所有地方都使用MyORM来代替.Nuget地址:https://www.nuget.org/packages/Dai.CommonLib.Sql
简介:MyORM是一个小型的ORM,稍微[b]参考了PetaPoco,支持简单的对象关系映射,非常智能化,整个框架主要使用2个类型:QueryContext和DbQuery,其中DbQuery只是一个高级的SqlHelper,故下文主要介绍QueryContext.[/b]目前只支持SqlServer2005+.Mysql的话以后再加上1.一些特性MyORM的映射关系主要是用LinqToSql自带的2个特性来完成的TableAttribute和ColumnAttribute例:
[Table(Name="AlbumInfo")]
publicpartialclassAlbumInfo
{

[Column(Name="Id",IsPrimaryKey=true,IsDbGenerated=true)]
publicintId{get;set;}

[Column(Name="AlbumName")]
publicstringAlbumName{get;set;}

[Column(Name="CoverImg")]
publicstringCoverImg{get;set;}

[Column(Name="Desc")]
publicstringDesc{get;set;}

[Column(Name="Scope")]
publicintScope{get;set;}

[Column(Name="LookCount")]
publicintLookCount{get;set;}

[Column(Name="PraiseCount")]
publicintPraiseCount{get;set;}

[Column(Name="CommentCount")]
publicintCommentCount{get;set;}

[Column(Name="CreatorId")]
publicintCreatorId{get;set;}

[Column(Name="CreateTime")]
publicDateTimeCreateTime{get;set;}

[Column(Name="State")]
publicintState{get;set;}

}
并且MyORM支持MetadataTypeAttribute特性
[MetadataType(typeof(MetadataTypeBase))]
partialclassAlbumInfo
{
}

internalclassMetadataTypeBase
{

     [System.Data.DeletedMapping(2)]
     [System.Data.InvalidValue(2)]
     [System.Data.ValidValue(0)]
     [System.Data.ValidValue(1)]
publicintState{get;set;}
}
上面有几个定义的特性:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;

namespaceSystem.Data
{
///<summary>
///一个特性,用于标记数据删除状态
///</summary>
[AttributeUsage(AttributeTargets.Property,AllowMultiple=false)]
publicsealedclassDeletedMappingAttribute:Attribute
{
publicDeletedMappingAttribute(objectvalue)
{
if(value==null||value==DBNull.Value)
{
thrownewArgumentNullException("值不能为null或DbNull.Value","value");
}
this.Value=value;
}
///<summary>
///获取标记为删除的值
///</summary>
publicobjectValue{get;privateset;}
}
}

//DeletedMappingAttribute顾名思义,就是它用来标记一些并不需要真删除的数据,后面介绍删除的时候会用到.

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;namespaceSystem.Data
{
///<summary>
///一个特性,用于标识数据字段数值的无效性
///</summary>
[AttributeUsage(AttributeTargets.Property,AllowMultiple=true)]
publicsealedclassInvalidValueAttribute:Attribute
{
publicInvalidValueAttribute(objectvalue)
{
//if(value==null||value==DBNull.Value)
//{
//thrownewArgumentNullException("值不能为null或DbNull.Value","value");
//}
this.Value=value;
}
///<summary>
///获取字段的无效值
///</summary>
publicobjectValue{get;privateset;}
}
}//这个特性主要用于根据主键查询数据,它会自动过滤掉不符合条件的数据,简化我们编码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;namespaceSystem.Data
{
///<summary>
///一个特性,用于标识数据字段数值的有效性
///</summary>
[AttributeUsage(AttributeTargets.Property,AllowMultiple=true)]
publicsealedclassValidValueAttribute:Attribute
{
publicValidValueAttribute(objectvalue)
{
//if(value==null||value==DBNull.Value)
//{
//thrownewArgumentNullException("值不能为null或DbNull.Value","value");
//}
this.Value=value;
}
///<summary>
///获取字段的有效值
///</summary>
publicobjectValue{get;privateset;}
}
}

//这个特性主要用于根据主键查询数据,它会自动匹符合条件的数据,简化我们编码

2.开始使用:表AlbumInfo:
usingSystem;namespaceSystem.Data
{
//摘要:
//数据查询上下文,支持映射实体的操作(使用默认设置)
publicsealedclassQueryContext:QueryContextBase
{
publicQueryContext();
publicQueryContext(stringconnectionStringName);publicstaticstringDefaultConnectionStringName{get;set;}
}
}usingSystem;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
usingSystem.Linq.Expressions;
usingSystem.Threading.Tasks;namespaceSystem.Data
{
//摘要:
//数据查询上下文,支持映射实体的操作
publicabstractclassQueryContextBase:DbQueryMethod,IQueryContext,IAsyncQueryContext,IDisposable
{
protectedQueryContextBase(stringconnectionStringName);//摘要:
//根据主键获取数据(可使用System.Data.ValidValueAttribute来约定条件)
//
//参数:
//primaryValue:
//
//类型参数:
//T:
publicTByPrimaryKey<T>(objectprimaryValue)whereT:class;
//
//摘要:
//根据主键获取数据(可使用System.Data.ValidValueAttribute来约定条件)
//
//参数:
//primaryValue:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
publicTResultByPrimaryKey<TTable,TResult>(objectprimaryValue)
whereTTable:class
whereTResult:class;
//
//摘要:
//根据主键获取数据,并指定是否忽略约束条件
//
//参数:
//primaryValue:
//
//ignore:
//
//类型参数:
//T:
publicTByPrimaryKey<T>(objectprimaryValue,boolignore)whereT:class;
//
//摘要:
//根据主键获取数据,并指定是否忽略约束条件
//
//参数:
//primaryValue:
//
//ignore:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
publicTResultByPrimaryKey<TTable,TResult>(objectprimaryValue,boolignore)
whereTTable:class
whereTResult:class;
//
//摘要:
//异步根据主键获取数据(可使用System.Data.ValidValueAttribute来约定条件)
//
//参数:
//primaryValue:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<T>ByPrimaryKeyAsync<T>(objectprimaryValue)whereT:class;
//
//摘要:
//异步根据主键获取数据(可使用System.Data.ValidValueAttribute来约定条件)
//
//参数:
//primaryValue:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
[DebuggerStepThrough]
publicTask<TResult>ByPrimaryKeyAsync<TTable,TResult>(objectprimaryValue)
whereTTable:class
whereTResult:class;
//
//摘要:
//异步根据主键获取数据,并指定是否忽略约束条件
//
//参数:
//primaryValue:
//
//ignore:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<T>ByPrimaryKeyAsync<T>(objectprimaryValue,boolignore)whereT:class;
//
//摘要:
//异步根据主键获取数据,并指定是否忽略约束条件
//
//参数:
//primaryValue:
//
//ignore:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
[DebuggerStepThrough]
publicTask<TResult>ByPrimaryKeyAsync<TTable,TResult>(objectprimaryValue,boolignore)
whereTTable:class
whereTResult:class;
//
//摘要:
//基于当前提供的驱动程序创建一个System.Data.DbParameter
//
//参数:
//parameterName:
//
//value:
publicIDbDataParameterCreateParameter(stringparameterName,objectvalue);
//
//摘要:
//基于当前提供的驱动程序创建一个System.Data.DbParameter,并指定System.Data.DbParameter参数类型
//
//参数:
//parameterName:
//
//value:
//
//direction:
publicIDbDataParameterCreateParameter(stringparameterName,objectvalue,ParameterDirectiondirection);
//
//摘要:
//根据主键删除实体(可用System.Data.DeletedMappingAttribute控制删除还是修改删除状态)
//
//参数:
//entity:
//
//类型参数:
//T:
publicboolDelete<T>(Tentity)whereT:class;
//
//摘要:
//根据主键删除实体,一个参数指示是否忽略System.Data.DeletedMappingAttribute属性
//
//参数:
//entity:
//
//ignoreDeletedMappingAttribute:
//
//类型参数:
//T:
publicboolDelete<T>(Tentity,boolignoreDeletedMappingAttribute)whereT:class;
//
//摘要:
//异步根据主键删除实体(可用System.Data.DeletedMappingAttribute控制删除还是修改删除状态)
//
//参数:
//entity:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<bool>DeleteAsync<T>(Tentity)whereT:class;
//
//摘要:
//异步根据主键删除实体,一个参数指示是否忽略System.Data.DeletedMappingAttribute属性
//
//参数:
//entity:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<bool>DeleteAsync<T>(Tentity,boolignoreDeletedMappingAttribute)whereT:class;
//
//摘要:
//执行Sql
//
//参数:
//sql:
//
//args:
publicintExecute(stringsql,paramsobject[]args);
//
//摘要:
//异步执行Sql
//
//参数:
//sql:
//
//args:
[DebuggerStepThrough]
publicTask<int>ExecuteAsync(stringsql,paramsobject[]args);
//
//摘要:
//执行存储过程
//
//参数:
//procedureName:
//
//args:
publicintExecuteProcedure(stringprocedureName,paramsobject[]args);
//
//摘要:
//异步执行存储过程
//
//参数:
//procedureName:
//
//args:
[DebuggerStepThrough]
publicTask<int>ExecuteProcedureAsync(stringprocedureName,paramsobject[]args);
//
//摘要:
//执行查询,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
//
//参数:
//sql:
//
//args:
publicobjectExecuteScalar(stringsql,paramsobject[]args);
//
//摘要:
//执行查询,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
publicTExecuteScalar<T>(stringsql,paramsobject[]args);
//
//摘要:
//异步执行查询,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<T>ExecuteScalarAsync<T>(stringsql,paramsobject[]args);
//
//摘要:
//异步执行查询,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
//
//参数:
//sql:
//
//args:
[DebuggerStepThrough]
publicTask<object>ExecuteScalarAsync(stringsql,paramsobject[]args);
//
//摘要:
//执行查询存储过程,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
//
//参数:
//procedureName:
//
//args:
//
//类型参数:
//T:
publicTExecuteScalarProcedure<T>(stringprocedureName,paramsobject[]args);
//
//摘要:
//执行查询存储过程,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
//
//参数:
//procedureName:
//
//args:
publicobjectExecuteScalarProcedure(stringprocedureName,paramsobject[]args);
//
//摘要:
//异步执行查询存储过程,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
//
//参数:
//procedureName:
//
//args:
[DebuggerStepThrough]
publicTask<object>ExecuteScalarProcedureAsync(stringprocedureName,paramsobject[]args);
//
//摘要:
//异步执行查询存储过程,并返回查询所返回的结果集中第一行的第一列。所有其他的列和行将被忽略。
//
//参数:
//procedureName:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<T>ExecuteScalarProcedureAsync<T>(stringprocedureName,paramsobject[]args);
//
//摘要:
//查询指定数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
publicList<T>Fetch<T>(stringsql,paramsobject[]args);
//
//摘要:
//查询指定数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
publicList<TResult>Fetch<TTable,TResult>(stringsql,paramsobject[]args)
whereTTable:class
whereTResult:class;
//
//摘要:
//查询数据
//
//参数:
//sql:
//
//args:
publicList<dynamic>Fetch(stringsql,paramsobject[]args);
//
//摘要:
//异步查询指定数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
[DebuggerStepThrough]
publicTask<List<TResult>>FetchAsync<TTable,TResult>(stringsql,paramsobject[]args)
whereTTable:class
whereTResult:class;
//
//摘要:
//异步查询指定数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<List<T>>FetchAsync<T>(stringsql,paramsobject[]args);
//
//摘要:
//异步查询数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<List<dynamic>>FetchAsync(stringsql,paramsobject[]args);
//
//摘要:
//调用存储过程查询指定数据
//
//参数:
//procedureName:
//
//args:
//
//类型参数:
//T:
publicList<T>FetchProcedure<T>(stringprocedureName,paramsobject[]args);
//
//摘要:
//调用存储过程查询数据
//
//参数:
//procedureName:
//
//args:
publicList<dynamic>FetchProcedure(stringprocedureName,paramsobject[]args);
//
//摘要:
//调用存储过程异步查询指定数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<List<T>>FetchProcedureAsync<T>(stringprocedureName,paramsobject[]args);
//
//摘要:
//调用存储过程异步查询数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<List<dynamic>>FetchProcedureAsync(stringprocedureName,paramsobject[]args);
//
//摘要:
//查询第一条数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
publicTResultFirstOrDefault<TTable,TResult>(stringsql,paramsobject[]args)
whereTTable:class
whereTResult:class;
//
//摘要:
//查询第一条数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
publicTFirstOrDefault<T>(stringsql,paramsobject[]args);
//
//摘要:
//查询第一条数据
//
//参数:
//sql:
//
//args:
publicdynamicFirstOrDefault(stringsql,paramsobject[]args);
//
//摘要:
//异步查询第一条数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<T>FirstOrDefaultAsync<T>(stringsql,paramsobject[]args);
//
//摘要:
//异步查询第一条数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<dynamic>FirstOrDefaultAsync(stringsql,paramsobject[]args);
//
//摘要:
//异步查询第一条数据
//
//参数:
//sql:
//
//args:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
[DebuggerStepThrough]
publicTask<TResult>FirstOrDefaultAsync<TTable,TResult>(stringsql,paramsobject[]args)
whereTTable:class
whereTResult:class;
//
//摘要:
//查询第一条数据
//
//参数:
//procedureName:
//
//args:
//
//类型参数:
//T:
publicTFirstOrDefaultProcedure<T>(stringprocedureName,paramsobject[]args);
//
//摘要:
//查询第一条数据
//
//参数:
//procedureName:
//
//args:
publicdynamicFirstOrDefaultProcedure(stringprocedureName,paramsobject[]args);
//
//摘要:
//调用存储过程异步查询第一条数据
//
//参数:
//procedureName:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<T>FirstOrDefaultProcedureAsync<T>(stringprocedureName,paramsobject[]args);
//
//摘要:
//调用存储过程异步查询第一条数据
//
//参数:
//procedureName:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<dynamic>FirstOrDefaultProcedureAsync(stringprocedureName,paramsobject[]args);
//
//摘要:
//得到表列
//
//类型参数:
//T:
publicstring[]GetColumns<T>()whereT:class;
//
//摘要:
//得到表列
//
//类型参数:
//T:
publicstaticstring[]GetColumns<T>(stringconnectionStringName);
//
//摘要:
//获取查询表列Sql
//
//类型参数:
//T:
publicstringGetColumnsString<T>()whereT:class;
//
//摘要:
//将映射实体插入数据库
//
//参数:
//entity:
//
//类型参数:
//T:
publicobjectInsert<T>(Tentity)whereT:class;
//
//摘要:
//异步将映射实体插入数据库
//
//参数:
//entity:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<object>InsertAsync<T>(Tentity)whereT:class;
//
//摘要:
//查询分页数据
//
//参数:
//page:
//
//pageSize:
//
//sql:
//
//args:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
publicIPageResult<TResult>Page<TTable,TResult>(intpage,intpageSize,stringsql,paramsobject[]args)
whereTTable:class
whereTResult:class;
//
//摘要:
//查询分页数据
//
//参数:
//page:
//
//pageSize:
//
//sql:
//
//args:
//
//类型参数:
//T:
publicIPageResult<T>Page<T>(intpage,intpageSize,stringsql,paramsobject[]args);
//
//摘要:
//查询分页数据
//
//参数:
//page:
//
//pageSize:
//
//sql:
//
//args:
publicIPageResult<dynamic>Page(intpage,intpageSize,stringsql,paramsobject[]args);
//
//摘要:
//异步查询分页
//
//参数:
//page:
//
//pageSize:
//
//sql:
//
//args:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<IPageResult<T>>PageAsync<T>(intpage,intpageSize,stringsql,paramsobject[]args);
//
//摘要:
//异步查询分页
//
//参数:
//page:
//
//pageSize:
//
//sql:
//
//args:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
[DebuggerStepThrough]
publicTask<IPageResult<TResult>>PageAsync<TTable,TResult>(intpage,intpageSize,stringsql,paramsobject[]args)
whereTTable:class
whereTResult:class;
//
//摘要:
//异步查询分页
//
//参数:
//page:
//
//pageSize:
//
//sql:
//
//args:
[DebuggerStepThrough]
publicTask<IPageResult<dynamic>>PageAsync(intpage,intpageSize,stringsql,paramsobject[]args);
//
//摘要:
//查询指定数据
//
//参数:
//sql:
//
//pars:
//
//类型参数:
//T:
publicIEnumerable<T>Query<T>(stringsql,paramsobject[]args);
//
//摘要:
//查询数据
//
//参数:
//sql:
//
//pars:
publicIEnumerable<dynamic>Query(stringsql,paramsobject[]args);
//
//摘要:
//查询指定数据
//
//参数:
//sql:
//
//pars:
//
//类型参数:
//TTable:
//要查询的数据表类型
//
//TResult:
//返回结果类型
publicIEnumerable<TResult>Query<TTable,TResult>(stringsql,paramsobject[]args)
whereTTable:class
whereTResult:class;
//
//摘要:
//调用存储过程查询指定数据
//
//参数:
//sql:
//
//pars:
//
//类型参数:
//T:
publicIEnumerable<T>QueryProcedure<T>(stringprocedureName,paramsobject[]args);
//
//摘要:
//调用存储过程查询数据
//
//参数:
//sql:
//
//pars:
//
//类型参数:
//T:
publicIEnumerable<dynamic>QueryProcedure(stringprocedureName,paramsobject[]args);
//
//摘要:
//启动事务
//
//参数:
//context:
publicvoidTransaction(Action<IQueryContext>action);
//
//摘要:
//启动事务
//
//参数:
//context:
publicvoidTransaction(IsolationLevelisolationLevel,Action<IQueryContext>action);
//
//摘要:
//启动异步事务
//
//参数:
//action:
[DebuggerStepThrough]
publicTaskTransactionAsync(Func<IAsyncQueryContext,Task>action);
//
//摘要:
//启动异步事务
//
//参数:
//action:
[DebuggerStepThrough]
publicTaskTransactionAsync(IsolationLevelisolationLevel,Func<IAsyncQueryContext,Task>action);
//
//摘要:
//将映射实体更新到数据库中
//
//参数:
//entity:
//
//properties:
//指定的属性
//
//类型参数:
//T:
publicboolUpdate<T>(Tentity,paramsExpression<Func<T,object>>[]properties)whereT:class;
//
//摘要:
//将映射实体更新到数据库中
//
//参数:
//entity:
//
//columns:
//暂不支持
//
//类型参数:
//T:
publicboolUpdate<T>(Tentity,paramsstring[]columns)whereT:class;
//
//摘要:
//异步将映射实体更新到数据库中
//
//参数:
//entity:
//
//properties:
//指定的属性
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<bool>UpdateAsync<T>(Tentity,paramsExpression<Func<T,object>>[]properties)whereT:class;
//
//摘要:
//异步将映射实体更新到数据库中
//
//参数:
//entity:
//
//columns:
//
//类型参数:
//T:
[DebuggerStepThrough]
publicTask<bool>UpdateAsync<T>(Tentity,paramsstring[]columns)whereT:class;
}
}
ViewCode性能方面,QueryContext内部使用Expression生成委托,并缓存委托的方式,因此性能不会差到哪去,比反射肯定是好的.关于MyORM相关的简单介绍就先告一段落了,近期有时间的话,我会把一些主要的结构,想法,代码介绍一下.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: