最近做项目自己整理了一个ORM框架,分享给大家看看,有很多不足望大家指出。

下面是使用方法

BLL 主要方法

逻辑层:子类继承父类, 直接用BASE调用 ManagerBLL 中的方法。

 public class TestBLL : ManagerBLL
{
public static TestBLL __instance = null;
//原先有打算采用单列模式,结果悲剧啦!,后面只有整体修改下面这个方法。
public static TestBLL GetInstance()
{
if (__instance == null) __instance = new TestBLL();
return new TestBLL();
} public void test()
{
Admin_User admin=new Admin_User();
admin.User_Name="";
admin.User_NikeName="";
//条件
WhereClip<Admin_User> where = new WhereClip<Admin_User>(a => a.User_Name=="x" && a.User_ID == 1 || a.User_RegIP=="dd");
where.And(a=>a.User_Status==1);
where.Or(a=>a.User_TrueName=="");
//模糊条件
where.And(a=>a.User_Password.Like(String.Format("%{0}%",1)));
//列名
ColumnsClip<Admin_User> columnsClip=new ColumnsClip<Admin_User>(a=>a.Columns(a.User_ID,a.User_LastIP,a.User_LoginNumber));
//排序
OrderByClip<Admin_User> orderByClip=new OrderByClip<Admin_User>(a=>a.OrderBy(a.User_ID.Desc(),a.User_LastIP.Asc()));
//查询第一行第一列
base.GetCount(where);
//查询一行
base.Get(columnsClip, where, orderByClip);
//查询成List
base.GetList(0, columnsClip, where, orderByClip);
//分页查询
base.GetList(columnsClip, where, orderByClip, 10, 1, 0);
//添加
base.Add(admin);
//批量添加-事物
List<BaseEntity> list = new List<BaseEntity>();
Admin_User admins = (Admin_User)admin.Clone();//深度复制
list.Add(admin);
list.Add(admins);
base.AddList(list);
//修改
base.Edit(admin, where);
//批量修改-事物
Dictionary<BaseEntity, object> dic = new Dictionary<BaseEntity, object>();
dic.Add(admin, where);
dic.Add(admins, where);
base.EditList(dic);
//删除
base.Remove(admin);
//批量删除-事物
base.RemoveList(dic);
}
}

实体类:codesmith 脚本写好既可以自动生成实体,非常方便快捷。

 /// <summary>
/// Modal class: Admin_User.
/// </summary>
[Serializable]
[Table(Name = "Admin_User", PrimaryKey = "user_ID")]
public class Admin_User : BaseEntity
{
#region Private Properties private int? user_ID;//ID
private string user_Name;//用户名
private string user_Password;//密码
private string user_NikeName;//用户昵称
private string user_TrueName;//用户真实姓名
private string user_Email;//Email
private string user_RegIP;//注册IP
private DateTime? user_CreateTime;//注册时间
private int? user_LoginNumber;//登录次数
private DateTime? user_LastTime;//最后登录时间
private string user_LastIP;//最后登录IP
private int? user_Status;//状态 #endregion #region Public Properties /// <summary>
/// ID.
/// </summary>
[Column(Name = "user_ID", PrimaryKey = true, Strategy = GenerationType.INDENTITY)]
public int? User_ID
{
get
{
return user_ID;
}
set
{ user_ID = value;
}
} /// <summary>
/// 用户名.
/// </summary>
[Column(Name = "user_Name")]
public string User_Name
{
get
{
return user_Name;
}
set
{
user_Name = value.SubStr(50);
}
} /// <summary>
/// 密码.
/// </summary>
[Column(Name = "user_Password")]
public string User_Password
{
get
{
return user_Password;
}
set
{
user_Password = value.SubStr(50);
}
} /// <summary>
/// 用户昵称.
/// </summary>
[Column(Name = "user_NikeName")]
public string User_NikeName
{
get
{
return user_NikeName;
}
set
{
user_NikeName = value.SubStr(50);
}
} /// <summary>
/// 用户真实姓名.
/// </summary>
[Column(Name = "user_TrueName")]
public string User_TrueName
{
get
{
return user_TrueName;
}
set
{
user_TrueName = value.SubStr(50);
}
} /// <summary>
/// Email.
/// </summary>
[Column(Name = "user_Email")]
public string User_Email
{
get
{
return user_Email;
}
set
{
user_Email = value.SubStr(50);
}
} /// <summary>
/// 注册IP.
/// </summary>
[Column(Name = "user_RegIP")]
public string User_RegIP
{
get
{
return user_RegIP;
}
set
{
user_RegIP = value.SubStr(50);
}
} /// <summary>
/// 注册时间.
/// </summary>
[Column(Name = "user_CreateTime")]
public DateTime? User_CreateTime
{
get
{
return user_CreateTime;
}
set
{
user_CreateTime = value;
}
} /// <summary>
/// 登录次数.
/// </summary>
[Column(Name = "user_LoginNumber")]
public int? User_LoginNumber
{
get
{
return user_LoginNumber;
}
set
{
user_LoginNumber = value;
}
} /// <summary>
/// 最后登录时间.
/// </summary>
[Column(Name = "user_LastTime")]
public DateTime? User_LastTime
{
get
{
return user_LastTime;
}
set
{
user_LastTime = value;
}
} /// <summary>
/// 最后登录IP.
/// </summary>
[Column(Name = "user_LastIP")]
public string User_LastIP
{
get
{
return user_LastIP;
}
set
{
user_LastIP = value.SubStr(50);
}
} /// <summary>
/// 状态.
/// </summary>
[Column(Name = "user_Status")]
public int? User_Status
{
get
{
return user_Status;
}
set
{
user_Status = value;
}
} #endregion }

