ASP.NET&Spring.NET&NHibernate最佳实践(十三)——第4章权限子系统(6)
用户数据访问接口(IUserDao.cs)
using System;
using Guushuuse.SalaryPrj.Security.DomainModel;
using System.Collections;

namespace Guushuuse.SalaryPrj.Security.Dao
{
/// <summary>
/// 用户数据访问接口
/// </summary>
public interface IUserDao
{
void CreateUser(User user);
void DeleteUser(User user);
IList FindUsersByEmail(Application application, string emailToMatch, int pageIndex, int pageSize, out int totalRecords);
IList FindUsersByEmail(Application application, string emailToMatch);
IList FindUsersByName(Application application, string usernameToMatch);
IList FindUsersByName(Application application, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords);
IList FindUsersInRole(Application application, Role role, string usernameToMatch);
int GetActiveUsersCount(Application application, DateTime activeDate);
IList GetAllUsers();
IList GetInactiveUsers(Application application, DateTime userInactiveSinceDate);
IList GetInactiveUsers(Application application, DateTime userInactiveSinceDate, bool isAnonymous);
User GetUser(int userID);
User GetUser(Application application, string username);
IList GetUsers(Application application, int pageIndex, int pageSize, out long totalRecords);
IList GetUsers(Application application, string email);
int GetUsersCountInRole(Application application, Role role);
IList GetUsersInRole(Application application, Role role);
void UpdateUser(User user);
}
}
用户数据访问类(UserDao.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.Data.NHibernate;
using NHibernate.Type;
using NHibernate;
using Spring.Dao.Support;

namespace Guushuuse.SalaryPrj.Security.Dao
{
/// <summary>
/// 用户数据访问类
/// </summary>
public class UserDao : HibernateDaoSupport, IUserDao
{
public UserDao()
{

}

[Transaction(ReadOnly = false)]
public void CreateUser(User user)
{
HibernateTemplate.Save(user);
}

[Transaction(ReadOnly = false)]
public void UpdateUser(User user)
{
HibernateTemplate.Update(user);
}

[Transaction(ReadOnly = false)]
public void DeleteUser(User user)
{
HibernateTemplate.Delete(user);
}

public IList GetAllUsers()
{
return HibernateTemplate.LoadAll(typeof(User));
}

public IList GetUsers(Application application, string email)
{
if (email != null)
{
string hql = " from User user where user.Application = ? and user.Email = ?";

return HibernateTemplate.Find(hql, new object[] { application, email });
}
else
{
string hql = " from User user where user.Application = ? and user.Email is null";

return HibernateTemplate.Find(hql, new object[] { application });
}
}


public IList GetInactiveUsers(Application application, DateTime userInactiveSinceDate)
{
string hql = " from User user where user.Application = ? and user.LastActivityDate < ?";

return HibernateTemplate.Find(hql, new object[] { application, userInactiveSinceDate });
}

public IList GetInactiveUsers(Application application, DateTime userInactiveSinceDate,
bool isAnonymous)
{
string hql = " from User user where user.Application = ? and user.LastActivityDate < ? and user.IsAnonymous = ?";

return HibernateTemplate.Find(hql, new object[] { application, userInactiveSinceDate,
isAnonymous});

}

public IList GetUsers(Application application, int pageIndex, int pageSize, out long totalRecords)
{
string hql = " from User user where user.Application = ?";

IList users = (IList)HibernateTemplate.Execute(new GetObjectsCallback(HibernateTemplate, hql,
new object[] { application }, null, pageIndex, pageSize), true);

hql = "select count(user.ID) from User user where user.Application = ?";

totalRecords = (long)DataAccessUtils.RequiredUniqueResultSet(HibernateTemplate.Find(hql, new object[] { application }));

return users;
}

public IList GetUsersInRole(Application application, Role role)
{
return new ArrayList();
}

public IList FindUsersByName(Application application, string usernameToMatch)
{
string hql = " from User user where user.Application = ? and user.Username like ?";

return HibernateTemplate.Find(hql, new object[] { application, "%" + usernameToMatch + "%" });
}


public IList FindUsersByName(Application application, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
string hql = " from User user where user.Application = ? and user.Username like ?";

IList users = (IList)HibernateTemplate.Execute(new GetObjectsCallback(HibernateTemplate, hql,
new object[] { application, "%" + usernameToMatch + "%" }, null, pageIndex, pageSize), true);

hql = "select count(user.ID) from User user where user.Application = ?";

totalRecords = (int)DataAccessUtils.RequiredUniqueResultSet(HibernateTemplate.Find(hql, new object[] { application }));

return users;
}

public IList FindUsersByEmail(Application application, string emailToMatch)
{
string hql = " from User user where user.Application = ? and user.Email like ?";

return HibernateTemplate.Find(hql, new object[] { application, "%" + emailToMatch + "%" });
}

public IList FindUsersByEmail(Application application, string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
string hql = " from User user where user.Application = ? and user.Email like ?";

IList users = (IList)HibernateTemplate.Execute(new GetObjectsCallback(HibernateTemplate, hql,
new object[] { application, "%" + emailToMatch + "%" }, null, pageIndex, pageSize), true);

hql = "select count(user.ID) from User user where user.Application = ?";

totalRecords = (int)DataAccessUtils.RequiredUniqueResultSet(HibernateTemplate.Find(hql, new object[] { application }));

return users;
}


public IList FindUsersInRole(Application application, Role role, string usernameToMatch)
{
return new ArrayList();
}


public User GetUser(int userID)
{
return (User)HibernateTemplate.Get(typeof(User), userID);
}

public User GetUser(Application application, string username)
{
string hql = " from User user where user.Application = ? and user.Username = ?";

IList users = HibernateTemplate.Find(hql, new object[] { application, username });

if (users.Count > 0)
{
return (User)DataAccessUtils.RequiredUniqueResultSet(users);
}
else
{
return null;
}
}

public int GetActiveUsersCount(Application application, DateTime activeDate)
{
string hql = "select count(user.ID) from User user where user.Application = ? and user.LastActivityDate > ?";

return (int)DataAccessUtils.RequiredUniqueResultSet(HibernateTemplate.Find(hql, new object[] { application, activeDate }));
}

public int GetUsersCountInRole(Application application, Role role)
{
return 0;
}

internal class GetObjectsCallback : IHibernateCallback
{
private HibernateTemplate _outer;
private string _queryString;
private object[] _values;
private IType[] _types;
private int _startIndex;
private int _maxRows;


public GetObjectsCallback(HibernateTemplate template, string queryString, object[] values, IType[] types, int startIndex, int maxRows)
{
this._outer = template;
this._queryString = queryString;
this._values = values;
this._types = types;
this._startIndex = startIndex;
this._maxRows = maxRows;

}

public object DoInHibernate(ISession session)
{
IQuery queryObject = session.CreateQuery(_queryString);
_outer.PrepareQuery(queryObject);

if (_values != null)
{
for (int i = 0; i < _values.Length; i++)
{
if (_types != null && _types[i] != null)
{
queryObject.SetParameter(i, _values[i], _types[i]);
}
else
{
queryObject.SetParameter(i, _values[i]);
}
}
}

queryObject.SetFirstResult(_startIndex);
queryObject.SetMaxResults(_maxRows);

return queryObject.List();
}
}
}
}
































用户数据访问类(UserDao.cs)









































































































































































































































posted on 2008-05-17 16:01 guushuuse 阅读(1233) 评论(1) 编辑 收藏 举报