NHibernate开源框架Cuyahoga学习之数据访问接口的实现
2010-04-07 15:32
162 查看
代码
一.数据访问接口
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Criterion;
namespace Cuyahoga.Core.DataAccess
{
public interface ICommonDao
{
object GetObjectById(Type type, int id);
object GetObjectById(Type type, int id, bool allowNull);
T GetObjectById<T>(int id);
object GetObjectByDescription(Type type, string propertyName, string description);
IList GetAll(Type type);
IList<T> GetAll<T>();
IList GetAll(Type type, params string[] sortProperties);
IList<T> GetAll<T>(params string[] sortProperties);
IList<T> GetAllByCriteria<T>(DetachedCriteria criteria);
IList<T> GetByIds<T>(int[] ids);
void SaveOrUpdateObject(object obj);
void UpdateObject(object obj);
void SaveObject(object obj);
void DeleteObject(object obj);
void MarkForDeletion(object obj);
void RemoveCollectionFromCache(string roleName);
void RemoveCollectionFromCache(string roleName, int id);
void RemoveQueryFromCache(string cacheRegion);
void Flush();
}
}
二.数据访问接口实现
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Castle.Facilities.NHibernateIntegration;
using Castle.Services.Transaction;
namespace Cuyahoga.Core.DataAccess
{
/// <summary>
/// Functionality for common simple data access. The class uses NHibernate.
/// </summary>
[Transactional]
public class CommonDao : ICommonDao
{
private readonly ISessionManager _sessionManager;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="sessionManager"></param>
public CommonDao(ISessionManager sessionManager)
{
this._sessionManager = sessionManager;
}
#region ICommonDao Members
public object GetObjectById(Type type, int id)
{
ISession session = this._sessionManager.OpenSession();
return session.Load(type, id);
}
public object GetObjectById(Type type, int id, bool allowNull)
{
if (! allowNull)
{
return GetObjectById(type, id);
}
else
{
ISession session = this._sessionManager.OpenSession();
return session.Get(type, id);
}
}
public T GetObjectById<T>(int id)
{
ISession session = this._sessionManager.OpenSession();
return session.Get<T>(id);
}
public object GetObjectByDescription(Type type, string propertyName, string description)
{
ISession session = this._sessionManager.OpenSession();
ICriteria crit = session.CreateCriteria(type);
crit.Add(Expression.Eq(propertyName, description));
return crit.UniqueResult();
}
public IList GetAll(Type type)
{
return GetAll(type, null);
}
public IList GetAll(Type type, params string[] sortProperties)
{
ISession session = this._sessionManager.OpenSession();
ICriteria crit = session.CreateCriteria(type);
if (sortProperties != null)
{
foreach (string sortProperty in sortProperties)
{
crit.AddOrder(Order.Asc(sortProperty));
}
}
return crit.List();
}
/// <summary>
/// Get all objects of T.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IList<T> GetAll<T>()
{
return GetAll<T>(null);
}
/// <summary>
/// Get all objects of T.
/// </summary>
/// <param name="sortProperties"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IList<T> GetAll<T>(params string[] sortProperties)
{
ISession session = this._sessionManager.OpenSession();
ICriteria crit = session.CreateCriteria(typeof(T));
if (sortProperties != null)
{
foreach (string sortProperty in sortProperties)
{
crit.AddOrder(Order.Asc(sortProperty));
}
}
return crit.List<T>();
}
/// <summary>
/// Get all objects of T that match the given criteria.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="criteria">NHibernate DetachedCriteria instance.</param>
/// <returns></returns>
/// <remarks>
/// Be careful to not use this one from the UI layer beacuse it ties the UI to NHibernate.
/// </remarks>
public IList<T> GetAllByCriteria<T>(DetachedCriteria criteria)
{
using (ISession session = this._sessionManager.OpenSession())
{
ICriteria crit = criteria.GetExecutableCriteria(session);
return crit.List<T>();
}
}
/// <summary>
/// Get all objects of T for the given id's.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ids"></param>
/// <returns></returns>
public IList<T> GetByIds<T>(int[] ids)
{
ISession session = this._sessionManager.OpenSession();
ICriteria crit = session.CreateCriteria(typeof(T))
.Add(Expression.In("Id", ids));
return crit.List<T>();
}
[Transaction(TransactionMode.Requires)]
public virtual void SaveOrUpdateObject(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.SaveOrUpdate(obj);
}
[Transaction(TransactionMode.Requires)]
public virtual void SaveObject(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.Save(obj);
}
[Transaction(TransactionMode.Requires)]
public virtual void UpdateObject(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.Update(obj);
}
[Transaction(TransactionMode.Requires)]
public virtual void DeleteObject(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.Delete(obj);
}
public void MarkForDeletion(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.Delete(obj);
}
public void RemoveCollectionFromCache(string roleName)
{
ISession session = this._sessionManager.OpenSession();
session.SessionFactory.EvictCollection(roleName);
}
public void RemoveCollectionFromCache(string roleName, int id)
{
ISession session = this._sessionManager.OpenSession();
session.SessionFactory.EvictCollection(roleName, id);
}
public void RemoveQueryFromCache(string cacheRegion)
{
ISession session = this._sessionManager.OpenSession();
session.SessionFactory.EvictQueries(cacheRegion);
}
public void Flush()
{
ISession session = this._sessionManager.OpenSession();
session.Flush();
}
#endregion
}
}
一.数据访问接口
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Criterion;
namespace Cuyahoga.Core.DataAccess
{
public interface ICommonDao
{
object GetObjectById(Type type, int id);
object GetObjectById(Type type, int id, bool allowNull);
T GetObjectById<T>(int id);
object GetObjectByDescription(Type type, string propertyName, string description);
IList GetAll(Type type);
IList<T> GetAll<T>();
IList GetAll(Type type, params string[] sortProperties);
IList<T> GetAll<T>(params string[] sortProperties);
IList<T> GetAllByCriteria<T>(DetachedCriteria criteria);
IList<T> GetByIds<T>(int[] ids);
void SaveOrUpdateObject(object obj);
void UpdateObject(object obj);
void SaveObject(object obj);
void DeleteObject(object obj);
void MarkForDeletion(object obj);
void RemoveCollectionFromCache(string roleName);
void RemoveCollectionFromCache(string roleName, int id);
void RemoveQueryFromCache(string cacheRegion);
void Flush();
}
}
二.数据访问接口实现
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Castle.Facilities.NHibernateIntegration;
using Castle.Services.Transaction;
namespace Cuyahoga.Core.DataAccess
{
/// <summary>
/// Functionality for common simple data access. The class uses NHibernate.
/// </summary>
[Transactional]
public class CommonDao : ICommonDao
{
private readonly ISessionManager _sessionManager;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="sessionManager"></param>
public CommonDao(ISessionManager sessionManager)
{
this._sessionManager = sessionManager;
}
#region ICommonDao Members
public object GetObjectById(Type type, int id)
{
ISession session = this._sessionManager.OpenSession();
return session.Load(type, id);
}
public object GetObjectById(Type type, int id, bool allowNull)
{
if (! allowNull)
{
return GetObjectById(type, id);
}
else
{
ISession session = this._sessionManager.OpenSession();
return session.Get(type, id);
}
}
public T GetObjectById<T>(int id)
{
ISession session = this._sessionManager.OpenSession();
return session.Get<T>(id);
}
public object GetObjectByDescription(Type type, string propertyName, string description)
{
ISession session = this._sessionManager.OpenSession();
ICriteria crit = session.CreateCriteria(type);
crit.Add(Expression.Eq(propertyName, description));
return crit.UniqueResult();
}
public IList GetAll(Type type)
{
return GetAll(type, null);
}
public IList GetAll(Type type, params string[] sortProperties)
{
ISession session = this._sessionManager.OpenSession();
ICriteria crit = session.CreateCriteria(type);
if (sortProperties != null)
{
foreach (string sortProperty in sortProperties)
{
crit.AddOrder(Order.Asc(sortProperty));
}
}
return crit.List();
}
/// <summary>
/// Get all objects of T.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IList<T> GetAll<T>()
{
return GetAll<T>(null);
}
/// <summary>
/// Get all objects of T.
/// </summary>
/// <param name="sortProperties"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IList<T> GetAll<T>(params string[] sortProperties)
{
ISession session = this._sessionManager.OpenSession();
ICriteria crit = session.CreateCriteria(typeof(T));
if (sortProperties != null)
{
foreach (string sortProperty in sortProperties)
{
crit.AddOrder(Order.Asc(sortProperty));
}
}
return crit.List<T>();
}
/// <summary>
/// Get all objects of T that match the given criteria.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="criteria">NHibernate DetachedCriteria instance.</param>
/// <returns></returns>
/// <remarks>
/// Be careful to not use this one from the UI layer beacuse it ties the UI to NHibernate.
/// </remarks>
public IList<T> GetAllByCriteria<T>(DetachedCriteria criteria)
{
using (ISession session = this._sessionManager.OpenSession())
{
ICriteria crit = criteria.GetExecutableCriteria(session);
return crit.List<T>();
}
}
/// <summary>
/// Get all objects of T for the given id's.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ids"></param>
/// <returns></returns>
public IList<T> GetByIds<T>(int[] ids)
{
ISession session = this._sessionManager.OpenSession();
ICriteria crit = session.CreateCriteria(typeof(T))
.Add(Expression.In("Id", ids));
return crit.List<T>();
}
[Transaction(TransactionMode.Requires)]
public virtual void SaveOrUpdateObject(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.SaveOrUpdate(obj);
}
[Transaction(TransactionMode.Requires)]
public virtual void SaveObject(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.Save(obj);
}
[Transaction(TransactionMode.Requires)]
public virtual void UpdateObject(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.Update(obj);
}
[Transaction(TransactionMode.Requires)]
public virtual void DeleteObject(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.Delete(obj);
}
public void MarkForDeletion(object obj)
{
ISession session = this._sessionManager.OpenSession();
session.Delete(obj);
}
public void RemoveCollectionFromCache(string roleName)
{
ISession session = this._sessionManager.OpenSession();
session.SessionFactory.EvictCollection(roleName);
}
public void RemoveCollectionFromCache(string roleName, int id)
{
ISession session = this._sessionManager.OpenSession();
session.SessionFactory.EvictCollection(roleName, id);
}
public void RemoveQueryFromCache(string cacheRegion)
{
ISession session = this._sessionManager.OpenSession();
session.SessionFactory.EvictQueries(cacheRegion);
}
public void Flush()
{
ISession session = this._sessionManager.OpenSession();
session.Flush();
}
#endregion
}
}
相关文章推荐
- NHibernate开源框架Cuyahoga学习之数据访问实现
- EJB3.0学习笔记---Bean实现多个接口的情况下定义,访问方式:
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- java代码实现访问网络外部接口并获取数据的工具类
- Effective C# 学习笔记(四十一)使利用DynamicObject或IDynamicMetaObjectProvider接口实现数据驱动的动态类型
- Spring.NET 1.3.2 集成 NHibernate 3.2 - 4 - 实现基本的数据访问
- DbEntry.Net---又一个国产开源ORM数据访问及 WEB 框架
- 购物车一级列表的简单实现(访问接口数据)
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (三)实现数据库接口层和业务逻辑层(转)
- 接口测试 Http 接口测试框架 (思路 + 实现中 + 开源 + 可能难产)
- NHibernate+WCF项目实战(二)使用NHibernate实现数据访问并进行单元测试
- 接口自动化 开源框架学习-supertest
- PHP Yii开源框架入门学习(二)修改Yii网站访问路径
- 学习使用NHibernate2.1.0Beta1(四)— 创建数据访问项目DAL
- 大数据技术学习笔记之hive框架基础2-hive中常用DML和UDF和连接接口使用
- 玩转数据系列:利用阿里云机器学习在深度学习框架下实现智能图片分类
- 开源中国源码学习数据篇(一)之android-async-http框架和AsyncTask
- 推荐一个学习NHibernate的开源项目--Cuyahoga
- 数据访问组件&ORM测试框架(1)——EF方案实现
- NHibernate+WCF项目实战(二)使用NHibernate实现数据访问并进行单元测试