用NClay进行业务编写到底有多简单?
[NClay.Services.Service(typeof(IUserService))]
public class UserService :DBAccess, IUserService
{
IUserService 成员#region IUserService 成员
public void Create(User user)
{
lock (typeof(UserService))
{
if ((_User.UserName == user.UserName).CountOf<User>() > 0)
throw new SPException(string.Format(SysMessage.ERROR_系统已经存在用户名的用户, user.UserName));
user.UserPWD = NClay.Common.MD5Crypto(user.UserPWD);
DaoContext.Add(user);
}
}
public void Delete(string[] userid)
{
_User.UserID.In(userid).Delete<User>();
}
public User Load(string userid)
{
return DaoContext.Load<User>(userid);
}
public User LoadByName(string username)
{
return (_User.UserName == username).ListFirst<User>();
}
public IList<User> List(NClay.IDataPage datapate)
{
Expression exp = new Expression();
return exp.List<User>(datapate);
}
public void ChangePWD(string username, string olepwd, string newpwd)
{
olepwd = NClay.Common.MD5Crypto(olepwd);
newpwd = NClay.Common.MD5Crypto(newpwd);
if ((_User.UserName == username & _User.UserPWD == olepwd)
.CountOf<User>() == 0)
throw new SPException(SysMessage.ERROR_用户或密码不正确);
(_User.UserName == username)
.Edit<User>(_User.UserPWD.Set(newpwd));
}
const string mPWDSeedStock = "1234567890qwertyuiopasdfghjklzxcvbnm";
public string GetNewPWD(string username)
{
Random ran = new Random();
string pwd="";
for (int i = 0; i < 6; i++)
{
pwd += mPWDSeedStock.Substring(ran.Next(mPWDSeedStock.Length), 1);
}
pwd = NClay.Common.MD5Crypto(pwd);
(_User.UserName == username).Edit<User>(
_User.UserPWD.Set(pwd)
);
return pwd;
}
public void ChangeState(int state, string[] userid)
{
_User.UserID.In(userid).Edit<User>(
_User.State.Set(state));
}
#endregion
IUserService 成员#region IUserService 成员
public User Login(string username, string pwd)
{
Expression exp;
exp = _User.UserName == username
& _User.UserPWD == NClay.Common.MD5Crypto(pwd);
User user = exp.ListFirst<User>();
if(user== null)
throw new SPException(SysMessage.ERROR_用户或密码不正确);
return user;
}
#endregion
}
public class UserService :DBAccess, IUserService
{
IUserService 成员#region IUserService 成员
public void Create(User user)
{
lock (typeof(UserService))
{
if ((_User.UserName == user.UserName).CountOf<User>() > 0)
throw new SPException(string.Format(SysMessage.ERROR_系统已经存在用户名的用户, user.UserName));
user.UserPWD = NClay.Common.MD5Crypto(user.UserPWD);
DaoContext.Add(user);
}
}
public void Delete(string[] userid)
{
_User.UserID.In(userid).Delete<User>();
}
public User Load(string userid)
{
return DaoContext.Load<User>(userid);
}
public User LoadByName(string username)
{
return (_User.UserName == username).ListFirst<User>();
}
public IList<User> List(NClay.IDataPage datapate)
{
Expression exp = new Expression();
return exp.List<User>(datapate);
}
public void ChangePWD(string username, string olepwd, string newpwd)
{
olepwd = NClay.Common.MD5Crypto(olepwd);
newpwd = NClay.Common.MD5Crypto(newpwd);
if ((_User.UserName == username & _User.UserPWD == olepwd)
.CountOf<User>() == 0)
throw new SPException(SysMessage.ERROR_用户或密码不正确);
(_User.UserName == username)
.Edit<User>(_User.UserPWD.Set(newpwd));
}
const string mPWDSeedStock = "1234567890qwertyuiopasdfghjklzxcvbnm";
public string GetNewPWD(string username)
{
Random ran = new Random();
string pwd="";
for (int i = 0; i < 6; i++)
{
pwd += mPWDSeedStock.Substring(ran.Next(mPWDSeedStock.Length), 1);
}
pwd = NClay.Common.MD5Crypto(pwd);
(_User.UserName == username).Edit<User>(
_User.UserPWD.Set(pwd)
);
return pwd;
}
public void ChangeState(int state, string[] userid)
{
_User.UserID.In(userid).Edit<User>(
_User.State.Set(state));
}
#endregion
IUserService 成员#region IUserService 成员
public User Login(string username, string pwd)
{
Expression exp;
exp = _User.UserName == username
& _User.UserPWD == NClay.Common.MD5Crypto(pwd);
User user = exp.ListFirst<User>();
if(user== null)
throw new SPException(SysMessage.ERROR_用户或密码不正确);
return user;
}
#endregion
}
[NClay.Services.Service(typeof(IRoleService))]
public class RoleService :DBAccess, IRoleService
{
IRoleService 成员#region IRoleService 成员
public void Create(Role role)
{
Expression exp = _Role.RoleName == role.RoleName;
if (exp.CountOf<Role>() > 0)
{
throw new SPException(string.Format(SysMessage.ERROR_系统已经存在角色名称的角色, role.RoleName));
}
DaoContext.Add(role);
}
public void Delete(string[] roleid)
{
Expression exp = _Role.RoleID.In(roleid);
exp.Delete<Role>();
exp = _RolesUser.RoleID.In(roleid);
exp.Delete<RolesUser>();
}
public void SetUserRole(string[] userid, string[] roleid)
{
Expression exp;
RolesUser ru;
using (TransactionContext tc = new TransactionContext())
{
foreach (string user in userid)
{
foreach (string role in roleid)
{
exp = _RolesUser.UserID == user & _RolesUser.RoleID == role;
if (exp.CountOf<RolesUser>() == 0)
{
ru = new RolesUser();
ru.RoleID = role;
ru.UserID = user;
DaoContext.Add(ru);
}
}
}
tc.Commit();
}
}
public void CancelUserRole(string[] userid, string[] roleid)
{
Expression exp;
exp = _RolesUser.RoleID.In(roleid) & _RolesUser.UserID.In(userid);
exp.Delete<RolesUser>();
}
public IList<User> GetUsersInRole(string roleid)
{
Expression exp;
exp = _User.UserID.In(_RolesUser.UserID, _RolesUser.RoleID == roleid);
return exp.List<User>();
}
public Role Load(string roleid)
{
return DaoContext.Load<Role>(roleid);
}
public IList<Role> GetRolesForUser(string userid)
{
Expression exp;
exp = _Role.RoleID.In(_RolesUser.RoleID, _RolesUser.UserID == userid);
return exp.List<Role>();
}
#endregion
}
public class RoleService :DBAccess, IRoleService
{
IRoleService 成员#region IRoleService 成员
public void Create(Role role)
{
Expression exp = _Role.RoleName == role.RoleName;
if (exp.CountOf<Role>() > 0)
{
throw new SPException(string.Format(SysMessage.ERROR_系统已经存在角色名称的角色, role.RoleName));
}
DaoContext.Add(role);
}
public void Delete(string[] roleid)
{
Expression exp = _Role.RoleID.In(roleid);
exp.Delete<Role>();
exp = _RolesUser.RoleID.In(roleid);
exp.Delete<RolesUser>();
}
public void SetUserRole(string[] userid, string[] roleid)
{
Expression exp;
RolesUser ru;
using (TransactionContext tc = new TransactionContext())
{
foreach (string user in userid)
{
foreach (string role in roleid)
{
exp = _RolesUser.UserID == user & _RolesUser.RoleID == role;
if (exp.CountOf<RolesUser>() == 0)
{
ru = new RolesUser();
ru.RoleID = role;
ru.UserID = user;
DaoContext.Add(ru);
}
}
}
tc.Commit();
}
}
public void CancelUserRole(string[] userid, string[] roleid)
{
Expression exp;
exp = _RolesUser.RoleID.In(roleid) & _RolesUser.UserID.In(userid);
exp.Delete<RolesUser>();
}
public IList<User> GetUsersInRole(string roleid)
{
Expression exp;
exp = _User.UserID.In(_RolesUser.UserID, _RolesUser.RoleID == roleid);
return exp.List<User>();
}
public Role Load(string roleid)
{
return DaoContext.Load<Role>(roleid);
}
public IList<Role> GetRolesForUser(string userid)
{
Expression exp;
exp = _Role.RoleID.In(_RolesUser.RoleID, _RolesUser.UserID == userid);
return exp.List<Role>();
}
#endregion
}