最近做项目自己整理了一个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. ecos内核概览--bakayi译

    http://blog.csdn.net/wangzaiwei2006/article/details/6453423

  2. netstat命令查看服务器运行情况

    netstat -n|grep 80出现大量time_wait 在运行netstat -n|grep 80 | awk '/^tcp/ {++S[$NF]} END {for(a in S) prin ...

  3. MapReduce编程系列 — 2:计算平均分

    1.项目名称: 2.程序代码: package com.averagescorecount; import java.io.IOException; import java.util.Iterator ...

  4. 8款替代Dreamweaver的开源网页开发工具

    Adobe Dreamweaver虽然非常好用,但它并不是唯一一个能够设计.开发.发布精彩网站的Web开发集成环境.我们的开源世界里有很多非常棒的可以完全替代Dreamweaver的各种功能的优秀We ...

  5. Java之跳出多重循环

    在java里,想要跳出多重循环,有两种方法 1.在循环语句前设置一个标记,然后使用带有该标记的break语句跳出该循环 public static void main(String args[]) { ...

  6. grunt安装中的cli和--save-dev解释

    grunt官网安装grunt的介绍中是这么写的: npm install -g grunt-cli 这里安装Grunt的命令行支持(command line interface,简称CLI),在这之后 ...

  7. poj2886Who Gets the Most Candies? (约瑟夫环)

    http://poj.org/problem?id=2886 单点更新 初始位置都是1 如果这个人出去 位置变为0 利用线段树求区间k值 k值的计算如下 如果这个数值是负的 那么下一个人的就是((k- ...

  8. SQL Server中Delete语句表名不能用别名

    delete from TABLEA A where A.FIELD1=10        (ORACLE适用)delete TABLEA from TABLEA A where A.FIELD1=1 ...

  9. ExecutorService与Executors例子的简单剖析

    对于多线程有了一点了解之后,那么来看看java.lang.concurrent包下面的一些东西.在此之前,我们运行一个线程都是显式调用了Thread的start()方法.我们用concurrent下面 ...

  10. vtiger 支持 物业收费功能 微信收费

    谁要?需要什么功能? 直接在下面留言,博主会整理大家的需求,形成产品,发出来.