 EntityManager 接口

public interface EntityManager
{ T Get<T>(WhereClip<T> whereClip) where T : BaseEntity, new(); T Get<T>(ColumnsClip<T> columnsClip, WhereClip<T> whereClip, OrderByClip<T> orderByClip) where T : BaseEntity, new(); List<T> GetList<T>(int top, ColumnsClip<T> columnsClip, WhereClip<T> whereClip, OrderByClip<T> orderByClip) where T : BaseEntity, new(); int GetCount<T>(WhereClip<T> whereClip) where T : BaseEntity, new(); //分页查询
ConditionResult<T> GetList<T>(ColumnsClip<T> columnsClip, WhereClip<T> whereClip, OrderByClip<T> orderByClip, int nPageSize, int nPageIndex, int nTotalCount) where T : BaseEntity, new(); //新增
int Add<T>(T entity); //批量新增,采用事务
bool AddList<T>(List<T> entityList); //修改
int Edit<T>(T entity); //多条件修改
int Edit<T>(T entity, WhereClip<T> whereClip) where T : BaseEntity, new(); //批量修改,采用事物
bool EditList<T>(Dictionary<T, object> dicList) where T : BaseEntity, new(); //删除
int Remove<T>(T entity); //根据ID删除数据
int Remove<T>(WhereClip<T> whereClip) where T : BaseEntity, new(); //批量删除,采用事物
bool RemoveList<T>(Dictionary<T, object> dicList) where T : BaseEntity, new();
}

  ManagerBLL 核心

