前不久换了家新公司,面对新的web项目,我参照自己以前的一些资料,重新设计了一个通用底层框架,记下来留个纪念,
首先:引用新知识,微软企业库的底层连接类(三个DLL文件),很好用。DownLoad
步骤1、引用三个DLL文件
步骤2、修改配置文件
Code
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<!--设置默认连接默认数据库-->
<dataConfiguration defaultDatabase="ConnectionString"/>
</configuration>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<!--设置默认连接默认数据库-->
<dataConfiguration defaultDatabase="ConnectionString"/>
</configuration>
然后是实体类:
Code
using System.Collections;
namespace Model {
/// <summary>
/// [Category]领域类
/// </summary>
public class Category : Farther.Farther
{
/// <summary>
/// 自动增长编号
/// </summary>
private int m_Id;
/// <summary>
/// 栏目名称
/// </summary>
private string m_Cate_Name;
/// <summary>
/// 栏目等级
/// </summary>
private int m_Level;
/// <summary>
/// 栏目描述
/// </summary>
private string m_Desc;
/// <summary>
/// 父级编号(外键)
/// </summary>
private int m_Top_id;
/// <summary>
/// 自动增长编号
/// </summary>
public virtual int Id {
get {
return this.m_Id;
}
set {
this.m_Id = value;SetField("Id", value);
}
}
/// <summary>
/// 栏目名称
/// </summary>
public virtual string Cate_Name {
get {
return this.m_Cate_Name;
}
set {
this.m_Cate_Name = value; SetField("Cate_Name", value);
}
}
/// <summary>
/// 栏目等级
/// </summary>
public virtual int Level {
get {
return this.m_Level;
}
set {
this.m_Level = value; SetField("Level", value);
}
}
/// <summary>
/// 栏目描述
/// </summary>
public virtual string Desc {
get {
return this.m_Desc;
}
set {
this.m_Desc = value; SetField("Desc", value);
}
}
/// <summary>
/// 父级编号(外键)
/// </summary>
public virtual int Top_id {
get {
return this.m_Top_id;
}
set {
this.m_Top_id = value; SetField("Top_id", value);
}
}
/// <summary>
/// 抽象方法 获得某表的表名、主键值
/// 实体类必须重写它
/// </summary>
/// <returns></returns>
public override Hashtable GetTableField()
{
Hashtable hash = new Hashtable();
hash.Add("Id", this.m_Id);
hash.Add("Tname", "Category");
return hash;
}
}
}
using System.Collections;
namespace Model {
/// <summary>
/// [Category]领域类
/// </summary>
public class Category : Farther.Farther
{
/// <summary>
/// 自动增长编号
/// </summary>
private int m_Id;
/// <summary>
/// 栏目名称
/// </summary>
private string m_Cate_Name;
/// <summary>
/// 栏目等级
/// </summary>
private int m_Level;
/// <summary>
/// 栏目描述
/// </summary>
private string m_Desc;
/// <summary>
/// 父级编号(外键)
/// </summary>
private int m_Top_id;
/// <summary>
/// 自动增长编号
/// </summary>
public virtual int Id {
get {
return this.m_Id;
}
set {
this.m_Id = value;SetField("Id", value);
}
}
/// <summary>
/// 栏目名称
/// </summary>
public virtual string Cate_Name {
get {
return this.m_Cate_Name;
}
set {
this.m_Cate_Name = value; SetField("Cate_Name", value);
}
}
/// <summary>
/// 栏目等级
/// </summary>
public virtual int Level {
get {
return this.m_Level;
}
set {
this.m_Level = value; SetField("Level", value);
}
}
/// <summary>
/// 栏目描述
/// </summary>
public virtual string Desc {
get {
return this.m_Desc;
}
set {
this.m_Desc = value; SetField("Desc", value);
}
}
/// <summary>
/// 父级编号(外键)
/// </summary>
public virtual int Top_id {
get {
return this.m_Top_id;
}
set {
this.m_Top_id = value; SetField("Top_id", value);
}
}
/// <summary>
/// 抽象方法 获得某表的表名、主键值
/// 实体类必须重写它
/// </summary>
/// <returns></returns>
public override Hashtable GetTableField()
{
Hashtable hash = new Hashtable();
hash.Add("Id", this.m_Id);
hash.Add("Tname", "Category");
return hash;
}
}
}
实体类需继承实体类父类:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Model.Farther
{
public abstract class Farther
{
//Ilist 存储所有 Hashtable , 即存储实体类保存的记录数
IList list = new ArrayList();
/// <summary>
/// 设置集合的值
/// </summary>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
protected void SetField(string name, object value)
{
if (!isExists(name))
{
Hashtable hash = new Hashtable();
hash.Add("key", name);
hash.Add("value", value);
list.Add(hash);
}
}
/// <summary>
/// 是否已添加字段
/// </summary>
/// <param name="name">字段名</param>
/// <returns></returns>
public bool isExists(string name)
{
bool b = false;
for (int i = 0; i < list.Count; i++)
{
Hashtable hash = list[i] as Hashtable;
if (hash["key"].Equals(name))
{
b = true;
break;
}
}
return b;
}
/// <summary>
/// 获取集合长度
/// </summary>
/// <returns></returns>
public int GetCount()
{
return list.Count;
}
/// <summary>
/// 获取集合索引字段名
/// </summary>
/// <param name="index">索引</param>
/// <returns></returns>
public object GetKey(int index)
{
Hashtable hash = list[index] as Hashtable;
if (hash != null)
return hash["key"];
return null;
}
/// <summary>
/// 获取集合索引字段值
/// </summary>
/// <param name="index">索引</param>
/// <returns></returns>
public object GetValue(int index)
{
Hashtable hash = list[index] as Hashtable;
if (hash != null)
return hash["value"];
return null;
}
/// <summary>
/// 抽象方法 获得某表的表名、主键值,用 Hashtable 保存, 实体类必须重写它
/// </summary>
/// <returns></returns>
public abstract Hashtable GetTableField();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Model.Farther
{
public abstract class Farther
{
//Ilist 存储所有 Hashtable , 即存储实体类保存的记录数
IList list = new ArrayList();
/// <summary>
/// 设置集合的值
/// </summary>
/// <param name="name">字段名</param>
/// <param name="value">字段值</param>
protected void SetField(string name, object value)
{
if (!isExists(name))
{
Hashtable hash = new Hashtable();
hash.Add("key", name);
hash.Add("value", value);
list.Add(hash);
}
}
/// <summary>
/// 是否已添加字段
/// </summary>
/// <param name="name">字段名</param>
/// <returns></returns>
public bool isExists(string name)
{
bool b = false;
for (int i = 0; i < list.Count; i++)
{
Hashtable hash = list[i] as Hashtable;
if (hash["key"].Equals(name))
{
b = true;
break;
}
}
return b;
}
/// <summary>
/// 获取集合长度
/// </summary>
/// <returns></returns>
public int GetCount()
{
return list.Count;
}
/// <summary>
/// 获取集合索引字段名
/// </summary>
/// <param name="index">索引</param>
/// <returns></returns>
public object GetKey(int index)
{
Hashtable hash = list[index] as Hashtable;
if (hash != null)
return hash["key"];
return null;
}
/// <summary>
/// 获取集合索引字段值
/// </summary>
/// <param name="index">索引</param>
/// <returns></returns>
public object GetValue(int index)
{
Hashtable hash = list[index] as Hashtable;
if (hash != null)
return hash["value"];
return null;
}
/// <summary>
/// 抽象方法 获得某表的表名、主键值,用 Hashtable 保存, 实体类必须重写它
/// </summary>
/// <returns></returns>
public abstract Hashtable GetTableField();
}
}
再来是通用业务层:(结合了Ke_Pager分页控件)
Code
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Collections;
using Model.Farther;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Common;
namespace DAL.DataHelp
{
public class DataHelp:IDAL.DataHelp.IDataHelp
{
Database db = DatabaseFactory.CreateDatabase();
#region 添加记录
/// <summary>
/// 添加一条记录
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns></returns>
public int Insert(Farther far)
{
Hashtable hash = far.GetTableField();
//不允许添加Id
int id = int.Parse(hash["Id"].ToString());
if (id > 0)
throw new Exception("编号赋值无效!");
if (far.GetCount() == 0)
throw new Exception("缺少添加选项!");
try
{
string sql = string.Format("insert into {0}({1}) values({2});select @@identity;",
hash["Tname"], GetCondition(far, Condition.Key), GetCondition(far, Condition.Value));
//执行
object o = db.ExecuteScalar(GetDbCommand(far,sql));
return int.Parse(o.ToString());
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 删除记录
/// <summary>
/// 根据条件删除记录,可以含多个条件
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns></returns>
public int Delete(Farther far)
{
//必须含删除条件
if (far.GetCount() == 0)
throw new Exception("缺少删除条件!");
try
{
Hashtable hash = far.GetTableField();
string sql = string.Format("delete {0} where 1=1 {1}", hash["Tname"], GetWhere(far));
int result = db.ExecuteNonQuery(GetDbCommand(far, sql));
return result;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 修改记录
/// <summary>
/// 根据编号修改一条记录
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns></returns>
public int Update(Farther far)
{
Hashtable hash = far.GetTableField();
//必须输入修改的行Id
int id = int.Parse(hash["Id"].ToString());
if (id == 0)
throw new Exception("缺少要修改的记录编号!");
try
{
string sql = string.Format("update {0} set {1} where Id = {2}", hash["Tname"], GetCondition(far, Condition.All), hash["Id"]);
int result = db.ExecuteNonQuery(GetDbCommand(far, sql));
return result;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 查询记录
/// <summary>
/// 查询所有记录,或者根据条件查询记录
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns>DataTable</returns>
public DataTable SelectAllOrByField(Farther far)
{
try
{
Hashtable hash = far.GetTableField();
string sql = string.Format("select * from {0} where 1=1 {1} order by id desc", hash["Tname"], GetWhere(far));
DataSet ds = db.ExecuteDataSet(GetDbCommand(far, sql));
if (ds != null)
return ds.Tables[0];
return null;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 分页查询所有记录,或者根据条件查询记录
/// </summary>
/// <param name="far">实体类父类,包含字段条件等</param>
/// <param name="PageStart">结果集开始行</param>
/// <param name="PageEnd">结果集结束行</param>
/// <returns></returns>
public DataTable SelectPagerAllOrByField(Farther far, int PageStart, int PageEnd)
{
try
{
Hashtable hash = far.GetTableField();
string sql = string.Format("select * from (select row_number() over(order by id desc)as Row,* from {0} where 1=1 {1})temp where Row between {2} and {3} order by id desc",
hash["Tname"], GetWhere(far), PageStart, PageEnd);
DataSet ds = db.ExecuteDataSet(GetDbCommand(far,sql));
if (ds != null)
return ds.Tables[0];
return null;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 事务处理多个操作
/// <summary>
/// 事务处理多个操作
/// </summary>
/// <param name="list">实体类对象和操作类型集合</param>
/// <returns></returns>
public bool TranOperate(IList list)
{
DbConnection dbconn = null;
DbTransaction dbtran = null;
try
{
dbconn = db.CreateConnection(); //连接对象
dbconn.Open(); //打开连接
dbtran = dbconn.BeginTransaction(); //开启事务
Hashtable hashList = null; //集合
Farther far = null; //实体类父类对象
Hashtable hash = null; //实体类父类保存的值
for (int i = 0; i < list.Count; i++)
{
hashList = list[i] as Hashtable;
far = hashList["Farther"] as Farther;
Operation type = (Operation)hashList["Type"];
hash = far.GetTableField();
switch (type)
{
case Operation.Insert: //增
//不允许添加Id
int id = int.Parse(hash["Id"].ToString());
if (id > 0)
throw new Exception("编号赋值无效!");
string sql = string.Format("insert into {0}({1}) values({2});select @@identity;",
hash["Tname"], GetCondition(far, Condition.Key), GetCondition(far, Condition.Value));
db.ExecuteScalar(GetDbCommand(far, sql));
break;
case Operation.Delete: //删
//必须含删除条件
if (far.GetCount() == 0)
throw new Exception("缺少删除条件!");
string sql2 = string.Format("delete {0} where 1=1 {1}", hash["Tname"], GetWhere(far));
db.ExecuteNonQuery(GetDbCommand(far, sql2));
break;
case Operation.Update: //修改
//必须输入修改的行Id
int id1 = int.Parse(hash["Id"].ToString());
if (id1 == 0)
throw new Exception("缺少要修改的记录编号!");
string sql3 = string.Format("update {0} set {1} where Id = {2}", hash["Tname"], GetCondition(far, Condition.All), hash["Id"]);
db.ExecuteNonQuery(GetDbCommand(far, sql3));
break;
}
}
dbtran.Commit();
dbconn.Close();
return true;
}
catch (Exception ex)
{
if (dbconn == null)
throw new ArgumentNullException("dbcon");
if (dbtran == null)
throw new ArgumentNullException("dbtran");
dbtran.Rollback();
dbconn.Close();
throw new Exception(ex.Message);
}
finally {
dbconn.Close();
}
}
#endregion
#region 公用方法
/// <summary>
/// 构建参数化DBCommand命令对象
/// </summary>
/// <param name="far"></param>
/// <param name="sql"></param>
/// <returns></returns>
private DbCommand GetDbCommand(Farther far, string sql)
{
DbCommand dbcom = db.GetSqlStringCommand(sql);
for (int i = 0; i < far.GetCount(); i++)
dbcom.Parameters.Add(new SqlParameter("@" + far.GetKey(i), far.GetValue(i)));
return dbcom;
}
/// <summary>
/// 根据实体类父类获取WHERE条件
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns></returns>
private string GetWhere(Farther far)
{
string where = "";
for (int i = 0; i < far.GetCount(); i++)
{
where += string.Format(" and [{0}]=@{1} ", far.GetKey(i), far.GetKey(i));
}
return where;
}
/// <summary>
/// 根据实体类父类获取SET条件
/// </summary>
/// <param name="far">实体类父类</param>
/// <param name="con">枚举(条件类型)</param>
/// <returns></returns>
private string GetCondition(Farther far, Condition con)
{
string condition = "";
for (int i = 0; i < far.GetCount(); i++)
{
//过滤ID项
if (far.GetKey(i).ToString() != "Id")
{
switch (con)
{
case Condition.Key:
condition += string.Format(" [{0}],", far.GetKey(i));
break;
case Condition.Value:
condition += string.Format(" @{0},", far.GetKey(i));
break;
case Condition.All:
condition += string.Format(" [{0}]=@{1},", far.GetKey(i), far.GetKey(i));
break;
}
}
}
if (condition.Length > 0)
condition = condition.Substring(0, condition.Length - 1);
return condition;
}
/// <summary>
/// 条件类型(Key表示键,Value表示值, All表示集合),以参数形式表示
/// 如Key: name,value \ Value: @name,@value \ All: name=@name,value=@value
/// </summary>
private enum Condition
{
/// <summary>
/// Key表示键,如:name,value
/// </summary>
Key,
/// <summary>
/// Value表示值,如:@name,@value
/// </summary>
Value,
/// <summary>
/// All表示集合,如:name=@name,value=@value
/// </summary>
All
}
#endregion
}
/// <summary>
/// 操作类型,(增、删、改)
/// </summary>
public enum Operation
{
Insert = 1,
Delete = 2,
Update = 3
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Collections;
using Model.Farther;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Common;
namespace DAL.DataHelp
{
public class DataHelp:IDAL.DataHelp.IDataHelp
{
Database db = DatabaseFactory.CreateDatabase();
#region 添加记录
/// <summary>
/// 添加一条记录
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns></returns>
public int Insert(Farther far)
{
Hashtable hash = far.GetTableField();
//不允许添加Id
int id = int.Parse(hash["Id"].ToString());
if (id > 0)
throw new Exception("编号赋值无效!");
if (far.GetCount() == 0)
throw new Exception("缺少添加选项!");
try
{
string sql = string.Format("insert into {0}({1}) values({2});select @@identity;",
hash["Tname"], GetCondition(far, Condition.Key), GetCondition(far, Condition.Value));
//执行
object o = db.ExecuteScalar(GetDbCommand(far,sql));
return int.Parse(o.ToString());
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 删除记录
/// <summary>
/// 根据条件删除记录,可以含多个条件
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns></returns>
public int Delete(Farther far)
{
//必须含删除条件
if (far.GetCount() == 0)
throw new Exception("缺少删除条件!");
try
{
Hashtable hash = far.GetTableField();
string sql = string.Format("delete {0} where 1=1 {1}", hash["Tname"], GetWhere(far));
int result = db.ExecuteNonQuery(GetDbCommand(far, sql));
return result;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 修改记录
/// <summary>
/// 根据编号修改一条记录
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns></returns>
public int Update(Farther far)
{
Hashtable hash = far.GetTableField();
//必须输入修改的行Id
int id = int.Parse(hash["Id"].ToString());
if (id == 0)
throw new Exception("缺少要修改的记录编号!");
try
{
string sql = string.Format("update {0} set {1} where Id = {2}", hash["Tname"], GetCondition(far, Condition.All), hash["Id"]);
int result = db.ExecuteNonQuery(GetDbCommand(far, sql));
return result;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 查询记录
/// <summary>
/// 查询所有记录,或者根据条件查询记录
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns>DataTable</returns>
public DataTable SelectAllOrByField(Farther far)
{
try
{
Hashtable hash = far.GetTableField();
string sql = string.Format("select * from {0} where 1=1 {1} order by id desc", hash["Tname"], GetWhere(far));
DataSet ds = db.ExecuteDataSet(GetDbCommand(far, sql));
if (ds != null)
return ds.Tables[0];
return null;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 分页查询所有记录,或者根据条件查询记录
/// </summary>
/// <param name="far">实体类父类,包含字段条件等</param>
/// <param name="PageStart">结果集开始行</param>
/// <param name="PageEnd">结果集结束行</param>
/// <returns></returns>
public DataTable SelectPagerAllOrByField(Farther far, int PageStart, int PageEnd)
{
try
{
Hashtable hash = far.GetTableField();
string sql = string.Format("select * from (select row_number() over(order by id desc)as Row,* from {0} where 1=1 {1})temp where Row between {2} and {3} order by id desc",
hash["Tname"], GetWhere(far), PageStart, PageEnd);
DataSet ds = db.ExecuteDataSet(GetDbCommand(far,sql));
if (ds != null)
return ds.Tables[0];
return null;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 事务处理多个操作
/// <summary>
/// 事务处理多个操作
/// </summary>
/// <param name="list">实体类对象和操作类型集合</param>
/// <returns></returns>
public bool TranOperate(IList list)
{
DbConnection dbconn = null;
DbTransaction dbtran = null;
try
{
dbconn = db.CreateConnection(); //连接对象
dbconn.Open(); //打开连接
dbtran = dbconn.BeginTransaction(); //开启事务
Hashtable hashList = null; //集合
Farther far = null; //实体类父类对象
Hashtable hash = null; //实体类父类保存的值
for (int i = 0; i < list.Count; i++)
{
hashList = list[i] as Hashtable;
far = hashList["Farther"] as Farther;
Operation type = (Operation)hashList["Type"];
hash = far.GetTableField();
switch (type)
{
case Operation.Insert: //增
//不允许添加Id
int id = int.Parse(hash["Id"].ToString());
if (id > 0)
throw new Exception("编号赋值无效!");
string sql = string.Format("insert into {0}({1}) values({2});select @@identity;",
hash["Tname"], GetCondition(far, Condition.Key), GetCondition(far, Condition.Value));
db.ExecuteScalar(GetDbCommand(far, sql));
break;
case Operation.Delete: //删
//必须含删除条件
if (far.GetCount() == 0)
throw new Exception("缺少删除条件!");
string sql2 = string.Format("delete {0} where 1=1 {1}", hash["Tname"], GetWhere(far));
db.ExecuteNonQuery(GetDbCommand(far, sql2));
break;
case Operation.Update: //修改
//必须输入修改的行Id
int id1 = int.Parse(hash["Id"].ToString());
if (id1 == 0)
throw new Exception("缺少要修改的记录编号!");
string sql3 = string.Format("update {0} set {1} where Id = {2}", hash["Tname"], GetCondition(far, Condition.All), hash["Id"]);
db.ExecuteNonQuery(GetDbCommand(far, sql3));
break;
}
}
dbtran.Commit();
dbconn.Close();
return true;
}
catch (Exception ex)
{
if (dbconn == null)
throw new ArgumentNullException("dbcon");
if (dbtran == null)
throw new ArgumentNullException("dbtran");
dbtran.Rollback();
dbconn.Close();
throw new Exception(ex.Message);
}
finally {
dbconn.Close();
}
}
#endregion
#region 公用方法
/// <summary>
/// 构建参数化DBCommand命令对象
/// </summary>
/// <param name="far"></param>
/// <param name="sql"></param>
/// <returns></returns>
private DbCommand GetDbCommand(Farther far, string sql)
{
DbCommand dbcom = db.GetSqlStringCommand(sql);
for (int i = 0; i < far.GetCount(); i++)
dbcom.Parameters.Add(new SqlParameter("@" + far.GetKey(i), far.GetValue(i)));
return dbcom;
}
/// <summary>
/// 根据实体类父类获取WHERE条件
/// </summary>
/// <param name="far">实体类父类</param>
/// <returns></returns>
private string GetWhere(Farther far)
{
string where = "";
for (int i = 0; i < far.GetCount(); i++)
{
where += string.Format(" and [{0}]=@{1} ", far.GetKey(i), far.GetKey(i));
}
return where;
}
/// <summary>
/// 根据实体类父类获取SET条件
/// </summary>
/// <param name="far">实体类父类</param>
/// <param name="con">枚举(条件类型)</param>
/// <returns></returns>
private string GetCondition(Farther far, Condition con)
{
string condition = "";
for (int i = 0; i < far.GetCount(); i++)
{
//过滤ID项
if (far.GetKey(i).ToString() != "Id")
{
switch (con)
{
case Condition.Key:
condition += string.Format(" [{0}],", far.GetKey(i));
break;
case Condition.Value:
condition += string.Format(" @{0},", far.GetKey(i));
break;
case Condition.All:
condition += string.Format(" [{0}]=@{1},", far.GetKey(i), far.GetKey(i));
break;
}
}
}
if (condition.Length > 0)
condition = condition.Substring(0, condition.Length - 1);
return condition;
}
/// <summary>
/// 条件类型(Key表示键,Value表示值, All表示集合),以参数形式表示
/// 如Key: name,value \ Value: @name,@value \ All: name=@name,value=@value
/// </summary>
private enum Condition
{
/// <summary>
/// Key表示键,如:name,value
/// </summary>
Key,
/// <summary>
/// Value表示值,如:@name,@value
/// </summary>
Value,
/// <summary>
/// All表示集合,如:name=@name,value=@value
/// </summary>
All
}
#endregion
}
/// <summary>
/// 操作类型,(增、删、改)
/// </summary>
public enum Operation
{
Insert = 1,
Delete = 2,
Update = 3
}
}
最后就是接口层(略)和工厂类了
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using IDAL;
namespace Factory
{
/// <summary>
/// factory class , create class DAL object, return Interface IDAL object
/// </summary>
public class Factory
{
#region 获取接口对象
/// <summary>
/// 获取DataHelp的口对象,含通用增、删、改、查方法
/// </summary>
/// <returns></returns>
public static IDAL.DataHelp.IDataHelp GetIDataHelp()
{
return CreateAssembly("DAL.DataHelp.DataHelp") as IDAL.DataHelp.IDataHelp;
}
#endregion
/// <summary>
/// 根据命名空间.类名,创建程序集制定的对象
/// </summary>
/// <param name="ClassName"></param>
/// <returns></returns>
private static object CreateAssembly(string ClassName) {
return Assembly.Load("DAL").CreateInstance(ClassName);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using IDAL;
namespace Factory
{
/// <summary>
/// factory class , create class DAL object, return Interface IDAL object
/// </summary>
public class Factory
{
#region 获取接口对象
/// <summary>
/// 获取DataHelp的口对象,含通用增、删、改、查方法
/// </summary>
/// <returns></returns>
public static IDAL.DataHelp.IDataHelp GetIDataHelp()
{
return CreateAssembly("DAL.DataHelp.DataHelp") as IDAL.DataHelp.IDataHelp;
}
#endregion
/// <summary>
/// 根据命名空间.类名,创建程序集制定的对象
/// </summary>
/// <param name="ClassName"></param>
/// <returns></returns>
private static object CreateAssembly(string ClassName) {
return Assembly.Load("DAL").CreateInstance(ClassName);
}
}
}