在当前的应用程序中,有两组对象,主要用作Domain和NHibernate对象,带有接口。域对象由表示层和业务层使用,而NHibernate对象仅由数据层使用。目前,对于任何数据库事务,业务层将域对象传递给数据层,数据层通过自动映射器在内部转换为NHibernate。
我想实现常见的Save,Get和Delete方法。 我已经使用泛型,存储库和反射来实现如下的通用Save方法和接口。
是否有人对如何使用泛型和反射创建常见的Get和Delete方法以及NHibernate的数据访问有任何见解?
答案 0 :(得分:0)
您好我不确定这是否正是您所寻找的,但您可以尝试这样的事情。
public abstract class BaseRepo<TIdType, TPersistentType>
{
protected ISession Session { get; private set; }
protected BaseRepo(ISession session)
{
Session = session;
}
public virtual void Save(TPersistentType request)
{
Session.Save(request);
}
public virtual TPersistentType GetById(TIdType id)
{
return (TPersistentType)Session.Load(typeof(TPersistentType), id);
}
public virtual IQueryable<TPersistentType> QueryList()
{
return Session.Linq<TPersistentType>();
}
}
答案 1 :(得分:0)
我认为这是您正在寻找的代码:
文件名:GenericDao.cs
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Spring.Data.NHibernate.Generic.Support;
namespace Mapits.Library.Dao
{
public class GenericDao<T, ID> : HibernateDaoSupport, IGenericDao<T, ID>
{
#region Constants
private const string Asc = "Asc";
private const string Desc = "Desc";
#endregion
#region Update
/// <summary>
/// Update Entity
/// </summary>
/// <param name="entity"></param>
public void Update(T entity)
{
HibernateTemplate.Update(entity);
}
/// <summary>
/// Update List of Entity
/// </summary>
/// <param name="listEntity"></param>
public void Update(List<T> listEntity)
{
foreach (object entity in listEntity)
{
HibernateTemplate.Update(entity);
}
}
#endregion
#region Save
/// <summary>
/// Save Entity
/// </summary>
/// <param name="entity"></param>
public void Save(T entity)
{
HibernateTemplate.Save(entity);
}
/// <summary>
/// Save List of Entity
/// </summary>
/// <param name="listEntity"></param>
public void Save(List<T> listEntity)
{
foreach (object entity in listEntity)
{
HibernateTemplate.Save(entity);
}
}
#endregion
#region Delete
/// <summary>
/// Delete Entity
/// </summary>
/// <param name="entity"></param>
public void Delete(T entity)
{
HibernateTemplate.Delete(entity);
}
/// <summary>
/// Delete List of Entity
/// </summary>
/// <param name="listEntity"></param>
public void Delete(List<T> listEntity)
{
foreach (object entity in listEntity)
{
HibernateTemplate.Delete(entity);
}
}
#endregion
#region SaveOrUpdate
/// <summary>
/// Save or Update Entity
/// </summary>
/// <param name="entity"></param>
public void SaveOrUpdate(T entity)
{
HibernateTemplate.SaveOrUpdate(entity);
}
/// <summary>
/// Flush Entity from Hibernate Session
/// </summary>
public void Flush()
{
HibernateTemplate.Flush();
//Session.Flush();
}
/// <summary>
/// Evict Entity from Hibernate Session
/// </summary>
/// <param name="entity"></param>
public void Evict(T entity)
{
HibernateTemplate.Evict(entity);
//Session.Evict(entity);
}
/// <summary>
/// Save or Update List of Entity
/// </summary>
/// <param name="listEntity"></param>
public void SaveOrUpdate(List<T> listEntity)
{
foreach (object entity in listEntity)
{
HibernateTemplate.SaveOrUpdate(entity);
}
}
#endregion
#region GetList
/// <summary>
/// GetList by CreateByQuery
/// </summary>
/// <param name="query"></param>
/// <param name="param"></param>
/// <returns></returns>
public List<T> CreateByQuery(string query, Dictionary<List<string>, List<string>> param)
{
IQuery q = Session.CreateQuery(query);
SetString(q, param);
return (List<T>)q.List<T>();
}
public IList GetListByCriteriaQuery(int minResult, int maxResult, ProjectionList projectionList,
ICriterion[] criterion, Dictionary<string, string> orders)
{
ICriteria crit = Session.CreateCriteria(typeof(T));
crit.SetFirstResult(minResult);
if (maxResult != 0) crit.SetMaxResults(maxResult);
if (projectionList != null)
{
crit.SetProjection(projectionList);
}
if (criterion != null)
{
foreach (ICriterion iCrit in criterion)
{
crit.Add(iCrit);
}
}
if (orders != null)
{
foreach (KeyValuePair<string, string> order in orders)
{
if (order.Value == Asc)
{
crit.AddOrder(Order.Asc(order.Key));
}
if (order.Value == Desc)
{
crit.AddOrder(Order.Desc(order.Key));
}
}
}
IList list = crit.List();
if (list.Count > 0)
return list;
return null;
}
/// <summary>
/// Get List By Criteria Query
/// </summary>
/// <param name="minResult"></param>
/// <param name="maxResult"></param>
/// <param name="projection"></param>
/// <param name="criterion"></param>
/// <param name="orders"></param>
/// <returns></returns>
public IList<T> GetListByCriteriaQuery(int minResult, int maxResult,
ICriterion[] criterion, Dictionary<string, string> orders)
{
ICriteria crit = Session.CreateCriteria(typeof(T));
crit.SetFirstResult(minResult);
if (maxResult != 0) crit.SetMaxResults(maxResult);
if (criterion != null)
{
foreach (ICriterion iCrit in criterion)
{
crit.Add(iCrit);
}
}
if (orders != null)
{
foreach (KeyValuePair<string, string> order in orders)
{
if (order.Value == Asc)
{
crit.AddOrder(Order.Asc(order.Key));
}
if (order.Value == Desc)
{
crit.AddOrder(Order.Desc(order.Key));
}
}
}
IList<T> list = crit.List<T>();
if (list.Count > 0)
return list;
return null;
}
/// <summary>
/// GetList by CreateByNamedQuery - Native SQL
/// </summary>
/// <param name="query"></param>
/// <param name="param"></param>
/// <returns></returns>
public List<T> CreateByNamedSQLQuery(string query, Dictionary<List<string>, List<string>> param)
{
IQuery q = Session.GetNamedQuery(query);
SetString(q, param);
return (List<T>)q.List<T>();
}
/// <summary>
/// GetList by GetByNamedQuery - Native SQL
/// </summary>
/// <param name="query"></param>
/// <param name="param"></param>
/// <returns></returns>
public IList GetByNamedQuery(string query, Dictionary<List<string>, List<string>> param)
{
IQuery q = Session.GetNamedQuery(query);
SetString(q, param);
return q.List();
}
#endregion
#region Miscellaneous
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public T Load(ID id)
{
return HibernateTemplate.Load<T>(id);
}
/// <summary>
/// Check if row exist by id
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public T Get(ID id)
{
T t = (T)Session.Get(typeof(T), id);
return t;
}
public T GetByInt(Int32 id)
{
T t = (T)Session.Get(typeof(T), id);
return t;
}
/// <summary>
/// Get Entity by Criteria
/// </summary>
/// <param name="fieldname"></param>
/// <param name="value"></param>
/// <returns></returns>
public T GetByCriteria(string fieldname, string value)
{
ICriteria crit = Session.CreateCriteria(typeof(T));
crit.Add(Restrictions.Eq(fieldname, value));
return (T)crit.UniqueResult();
}
/// <summary>
/// Get ISQLQuery by CreateByISQLQuery - Native SQL
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public ISQLQuery CreateByISQLQuery(string query)
{
ISQLQuery q = Session.CreateSQLQuery(query);
return q;
}
#endregion
#region Private Methods...
private void SetString(IQuery q, Dictionary<List<string>, List<string>> param)
{
foreach (KeyValuePair<List<string>, List<string>> valuePair in param)
{
for (int i = 0; i < valuePair.Key.Count; i++)
{
q.SetString(valuePair.Key[i], valuePair.Value[i]);
}
}
}
#endregion
}
}