- 实体基类:BaseDomain
- 持久层:IAppDao和AppDao
- 服务层:IXYZService和XYZService
本文内容涉及到了Castle.Facilities,Castle.IOC以及IBatisNet.DataMapper,IBatisNet.DataMapper是通过Castle来管理的,下面我介绍一下该方案的基础编码、配置以及应用:
一、基础编码:
- 实体基类:BaseDomain
[Serializable]
public abstract class BaseDomain
{
private int id;
private DateTime created_at;
private DateTime updated_at;
public int Id
{
get { return id; }
set { id = value; }
}
public DateTime Created_at
{
get { return created_at; }
set { created_at = value; }
}
public DateTime Updated_at
{
get { return updated_at; }
set { updated_at = value; }
}
public BaseDomain()
{
created_at = DateTime.Now;
updated_at = DateTime.Parse("1759-1-1");
}
private Exception ex;
/// <summary>
/// 异常
/// </summary>
public Exception Ex
{
get { return ex; }
set { ex = value; }
}
/// <summary>
/// 抛出异常
/// </summary>
/// <returns></returns>
public Exception Throw()
{
if (ex != null)
{
throw new DomainException(ex.Message);
}
return ex;
}
/// <summary>
/// 异常信息
/// </summary>
public string ExceptionMessage
{
get
{
if (ex != null)
{
return ex.Message;
}
else
{
return null;
}
}
}
/// <summary>
/// 验证
/// </summary>
/// <returns></returns>
public virtual bool Validate()
{
throw new NotImplementedException();
}
}
2、持久层:IAppDao和AppDao IAppDao代码:
public interface IAppDao<T>
{
/// <summary>
/// 创建对象
/// </summary>
/// <param name="obj">要创建对象</param>
void Create(T obj);
/// <summary>
/// 创建对象
/// </summary>
/// <param name="objs">对象集合</param>
void Create(Hashtable objs);
/// <summary>
/// 更新对象
/// </summary>
/// <param name="obj">要更新的对象</param>
void Update(T obj);
/// <summary>
/// 删除对象
/// </summary>
/// <param name="id">对象键值</param>
void DeleteById(object id);
/// <summary>
/// 获取对象
/// </summary>
/// <param name="id">对象主健</param>
/// <returns>对象信息</returns>
T GetById(object id);
/// <summary>
/// 搜索并返回一个对象
/// </summary>
/// <param name="conditions">搜索条件</param>
/// <returns>一个对象</returns>
T SearchOne(Hashtable conditions);
/// <summary>
/// 搜索并返回一组对象
/// </summary>
/// <param name="conditions">搜索条件</param>
/// <returns>一组对象</returns>
IList<T> SearchList(Hashtable conditions);
/// <summary>
/// 搜索并返回一组对象
/// </summary>
/// <param name="conditions">搜索条件</param>
/// <param name="pageno">页索引从1开始</param>
/// <param name="pagesize">页大小</param>
/// <returns>一组对象</returns>
IList<T> SearchList(Hashtable conditions, int pageno, int pagesize);
/// <summary>
/// 搜索并返回对象数量
/// </summary>
/// <param name="conditions">搜索条件</param>
/// <returns>对象数量</returns>
int SearchCount(Hashtable conditions);
/// <summary>
/// 搜索并返回一个对象
/// </summary>
/// <param name="statement">XML映射声明ID</param>
/// <param name="conditions">搜索条件</param>
/// <returns>一个对象</returns>
T SearchOne(string statement, Hashtable conditions);
/// <summary>
/// 搜索并返回一组对象
/// </summary>
/// <param name="statement">XML映射声明</param>
/// <param name="conditions">搜索条件</param>
/// <returns>一组对象</returns>
IList<T> SearchList(string statement, Hashtable conditions);
/// <summary>
/// 搜索并返回一组对象
/// </summary>
/// <param name="statement">XML映射声明</param>
/// <param name="conditions">搜索条件</param>
/// <param name="pageno">页索引从1开始</param>
/// <param name="pagesize">页大小</param>
/// <returns>一组对象</returns>
IList<T> SearchList(string statement, Hashtable conditions, int pageno, int pagesize);
/// <summary>
/// 搜索并返回对象数量
/// </summary>
/// <param name="statement">XML映射声明</param>
/// <param name="conditions">搜索条件</param>
/// <returns>对象数量</returns>
int SearchCount(string statement, Hashtable conditions);
}
AppDao代码:
public class AppDao<T> : IDao, IAppDao<T>
{
private const string DAO_STATEMENT_CREATE = "{0}.Domain.{1}.create";
private const string DAO_STATEMENT_DELETE = "{0}.Domain.{1}.deletebyid";
private const string DAO_STATEMENT_UPDATE = "{0}.Domain.{1}.update";
private const string DAO_STATEMENT_GETBYID = "{0}.Domain.{1}.getbyid";
private const string DAO_STATEMENT_SEARCHLIST = "{0}.Domain.{1}.searchlist";
private const string DAO_STATEMENT_SEARCHONE = "{0}.Domain.{1}.searchone";
private const string DAO_STATEMENT_SEARCH_COUNT = "{0}.Domain.{1}.searchcount";
protected ISqlMapper _sqlMap;
public ISqlMapper SqlMap
{
get { return _sqlMap; }
set { _sqlMap = value; }
}
private static string AssemblyName
{
get { return typeof (T).Assembly.GetName().Name; }
}
private static string ClassName
{
get { return typeof (T).Name; }
}
#region IAppDao<T> 成员
public void Create(T obj)
{
try
{
_sqlMap.Insert(string.Format(DAO_STATEMENT_CREATE, AssemblyName, ClassName), obj);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_CREATE, e);
}
}
public void Create(Hashtable objs)
{
try
{
_sqlMap.Insert(string.Format(DAO_STATEMENT_CREATE, AssemblyName, ClassName), objs);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_CREATE, e);
}
}
public void Update(T obj)
{
try
{
_sqlMap.Update(string.Format(DAO_STATEMENT_UPDATE, AssemblyName, ClassName), obj);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_UPDATE, e);
}
}
public void DeleteById(object id)
{
try
{
_sqlMap.Delete(string.Format(DAO_STATEMENT_DELETE, AssemblyName, ClassName), id);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_DELETE, e);
}
}
public T GetById(object id)
{
try
{
return _sqlMap.QueryForObject<T>(string.Format(DAO_STATEMENT_GETBYID, AssemblyName, ClassName), id);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_GETBYID, e);
}
}
public T SearchOne(Hashtable conditions)
{
try
{
return
_sqlMap.QueryForObject<T>(string.Format(DAO_STATEMENT_SEARCHONE, AssemblyName, ClassName),
conditions);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_SEARCHONE, e);
}
}
public IList<T> SearchList(Hashtable conditions)
{
try
{
return
_sqlMap.QueryForList<T>(string.Format(DAO_STATEMENT_SEARCHLIST, AssemblyName, ClassName), conditions);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_SEARCHLIST, e);
}
}
public IList<T> SearchList(Hashtable conditions, int pageno, int pagesize)
{
try
{
if (pageno == 0)
pageno = 1;
return
_sqlMap.QueryForList<T>(string.Format(DAO_STATEMENT_SEARCHLIST, AssemblyName, ClassName), conditions,
(pageno - 1)*pagesize, pagesize);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_SEARCHLIST, e);
}
}
public int SearchCount(Hashtable conditions)
{
try
{
return
_sqlMap.QueryForObject<int>(string.Format(DAO_STATEMENT_SEARCH_COUNT, AssemblyName, ClassName),
conditions);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_SEARCH_COUNT, e);
}
}
public T SearchOne(string statement, Hashtable conditions)
{
try
{
return _sqlMap.QueryForObject<T>(statement, conditions);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_SEARCHONE, e);
}
}
public IList<T> SearchList(string statement, Hashtable conditions)
{
try
{
return _sqlMap.QueryForList<T>(statement, conditions);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_SEARCHLIST, e);
}
}
public IList<T> SearchList(string statement, Hashtable conditions, int pageno, int pagesize)
{
try
{
if (pageno == 0)
pageno = 1;
return _sqlMap.QueryForList<T>(statement, conditions, (pageno - 1)*pagesize, pagesize);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_SEARCHLIST, e);
}
}
public int SearchCount(string statement, Hashtable conditions)
{
try
{
return _sqlMap.QueryForObject<int>(statement, conditions);
}
catch (Exception e)
{
throw new DaoException(DAO_STATEMENT_SEARCH_COUNT, e);
}
}
#endregion
}
该类中使用包含了IBatis声明id命名规则,在编写映射文件时请注意该命名规则;
原则上,Dao层不包含任何业务逻辑,对实体类也不验证其有效性,只负责实体类的CURD操作;
3、服务层:该层主要的工作:
- 验证实体类是否有效;
- 实现实体类的关联操作,如级联删除、级联创建等;
- 实现事务;
- 定义事件,如发送电子邮件、删除文件等等;
以Picture为例子,存在类Picture、PictureDao和PictureService以及接口IPictureDao、IPictureService: Picture代码:
[Serializable]
public class Picture : BaseDomain
{
private readonly string[] _format = new string[] {".jpg", ".bmp", ".png", ".gif"};
private string fileName;
private string extension;
public Picture() : base()
{
}
public string FileName
{
get { return fileName == null ? string.Empty : fileName.Trim(); }
set { fileName = value; }
}
public string Extension
{
get { return extension== null ? string.Empty : extension.Trim(); }
set { extension= value; }
}
public override bool Validate()
{
if (string.IsNullOrEmpty(fileName))
{
Ex = new ArgumentNullException(fileName);
return false;
}
bool valid_format = false;
foreach (string format in _format)
{
if (format.Equals(extension))
{
valid_format = true;
break;
}
}
return valid_format;
}
}
IPictureService代码:
public interface IPictureService
{
void Create(Picture obj);
void Update(Picture obj);
Picture GetPictureById(int id);
Picture SearchOne(Hashtable conditions);
IList<Picture> SearchList(Hashtable conditions);
IList<Picture> SearchList(Hashtable conditions, int pageno, int pagesize);
int SearchCount(Hashtable conditions);
void Delete(Picture obj);
}
PictureService代码:
public class PictureService : IPictureService
{
private IAppDao<Picture> _PictureDao;
private IAppDao<GalleryPicture> _GalleryPictureDao;
public IAppDao<Picture> PictureDao
{
get { return _PictureDao; }
set { _PictureDao = value; }
}
public IAppDao<GalleryPicture> GalleryPictureDao
{
get { return _GalleryPictureDao; }
set { _GalleryPictureDao = value; }
}
#region IAppDao<Picture> 成员
public void Create(Picture obj)
{
if (obj.Validate())
{
PictureDao.Create(obj);
}
else
{
throw new ServiceException(obj.ExceptionMessage);
}
}
public void Update(Picture obj)
{
PictureDao.Update(obj);
}
public Picture GetPictureById(int id)
{
return PictureDao.GetById(id);
}
public Picture SearchOne(Hashtable conditions)
{
return PictureDao.SearchOne(conditions);
}
public IList<Picture> SearchList(Hashtable conditions)
{
return PictureDao.SearchList(conditions);
}
public IList<Picture> SearchList(Hashtable conditions, int pageno, int pagesize)
{
return PictureDao.SearchList(conditions, pageno, pagesize);
}
public int SearchCount(Hashtable conditions)
{
return PictureDao.SearchCount(conditions);
}
public void Delete(Picture obj)
{
if (obj != null)
{
Hashtable conditions = new Hashtable();
conditions.Add("Picture_Id", obj.Id);
IList<GalleryPicture> PictureServices = GalleryPictureDao.SearchList(conditions);
foreach (GalleryPicture item in PictureServices)
{
GalleryPictureDao.DeleteById(item.Id);
}
PictureDao.DeleteById(obj.Id);
}
}
#endregion
}