ASP.NET&Spring.NET&NHibernate最佳实践(十二)——第4章权限子系统(5)
角色数据访问接口(IRoleDao.cs)
角色数据访问类(RoleDao.cs)
using System;
using Guushuuse.SalaryPrj.Security.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.Security.Dao
{
/// <summary>
/// 角色数据访问接口
/// </summary>
public interface IRoleDao
{
void CreateRole(Role role);
void DeleteRole(Role role);
IList GetAllRoles();
Role GetRole(int roleID);
Role GetRole(Application application, string roleName);
IList GetRoles(Application application);
void UpdateRole(Role role);
}
}
using Guushuuse.SalaryPrj.Security.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.Security.Dao
{
/// <summary>
/// 角色数据访问接口
/// </summary>
public interface IRoleDao
{
void CreateRole(Role role);
void DeleteRole(Role role);
IList GetAllRoles();
Role GetRole(int roleID);
Role GetRole(Application application, string roleName);
IList GetRoles(Application application);
void UpdateRole(Role role);
}
}
角色数据访问类(RoleDao.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate.Support;
using Spring.Transaction.Interceptor;
using Guushuuse.SalaryPrj.Security.DomainModel;
using System.Collections;
using Spring.Dao.Support;
namespace Guushuuse.SalaryPrj.Security.Dao
{
/// <summary>
/// 角色数据访问类
/// </summary>
public class RoleDao : HibernateDaoSupport, IRoleDao
{
public RoleDao()
{
}
[Transaction(ReadOnly = false)]
public void CreateRole(Role role)
{
HibernateTemplate.Save(role);
}
[Transaction(ReadOnly = false)]
public void UpdateRole(Role role)
{
HibernateTemplate.Update(role);
}
[Transaction(ReadOnly = false)]
public void DeleteRole(Role role)
{
HibernateTemplate.Delete(role);
}
public IList GetAllRoles()
{
return HibernateTemplate.LoadAll(typeof(Role));
}
public IList GetRoles(Application application)
{
string hql = " from Role r where r.Application = ?";
return HibernateTemplate.Find(hql, new object[] { application });
}
public Role GetRole(int roleID)
{
return (Role)HibernateTemplate.Get(typeof(Role), roleID);
}
public Role GetRole(Application application, string roleName)
{
string hql = " from Role role where role.Application = ? and role.Name = ?";
IList roles = HibernateTemplate.Find(hql, new object[] { application, roleName });
if (roles.Count > 0)
{
return (Role)DataAccessUtils.RequiredUniqueResultSet(roles);
}
else
{
return null;
}
}
}
}
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate.Support;
using Spring.Transaction.Interceptor;
using Guushuuse.SalaryPrj.Security.DomainModel;
using System.Collections;
using Spring.Dao.Support;
namespace Guushuuse.SalaryPrj.Security.Dao
{
/// <summary>
/// 角色数据访问类
/// </summary>
public class RoleDao : HibernateDaoSupport, IRoleDao
{
public RoleDao()
{
}
[Transaction(ReadOnly = false)]
public void CreateRole(Role role)
{
HibernateTemplate.Save(role);
}
[Transaction(ReadOnly = false)]
public void UpdateRole(Role role)
{
HibernateTemplate.Update(role);
}
[Transaction(ReadOnly = false)]
public void DeleteRole(Role role)
{
HibernateTemplate.Delete(role);
}
public IList GetAllRoles()
{
return HibernateTemplate.LoadAll(typeof(Role));
}
public IList GetRoles(Application application)
{
string hql = " from Role r where r.Application = ?";
return HibernateTemplate.Find(hql, new object[] { application });
}
public Role GetRole(int roleID)
{
return (Role)HibernateTemplate.Get(typeof(Role), roleID);
}
public Role GetRole(Application application, string roleName)
{
string hql = " from Role role where role.Application = ? and role.Name = ?";
IList roles = HibernateTemplate.Find(hql, new object[] { application, roleName });
if (roles.Count > 0)
{
return (Role)DataAccessUtils.RequiredUniqueResultSet(roles);
}
else
{
return null;
}
}
}
}