文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>简单项目的简单数据库访问层封装(三)

简单项目的简单数据库访问层封装(三)

时间:2011-03-02  来源:菜鸟老了

1、使用工厂模式建立访问类:

SqlQueryHelperBuilder
 1  public class SqlQueryHelperBuilder<T> where T : DBHelperModel, new()
2 {
3 private string TabelName;
4
5 private string DataBaseEnginer = ConfigurationManager.AppSettings["SqlEngine"];
6 public SqlQueryHelperBuilder()
7 {
8 }
9 public SqlQueryHelperBuilder(string TabelName)
10 {
11 if(string.IsNullOrEmpty(TabelName))
12 throw new Exception("DataBase source tabel name request");
13 this.TabelName = TabelName;
14 }
15
16 public ISqlSimpleQueryHelper<T> GetSimpleQueryHelperInstance()
17 {
18 switch (DataBaseEnginer)
19 {
20 case "MySQL":
21 return new MySqlSimpleQueryHelper<T>(TabelName);
22 case "MSSQL":
23 break;
24 case "Oracle":
25 break;
26 default:
27 break;
28 }
29 throw new NullReferenceException("the ISqlSimpleQueryHeper instance is not initialized!");
30 }
31 }

2、接口定义:

IDBAccess.cs
 1   public interface IDBAccess<T> where T : DBHelperModel, new()
2 {
3 /// <summary>
4 /// 增加一条数据
5 /// </summary>
6 int AddModel(T model);
7
8 /// <summary>
9 /// 更新一条数据
10 /// </summary>
11 /// <param name="model"></param>
12 /// <returns></returns>
13 int UpdateModel(T model,string keyPropertyName);
14
15 /// <summary>
16 /// 删除一条数据
17 /// </summary>
18 int DeleteByKey(object keyValue, string keyPropertyName);
19
20 /// <summary>
21 /// 删除一些数据
22 /// </summary>
23 int DeleteWhere(string whereStr = null);
24
25 List<T> GetModelList(string whereStr = null);
26
27 List<T> GetModelList(out int count, string whereStr = null, int pageIndex = 1, int pageSize = 10, string orderColumn = null, string order = null);
28
29 T GetModelByKey(object keyValue, string keyPropertyName);
30
31 bool IsExist(object keyValue, string keyPropertyName);
32
33 int GetCount(string strWhere = null);
34 }

3.最后的访问

DBAccesser
    public class DBAccess<T> : IDAL.IDBAccess<T> where T : DBHelperModel, new()
{
private readonly ISqlSimpleQueryHelper<T> QueryHelper = null;
T model;
public DBAccess()
{
model
= new T();
QueryHelper
= new SqlQueryHelperBuilder<T>(model.TabelName).GetSimpleQueryHelperInstance();

}
public int AddModel(T model)
{
return QueryHelper.AddModel(model);
}

public int UpdateModel(T model, string keyPropertyName)
{
model.KeyPropertyName
= keyPropertyName;
return QueryHelper.UpdateModel(model, model.KeyPropertyColumnName);
}

public int DeleteByKey(object keyValue, string keyPropertyName)
{
model.KeyPropertyName
= keyPropertyName;
return QueryHelper.DeleteByParameter(model.KeyProperty.ParameterName, keyValue, model.KeyProperty.DbValueType);
}

public int DeleteWhere(string whereStr = null)
{
return QueryHelper.DeleteWhere(whereStr);
}

public List<T> GetModelList(string whereStr = null)
{
return QueryHelper.GetModelList(whereStr);
}

public List<T> GetModelList(out int count, string whereStr = null, int pageIndex = 1, int pageSize = 10, string orderColumn = null, string order = null)
{
return QueryHelper.GetPagedList(Count: out count, StrWhere: whereStr, PageIndex: pageIndex, PageSize: pageSize, Order: order, OrderColumn: orderColumn);
}

public T GetModelByKey(object keyValue, string keyPropertyName)
{
model.KeyPropertyName
= keyPropertyName;
return QueryHelper.GetModelByParameter(model.KeyProperty.ParameterName, keyValue, model.KeyProperty.DbValueType);
}

public bool IsExist(object keyValue, string keyPropertyName)
{
model.KeyPropertyName
= keyPropertyName;
return QueryHelper.IsExist(model.KeyProperty.ParameterName, keyValue, model.KeyProperty.DbValueType);
}

public int GetCount(string strWhere = null)
{
return QueryHelper.GetCount(strWhere);
}
}

例:

static void Main(string[] args)
{
ST.EPolice.IDAL.IDBAccess
<ST.EPolice.Model.Student> target = new ST.EPolice.DAL.DBAccess<ST.EPolice.Model.Student>();
ST.EPolice.Model.Student model
= new ST.EPolice.Model.Student { StudentId = "001", Name = "lcn" }; // TODO: 初始化为适当的值
Console.WriteLine(target.AddModel(model));

Console.WriteLine(target.GetModelByKey(model.StudentId,
"StudentId"));
Console.Read();
}
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载