        /// <summary>
/// 获取一行数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="whereClip"></param>
/// <returns></returns>
///
public T Get<T>(WhereClip<T> whereClip) where T : BaseEntity, new()
{
//说明 性能问题还尚未考虑。
T _T = new T(); PropertyInfo[] properties = ReflectionUtils.GetProperties(_T.GetType()); string whereString = string.Empty; if (whereClip != null)
whereString = whereClip.WhereString; TableInfo tableInfo = DbEntityUtils.GetTableInfo(_T, DbOperateType.SELECT); string commandText = BuildSql.GetFristSql(0, tableInfo, whereString, string.Empty); IDataReader sdr = null;
if (whereClip != null)
{
DbParameter[] parms = SqlOpera.MakeInParams(whereClip.Table);
sdr = SqlOpera.ExecuteReader(CommandType.Text, commandText, parms);
}
else
sdr = SqlOpera.ExecuteReader(CommandType.Text, commandText); while (sdr.Read())
{
foreach (PropertyInfo property in properties)
{
//通过实体类属性名称获取Column自定义属性配置的映射列名
string name = tableInfo.PropToColumn[property.Name].ToString(); //通过获取的列名从dataReader中检索值,并赋给实体对象属性
ReflectionUtils.SetPropertyValue(_T, property, sdr[name]);
}
break;
}
sdr.Close();
return _T;
} /// <summary>
/// 获取一行数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="columnsClip"></param>
/// <param name="whereClip"></param>
/// <param name="orderByClip"></param>
/// <returns></returns>
public T Get<T>(ColumnsClip<T> columnsClip, WhereClip<T> whereClip, OrderByClip<T> orderByClip) where T : BaseEntity, new()
{
//说明 性能问题还尚未考虑。
T _T = new T();
string orderByWhere = string.Empty;
string columnsWhere = string.Empty;
string whereString = string.Empty;
PropertyInfo[] properties = ReflectionUtils.GetProperties(_T.GetType()); if (orderByClip != null)
orderByWhere = orderByClip.OrderByWhere; if (columnsClip != null)
columnsWhere = columnsClip.ColumnsWhere; if (whereClip != null)
whereString = whereClip.WhereString; TableInfo tableInfo = null;
if (columnsClip == null)
tableInfo = DbEntityUtils.GetTableInfo(_T, DbOperateType.SELECT); string tableName = DbEntityUtils.GetTableName(_T.GetType());//获取表名 string commandText = null; if (columnsClip != null)
commandText = BuildSql.GetFristSqlByWhere(tableName, columnsWhere, whereString, orderByWhere);
else
commandText = BuildSql.GetFristSql(0, tableInfo, whereString, orderByWhere); IDataReader sdr = null;
if (whereClip != null)
{
DbParameter[] parms = SqlOpera.MakeInParams(whereClip.Table);
sdr = SqlOpera.ExecuteReader(CommandType.Text, commandText, parms);
}
else
{
sdr = SqlOpera.ExecuteReader(CommandType.Text, commandText);
} while (sdr.Read())
{
foreach (PropertyInfo property in properties)
{
//通过实体类属性名称获取Column自定义属性配置的映射列名
string name = DbEntityUtils.GetColumnAttributeName(property); //通过获取的列名从dataReader中检索值,并赋给实体对象属性
for (int i = 0; i < sdr.FieldCount; i++)
{
if (sdr.GetName(i) == name)
{
ReflectionUtils.SetPropertyValue(_T, property, sdr[name]);
break;
}
}
}
break;
}
sdr.Close();
return _T;
} /// <summary>
/// 获取List集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="top"></param>
/// <param name="columnsClip"></param>
/// <param name="whereClip"></param>
/// <param name="orderByClip"></param>
/// <returns></returns>
public List<T> GetList<T>(int top, ColumnsClip<T> columnsClip, WhereClip<T> whereClip, OrderByClip<T> orderByClip) where T : BaseEntity, new()
{ //说明 性能问题还尚未考虑。
T _T = new T();
List<T> listT = new List<T>();
string orderByWhere = string.Empty;
string columnsWhere = string.Empty;
string whereString = string.Empty;
PropertyInfo[] properties = ReflectionUtils.GetProperties(_T.GetType()); if (orderByClip != null)
orderByWhere = orderByClip.OrderByWhere; if (columnsClip != null)
columnsWhere = columnsClip.ColumnsWhere; if (whereClip != null)
whereString = whereClip.WhereString; string commandText = null;
DataSet ds = null; if (columnsClip != null)
{
string tableName = DbEntityUtils.GetTableName(_T.GetType());//获取表名
commandText = BuildSql.GetListSql(top, tableName, columnsWhere, whereString, orderByWhere);
}
else
{
TableInfo tableInfo = DbEntityUtils.GetTableInfo(_T, DbOperateType.SELECT);
commandText = BuildSql.GetFristSql(top, tableInfo, whereString, orderByWhere);
} if (whereClip != null)
{
DbParameter[] parms = SqlOpera.MakeInParams(whereClip.Table);
ds = SqlOpera.ExecuteDataset(CommandType.Text, commandText, parms);
}
else
{
ds = SqlOpera.ExecuteDataset(CommandType.Text, commandText);
} if (ds.Tables.Count == 0) return listT;
foreach (DataRow dr in ds.Tables[0].Rows)
{
T entityT = new T();
foreach (PropertyInfo property in properties)
{
//通过实体类属性名称获取Column自定义属性配置的映射列名
string name = DbEntityUtils.GetColumnAttributeName(property); //通过获取的列名从datatable中检索值,并赋给实体对象属性 if (dr.Table.Columns.Contains(name))
ReflectionUtils.SetPropertyValue(entityT, property, dr[name]); }
listT.Add(entityT);
} return listT;
} /// <summary>
/// 分页
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="columnsClip"></param>
/// <param name="whereClip"></param>
/// <param name="orderByClip"></param>
/// <param name="nPageSize"></param>
/// <param name="nPageIndex"></param>
/// <param name="nTotalCount"></param>
/// <returns></returns>
public ConditionResult<T> GetList<T>(ColumnsClip<T> columnsClip, WhereClip<T> whereClip, OrderByClip<T> orderByClip, int nPageSize, int nPageIndex, int nTotalCount) where T : BaseEntity, new()
{ T _T = new T();
List<T> listT = new List<T>();
ConditionResult<T> conditionResult = new ConditionResult<T>();
string orderByWhere = string.Empty;
string columnsWhere = string.Empty;
string whereString = string.Empty;
int totalCount = 0;
PropertyInfo[] properties = ReflectionUtils.GetProperties(_T.GetType()); if (orderByClip != null)
orderByWhere = orderByClip.OrderByWhere; if (columnsClip != null)
columnsWhere = columnsClip.ColumnsWhere; if (whereClip != null)
whereString = whereClip.WhereString; totalCount = GetCount(whereClip); string commandText = null; DataSet ds = null; TableInfo tableInfo = DbEntityUtils.GetTableInfo(_T, DbOperateType.SELECT);
string tableName = DbEntityUtils.GetTableName(_T.GetType());//获取表名
commandText = BuildSql.GetPageSql(tableName, columnsWhere, whereString, orderByWhere, nPageSize, nPageIndex, tableInfo); if (whereClip != null)
{
DbParameter[] parms = SqlOpera.MakeInParams(whereClip.Table);
ds = SqlOpera.ExecuteDataset(CommandType.Text, commandText, parms);
}
else
{
ds = SqlOpera.ExecuteDataset(CommandType.Text, commandText);
} if (ds.Tables.Count == 0) return conditionResult; foreach (DataRow dr in ds.Tables[0].Rows)
{
T entityT = new T();
foreach (PropertyInfo property in properties)
{
//通过实体类属性名称获取Column自定义属性配置的映射列名
string name = DbEntityUtils.GetColumnAttributeName(property); //通过获取的列名从datatable中检索值,并赋给实体对象属性 if (dr.Table.Columns.Contains(name))
ReflectionUtils.SetPropertyValue(entityT, property, dr[name]); }
listT.Add(entityT);
} conditionResult.ResultList = listT;
conditionResult.PageSize = nPageSize;
conditionResult.TotalCount = totalCount;
conditionResult.TotalPage = (int)Math.Ceiling((double)totalCount / nPageSize);
conditionResult.PageIndex = nPageIndex;
return conditionResult;
} /// <summary>
/// 获取第一行第一列
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="whereClip"></param>
/// <returns></returns>
public int GetCount<T>(WhereClip<T> whereClip) where T : BaseEntity, new()
{
Type type = new T().GetType(); string whereString = string.Empty; if (whereClip != null)
whereString = whereClip.WhereString; string tableName = DbEntityUtils.GetTableName(type);//获取表名 string commandText = BuildSql.GetCountSql(tableName, whereString); ////执行SQL命令
object val = null;
if (whereClip != null)
{
DbParameter[] parms = SqlOpera.MakeInParams(whereClip.Table);
val = SqlOpera.ExecuteScalar(CommandType.Text, commandText, parms);
}
else
val = SqlOpera.ExecuteScalar(CommandType.Text, commandText); //返回所影响的行数
return Utils.StrToInt(val, 0);
}

ManagerBLL 必须实现 EntityManager 接口,部分代码还未贴。

------------------------------------------------------------------

整理中。。。。。

.NET ORM框架(一)的更多相关文章

