Ibaitis+Castle的设计方案
来源:互联网 发布:淘天下网络pos机是真是假 编辑:程序博客网 时间:2024/05/18 02:18
- 实体基类: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 }
- Ibaitis+Castle的设计方案
- ibaitis 关于处理null值的方法
- ibaitis简介
- ibaitis实例
- Castle.DynamicProxy的二次开发
- Castle
- Castle
- GUI设计方案的异同
- 汽车钥匙的设计方案
- 智能数据仓库的设计方案
- 智能手机的双网双待设计方案
- Yahle 的服务器设计方案!
- freescale那时的设计方案。
- 读写分离的设计方案
- 简单的权限设计方案
- APK的瘦身设计方案
- ibaitis之foreach使用
- 设计方案
- 所谓“杨不管”……
- 最近干的事情:跟自己过不去
- 破解加密存储过程、触发器、视图和函数的脚本
- Project日程表实练体会
- 给ASP.NET中的用户自定义空间添加属性
- Ibaitis+Castle的设计方案
- 形式化方法
- HOWTO Joystick Setup
- C++ STL基本容器比较
- 创造自己的xtype
- java中String的比较
- lib和dll文件的区别和联系
- 良好的开始
- 玩转gridview 1