代码
//
泛型约束接口
using System;
using System.Collections.Generic;
using Cuyahoga.Core.Domain;
using NHibernate.Criterion;
namespace Cuyahoga.Core.DataAccess
{
public interface IContentItemDao < T > where T : IContentItem
{
T GetById( long id);
T GetById(Guid id);
IList < T > GetAll();
IList < T > GetBySite(Site site);
T Save(T entity);
void Delete(T entity);
}
}
// 接口实现
using System;
using System.Collections.Generic;
using Cuyahoga.Core.Domain;
using NHibernate;
using NHibernate.Criterion;
using Castle.Facilities.NHibernateIntegration;
using Castle.Services.Transaction;
namespace Cuyahoga.Core.DataAccess
{
[Transactional]
public class ContentItemDao < T > : IContentItemDao < T > where T : IContentItem
{
protected readonly ISessionManager SessionManager;
protected readonly Type PersistentType = typeof (T);
public ContentItemDao(ISessionManager sessionManager)
{
this .SessionManager = sessionManager;
}
protected ISession GetSession()
{
return this .SessionManager.OpenSession();
}
public T GetById( long id)
{
return this .GetSession().Get < T > (id);
}
public T GetById(Guid id)
{
return this .GetSession().Get < T > (id);
}
public IList < T > GetAll()
{
ICriteria criteria = this .GetSession().CreateCriteria(PersistentType);
return criteria.List < T > ();
}
public IList < T > GetBySite(Site site)
{
ICriteria criteria = this .GetSession().CreateCriteria(PersistentType)
.CreateCriteria( " Section " , " s " )
.Add(Expression.Eq( " Site " , site));
return criteria.List < T > ();
}
[Transaction(TransactionMode.Requires)]
public T Save(T entity)
{
this .GetSession().SaveOrUpdate(entity);
return entity;
}
[Transaction(TransactionMode.Requires)]
public void Delete(T entity)
{
this .GetSession().Delete(entity);
}
}
}
using System;
using System.Collections.Generic;
using Cuyahoga.Core.Domain;
using NHibernate.Criterion;
namespace Cuyahoga.Core.DataAccess
{
public interface IContentItemDao < T > where T : IContentItem
{
T GetById( long id);
T GetById(Guid id);
IList < T > GetAll();
IList < T > GetBySite(Site site);
T Save(T entity);
void Delete(T entity);
}
}
// 接口实现
using System;
using System.Collections.Generic;
using Cuyahoga.Core.Domain;
using NHibernate;
using NHibernate.Criterion;
using Castle.Facilities.NHibernateIntegration;
using Castle.Services.Transaction;
namespace Cuyahoga.Core.DataAccess
{
[Transactional]
public class ContentItemDao < T > : IContentItemDao < T > where T : IContentItem
{
protected readonly ISessionManager SessionManager;
protected readonly Type PersistentType = typeof (T);
public ContentItemDao(ISessionManager sessionManager)
{
this .SessionManager = sessionManager;
}
protected ISession GetSession()
{
return this .SessionManager.OpenSession();
}
public T GetById( long id)
{
return this .GetSession().Get < T > (id);
}
public T GetById(Guid id)
{
return this .GetSession().Get < T > (id);
}
public IList < T > GetAll()
{
ICriteria criteria = this .GetSession().CreateCriteria(PersistentType);
return criteria.List < T > ();
}
public IList < T > GetBySite(Site site)
{
ICriteria criteria = this .GetSession().CreateCriteria(PersistentType)
.CreateCriteria( " Section " , " s " )
.Add(Expression.Eq( " Site " , site));
return criteria.List < T > ();
}
[Transaction(TransactionMode.Requires)]
public T Save(T entity)
{
this .GetSession().SaveOrUpdate(entity);
return entity;
}
[Transaction(TransactionMode.Requires)]
public void Delete(T entity)
{
this .GetSession().Delete(entity);
}
}
}
转载于:https://www.cnblogs.com/hubcarl/archive/2010/04/07/1706397.html