  1. ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...

  2. 最好的5个Android ORM框架

    在开发Android应用时,保存数据有这么几个方式, 一个是本地保存,一个是放在后台(提供API接口),还有一个是放在开放云服务上(如 SyncAdapter 会是一个不错的选择). 对于第一种方式, ...

  3. [Android]Android端ORM框架——RapidORM(v2.1)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/6020412.html [Android]Android端ORM ...

  4. [Android]Android端ORM框架——RapidORM(v2.0)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5626716.html [Android]Android端ORM ...

  5. 轻量级ORM框架——第一篇:Dapper快速学习

    我们都知道ORM全称叫做Object Relationship Mapper,也就是可以用object来map我们的db,而且市面上的orm框架有很多,其中有一个框架 叫做dapper,而且被称为th ...

  6. ORM之殇,我们需要什么样的ORM框架?

    最近在研究ORM,究竟什么样的框架才是我们想要的 开发框架的意义在于 开发更标准,更统一,不会因为不同人写的代码不一样 开发效率更高,无需重新造轮子,重复无用的代码,同时简化开发流程 运行效率得到控制 ...

  7. 轻量级ORM框架初探-Dapper与PetaPoco的基本使用

    一.EntityFramework EF是传统的ORM框架,也是一个比较重量级的ORM框架.这里仍然使用EF的原因在于为了突出轻量级ORM框架的性能,所谓有对比才有更优的选择. 1.1 准备一张数据库 ...

