ado.net快速上手实践篇(二)
五、dal层数据访问实现
在这里我们使用前一篇文章里实现的数据持久化层和伪SqlMapper对象,实现数据操作。下面我们来看看Dal下核心的Dao如何实现:
还记得我们在IBatis.net下面的dao类是怎么实现的吗?没错,我们根据一个基类BaseDAO和它的构造函数,实现dao的配置加载。但是楼猪的实现没有那么复杂和强大,本文的实现其实就是通过BaseDAO和构造函数获取数据库连接对象的key,初始化一个SqlMapper,然后利用SqlMapper对象进行基本的CRUD等等数据操作。那么我们如何利用BaseDAO和构造函数就像以前在IBatis.net系列文章里的提到的Dal层下那样进行SqlMapper的初始化呢?
1、在AdoNetDataaccess.Mapper下我们定义公共的BaseDAO类
代码
namespace AdoNetDataAccess.Mapper
{
public abstract class BaseDAO
{
#region PRoperties
public SqlMapper SqlMapper { get; set; }
#endregion
#region Constructor
private BaseDAO()
{
}
/// <summary>
/// SqlMapper属性适用
/// </summary>
/// <param name="mapperName"></param>
public BaseDAO(string mapperName)
{
this.SqlMapper = MapperUtill.GetMapper(mapperName);
}
#endregion
}
}2、初始化SqlMapper的实用类
代码
using System;
using System.Collections.Generic;
using System.Configuration;
namespace AdoNetDataAccess.Mapper
{
using AdoNetDataAccess.Core.Contract;
using AdoNetDataAccess.Core.Implement;
public sealed class MapperUtill
{
#region fields
public static string currentSqlKey = "sqlConn";
public static int cmdTimeOut = 15;
private static readonly object objSync = new object();
private static readonly IDictionary<string, SqlMapper> dictMappers = new Dictionary<string, SqlMapper>();
#endregion
#region constructor and methods
private MapperUtill()
{
}
static MapperUtill()
{
try
{
cmdTimeOut = int.Parse(ConfigurationManager.AppSettings["db_timeOut"]);
}
catch
{
cmdTimeOut = 15;
}
//实例化SqlDbMapper
for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++)
{
string key = ConfigurationManager.ConnectionStrings[i].Name;
string value = ConfigurationManager.ConnectionStrings[i].ConnectionString;
CreateMapper(key, value, cmdTimeOut);
}
}
public static SqlMapper GetSqlMapper(string key)
{
return MapperUtill.GetMapper(key);
}
public static SqlMapper GetCurrentSqlMapper()
{
return MapperUtill.GetMapper(currentSqlKey);
}
public static void CreateMapper(string connKey, string sqlConStr, int connTimeOut)
{
IDbOperation operation = new SqlServer(sqlConStr, connTimeOut);
SqlMapper mapper = new SqlMapper(operation);
dictMappers.Add(connKey.ToUpper().Trim(), mapper);//不区分大小写
}
public static SqlMapper GetMapper(string sqlConKey)
{
if (string.IsNullOrEmpty(sqlConKey))
{
throw new Exception("数据库连接字符串主键为空!");
}
sqlConKey = sqlConKey.ToUpper();//不区分大小写
SqlMapper mapper = null;
if (dictMappers.ContainsKey(sqlConKey))
{
mapper = dictMappers[sqlConKey];
}
else
{
throw new Exception(string.Format("没有{0}所对应的数据库连接", sqlConKey));
}
return mapper;
}
/// <summary>
/// 释放所有
/// </summary>
public void Release()
{
foreach (KeyValuePair<string, SqlMapper> kv in dictMappers)
{
SqlMapper mapper = kv.Value;
if (mapper == null)
{
continue;
}
mapper.CurrentDbOperation.CloseConnection();
}
dictMappers.Clear();
}
#endregion
}
}
这个实用类的重要作用就是初始化配置文件里connectionStrings配置节点,以获取sql连接对象必须的连接字符串。3、PersonDao类
下面就是针对具体的Person表的数据操作了:
代码
using System.Collections.Generic;
using System.Data;
namespace AdoNetDataAccess.Dal.Dao
{
using AdoNetDataAccess.Dal.Model;
using AdoNetDataAccess.Dal.Utility;
using AdoNetDataAccess.Mapper;
public class PersonDao : BaseDAO
{
public PersonDao()
: base("sqlConn")//sqlConn是<connectionStrings>配置节点的一个name
{
}
public int Insert(string sqlInsert)
{
int id = this.SqlMapper.Insert(sqlInsert);
//object obj = this.SqlMapper.ExecuteScalar(sqlInsert, System.Data.CommandType.Text, null);
return id;
}
public bool BatchInsert(IList<Person> listModels)
{
int batchSize = 50000;
int copyTimeOut = 60;
DataTable dt = DataTableHelper.CreateTable<Person>(listModels);
bool flag = this.SqlMapper.BatchInsert(typeof(Person).Name, batchSize, copyTimeOut, dt);
return flag;
}
public int Update(string sqlUpdate)
{
int result = this.SqlMapper.Update(sqlUpdate);
return result;
}
public IList<Person> SelectPersons(string sqlSelect)
{
IList<Person> listPersons = this.SqlMapper.QueryForList<Person>(sqlSelect);
return listPersons;
}
public IDictionary<int, Person> SelectDictPersons(string sqlSelect)
{
IDictionary<int, Person> dictPersons = this.SqlMapper.QueryForDictionary<i
补充:Web开发 , ASP.Net ,