  8. ORM框架示例及查询测试,上首页修改版(11种框架)

    继上次ORM之殇,我们需要什么样的ORM框架? 整理了11个ORM框架测试示例,通过示例代码和结果,能很容易了解各种框架的特性,优缺点,排名不分先后 EF PDF XCODE CRL NHiberna ...

  9. [Android]Android端ORM框架——RapidORM(v1.0)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4748077.html  Android上主流的ORM框架有很多 ...

  10. 吉特仓库管理系统-ORM框架的使用

    最近在园子里面连续看到几篇关于ORM的文章,其中有两个印象比较深刻<<SqliteSugar>>,另外一篇文章是<<我的开发框架之ORM框架>>, 第一 ...

随机推荐

  1. MSSQLServer基础03(数据检索(查询))

    执行备注中的代码创建测试数据表. 简单的数据检索 :SELECT * FROM Student 只检索需要的列 :SELECT sName FROM Student .ame FROM Student ...

  2. 120. Triangle

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  3. IIS7 发布mvc3.0

    Windows7系统和我们见面已经有一段时间了,在我们经过一段时间熟悉了她的新鲜好玩儿的功能之后,也许我们该静下心来想一下怎么用她做一些与学习有 关的事情,从Windows7的第一个试用版到现在的零售 ...

  4. Memcache+Cookie解决分布式系统共享登录状态------------------------------Why Memcached?

    每个用户请求向IIS发送一个请求,但IIS服务器的请求数有限,cpu支持的线程数有限,如果一秒钟向这台服务器发送10000次,那么则一般就会有问题,考虑集群, 请求数据分流,几台服务器共同对应一个公共 ...

  5. [原]数据库中的partitioning和sharding

    1. 如何理解定义 在中文中,partitioning和sharding都有分区的意思.从大的方面来说,这两个词所执行的动作确实也和分区相关.partitioning在很多场合是vertical pa ...

  6. Windbg调试命令详解(3)

    3 进程与线程 既可以显示进程和线程列表,又可以显示指定进程或线程的详细信息.调试命令可以提供比taskmgr更详尽的进程资料,在调试过程中不可或缺. 3.1 进程命令 进程命令包括这些内容:显示进程 ...

  7. 2013ACM省赛题目

    地址就贴这一个吧 都在附近 当时回来也没做做 一伤心了 二是当时实在太弱了 先补两道DP E题的区间DP dp[i][j]  截止到i位置以字母j为结束的上升序列 正序 逆序各来一遍 再循环一遍保存一 ...

  8. LCA与RMQ

    一.什么是LCA? LCA:Least Common Ancestors(最近公共祖先),对于一棵有根树T的任意两个节点u,v,求出LCA(T, u, v),即离跟最远的节点x,使得x同时是u和v的祖 ...

  9. I.MX6 Android USB Touch eGTouchA.ini文件存放

    /******************************************************************** * I.MX6 Android USB Touch eGTo ...

  10. Mysql slave 状态之Seconds_Behind_Master

    在MySQL的主从环境中,我们可以通过在slave上执行show slave status来查看slave的一些状态信息,其中有一个比较重要的参数Seconds_Behind_Master.那么你是否 ...