Asterisk2B用户管理逻辑设计
前时间一直在搞Asterisk,所以打算用Asp.net结合Flex做个Asterisk的业务系统,已经在Google Code申请了http://code.google.com/p/asterisk2b/ 由于用空闲时间来做,所以进度比较难以保证。在这里会慢慢的介绍在写Asterisk2B时对于业务的设计,第一步先由用户管理逻辑开始。
用户数据是系统的基础在Asterisk2B中也不例外,首先看下需要有那些功能:用户注册,查询,信息修改等等。
根据需求定制实体对象
Code
/// <summary>
/// 业务系统用户表
/// </summary>
[Table("users")]
interface IUser
{
[UID]
[ID]
string ID { get; set; }
/// <summary>
/// 用户名
/// </summary>
[Column]
[Unique("用户名已经被使用!")]
[NotNull("用户名不能为空!")]
string UserName { get; set; }
/// <summary>
/// 用户密码
/// </summary>
[Column]
[NotNull("用户密码不能为空!")]
string PWD { get; set; }
/// <summary>
/// 邮件地址
/// </summary>
[Column]
[Unique("邮件地址已经被使用!")]
[NotNull("邮件地址不能为空!")]
string EMail { get; set; }
/// <summary>
/// 上级代码
/// </summary>
[Column]
string Parent { get; set; }
/// <summary>
/// 创建日期
/// </summary>
[Column]
DateTime CreateDate { get; set; }
/// <summary>
/// 会员等级
/// </summary>
[Column]
[EnumToString]
[DefaultEnum]
MemberLevel MemberLevel { get; set; }
/// <summary>
/// 用户类型
/// </summary>
[Column]
[EnumToString]
[DefaultEnum]
UserType Type { get; set; }
/// <summary>
/// 用户是否有效
/// </summary>
[Column]
[Enabled]
bool Enabled { get; set; }
}
[Table("account")]
interface IAccount
{
[ID]
string UserID { get; set; }
/// <summary>
/// 电子金额
/// </summary>
[Column]
[DefaultDecimal("0")]
decimal Amount { get; set; }
/// <summary>
/// 可拔打电话费用
/// </summary>
[Column]
[DefaultDecimal("1")]
decimal PhoneFee { get; set; }
/// <summary>
/// 现在话费
/// </summary>
[Column]
[DefaultDecimal("0")]
decimal TotlePhoneFee { get; set; }
}
/// <summary>
/// 用户基础信息
/// </summary>
[Table("users inner join account on users.id=account.userid")]
interface ISelectUserBaseInfo
{
[ID]
string ID { get; set; }
/// <summary>
/// 用户名
/// </summary>
[Column]
string UserName { get; set; }
/// <summary>
/// 用户等级
/// </summary>
[Column]
[EnumToString]
[DefaultEnum]
MemberLevel MemberLevel { get; set; }
/// <summary>
/// 电子金额
/// </summary>
[Column]
decimal Amount { get; set; }
/// <summary>
/// 可拔打费用
/// </summary>
[Column]
decimal PhoneFee { get; set; }
/// <summary>
/// 现有话费
/// </summary>
[Column]
decimal TotlePhoneFee { get; set; }
}
/// <summary>
/// 业务系统用户表
/// </summary>
[Table("users")]
interface IUser
{
[UID]
[ID]
string ID { get; set; }
/// <summary>
/// 用户名
/// </summary>
[Column]
[Unique("用户名已经被使用!")]
[NotNull("用户名不能为空!")]
string UserName { get; set; }
/// <summary>
/// 用户密码
/// </summary>
[Column]
[NotNull("用户密码不能为空!")]
string PWD { get; set; }
/// <summary>
/// 邮件地址
/// </summary>
[Column]
[Unique("邮件地址已经被使用!")]
[NotNull("邮件地址不能为空!")]
string EMail { get; set; }
/// <summary>
/// 上级代码
/// </summary>
[Column]
string Parent { get; set; }
/// <summary>
/// 创建日期
/// </summary>
[Column]
DateTime CreateDate { get; set; }
/// <summary>
/// 会员等级
/// </summary>
[Column]
[EnumToString]
[DefaultEnum]
MemberLevel MemberLevel { get; set; }
/// <summary>
/// 用户类型
/// </summary>
[Column]
[EnumToString]
[DefaultEnum]
UserType Type { get; set; }
/// <summary>
/// 用户是否有效
/// </summary>
[Column]
[Enabled]
bool Enabled { get; set; }
}
[Table("account")]
interface IAccount
{
[ID]
string UserID { get; set; }
/// <summary>
/// 电子金额
/// </summary>
[Column]
[DefaultDecimal("0")]
decimal Amount { get; set; }
/// <summary>
/// 可拔打电话费用
/// </summary>
[Column]
[DefaultDecimal("1")]
decimal PhoneFee { get; set; }
/// <summary>
/// 现在话费
/// </summary>
[Column]
[DefaultDecimal("0")]
decimal TotlePhoneFee { get; set; }
}
/// <summary>
/// 用户基础信息
/// </summary>
[Table("users inner join account on users.id=account.userid")]
interface ISelectUserBaseInfo
{
[ID]
string ID { get; set; }
/// <summary>
/// 用户名
/// </summary>
[Column]
string UserName { get; set; }
/// <summary>
/// 用户等级
/// </summary>
[Column]
[EnumToString]
[DefaultEnum]
MemberLevel MemberLevel { get; set; }
/// <summary>
/// 电子金额
/// </summary>
[Column]
decimal Amount { get; set; }
/// <summary>
/// 可拔打费用
/// </summary>
[Column]
decimal PhoneFee { get; set; }
/// <summary>
/// 现有话费
/// </summary>
[Column]
decimal TotlePhoneFee { get; set; }
}
User:是用户基础信息表,通过一些Attribute把一些简单的数据验证和默认值处理了,这样能省下一些逻辑代码.
Account:是用户的费用表
SelectUserBaseInfo:是基础信息描述,毕竟在查询中很多用户信息是不需要的。
适当的使用枚举类型来代替数字或字符来描述不同情况的字段,这样在编写程序上会省一些事情,毕竟枚举是有约束的。
制定业务逻辑
Code
public class UserService
{
static object mLockRegister = new object();
//用户注册
public void Register(Entities.User user)
{
lock (mLockRegister)
{
DBContext.TransactionExecute(o =>
{
DBContext.Add(user);
Account account = new Account();
account.UserID = user.ID;
DBContext.Add(account);
});
}
}
//更改用户等级
public void SetLevel(string userid, MemberLevel level)
{
(User.iD == userid).Edit<User>(o => { o.MemberLevel = level; });
}
//设置用户有效性
public void SetEnabled(string userid, bool enabled)
{
(User.iD == userid).Edit<User>(o => { o.Enabled = enabled; });
}
//删除用户
public void Delete(string id)
{
(User.iD == id).Delete<User>();
}
//查询用户
public IList<SelectUserBaseInfo> List(string name, Smark.Core.IDataPage dp)
{
Expression exp = new Expression();
exp &= (User.parent.IsNull() | User.parent == "");
if(!string.IsNullOrEmpty(name))
exp &= User.userName.Like(name);
dp.RecordCount = exp.Count<SelectUserBaseInfo>();
return exp.List<SelectUserBaseInfo>(new Region(dp.PageIndex, dp.PageSize), null);
}
//修改用户类型
public void SetUserType(string userid, UserType type)
{
(User.iD == userid).Edit<User>(o => { o.Type = type; });
}
//修改用户密码
public void ChangePWD(string userid, string newpwd)
{
(User.iD == userid).Edit<User>(o => { o.PWD = newpwd; });
}
//修改邮件地址
public void ChangeEmail(string userid, string email)
{
(User.iD == userid).Edit<User>(o => { o.EMail = email; });
}
//获取用户信息
public User Load(string userid)
{
return DBContext.Load<User>(userid);
}
//根据用户名获取用户信息
public User LoadByName(string name)
{
return (User.userName == name).ListFirst<User>();
}
//用户登陆
public User Login(string name, string pwd)
{
User item = (User.userName == name).ListFirst<User>();
if (item == null || item.PWD != pwd)
A2BExcetion.USER_用户名或密码不正确();
return item;
}
//获取帐号信息
public Account LoadAccount(string userid)
{
return DBContext.Load<Account>(userid);
}
}
public class UserService
{
static object mLockRegister = new object();
//用户注册
public void Register(Entities.User user)
{
lock (mLockRegister)
{
DBContext.TransactionExecute(o =>
{
DBContext.Add(user);
Account account = new Account();
account.UserID = user.ID;
DBContext.Add(account);
});
}
}
//更改用户等级
public void SetLevel(string userid, MemberLevel level)
{
(User.iD == userid).Edit<User>(o => { o.MemberLevel = level; });
}
//设置用户有效性
public void SetEnabled(string userid, bool enabled)
{
(User.iD == userid).Edit<User>(o => { o.Enabled = enabled; });
}
//删除用户
public void Delete(string id)
{
(User.iD == id).Delete<User>();
}
//查询用户
public IList<SelectUserBaseInfo> List(string name, Smark.Core.IDataPage dp)
{
Expression exp = new Expression();
exp &= (User.parent.IsNull() | User.parent == "");
if(!string.IsNullOrEmpty(name))
exp &= User.userName.Like(name);
dp.RecordCount = exp.Count<SelectUserBaseInfo>();
return exp.List<SelectUserBaseInfo>(new Region(dp.PageIndex, dp.PageSize), null);
}
//修改用户类型
public void SetUserType(string userid, UserType type)
{
(User.iD == userid).Edit<User>(o => { o.Type = type; });
}
//修改用户密码
public void ChangePWD(string userid, string newpwd)
{
(User.iD == userid).Edit<User>(o => { o.PWD = newpwd; });
}
//修改邮件地址
public void ChangeEmail(string userid, string email)
{
(User.iD == userid).Edit<User>(o => { o.EMail = email; });
}
//获取用户信息
public User Load(string userid)
{
return DBContext.Load<User>(userid);
}
//根据用户名获取用户信息
public User LoadByName(string name)
{
return (User.userName == name).ListFirst<User>();
}
//用户登陆
public User Login(string name, string pwd)
{
User item = (User.userName == name).ListFirst<User>();
if (item == null || item.PWD != pwd)
A2BExcetion.USER_用户名或密码不正确();
return item;
}
//获取帐号信息
public Account LoadAccount(string userid)
{
return DBContext.Load<Account>(userid);
}
}
虽然业务逻辑制定了,但实际情况我们还要把业务逻辑发布到网上给Flex调用。对于现在的业务逻辑都是集中的,这样对于不同情况调用逻辑还是比较难控制。
再根据不同情况划分服务.
所有用户都能访问的
Code
[Service]
public class Public
{
public void Register(Entities.User user)
{
Services.User.Register(user);
}
public Entities.User Login(string username, string pwd)
{
Entities.User user = Services.User.Login(username, pwd);
Smark.Web.MVCContext.Current.SetUserName(user.UserName, null);
return user;
}
public void ChangeEMail(string username, string pwd, string newemail)
{
Entities.User user = Services.User.LoadByName(username);
if (user == null || user.PWD != pwd)
Business.A2BExcetion.USER_用户名或密码不正确();
Services.User.ChangeEmail(user.ID, newemail);
}
public void ChangePWD(string username, string pwd, string newpwd)
{
Entities.User user = Services.User.LoadByName(username);
if (user == null || user.PWD != pwd)
Business.A2BExcetion.USER_用户名或密码不正确();
Services.User.ChangePWD(user.ID, newpwd);
}
public void LoginOut()
{
Smark.Web.MVCContext.Current.SetUserName(null, null);
}
}
[Service]
public class Public
{
public void Register(Entities.User user)
{
Services.User.Register(user);
}
public Entities.User Login(string username, string pwd)
{
Entities.User user = Services.User.Login(username, pwd);
Smark.Web.MVCContext.Current.SetUserName(user.UserName, null);
return user;
}
public void ChangeEMail(string username, string pwd, string newemail)
{
Entities.User user = Services.User.LoadByName(username);
if (user == null || user.PWD != pwd)
Business.A2BExcetion.USER_用户名或密码不正确();
Services.User.ChangeEmail(user.ID, newemail);
}
public void ChangePWD(string username, string pwd, string newpwd)
{
Entities.User user = Services.User.LoadByName(username);
if (user == null || user.PWD != pwd)
Business.A2BExcetion.USER_用户名或密码不正确();
Services.User.ChangePWD(user.ID, newpwd);
}
public void LoginOut()
{
Smark.Web.MVCContext.Current.SetUserName(null, null);
}
}
登陆用户可访问的
Code
public class OwnerIntercept:Smark.Service.Intercept
{
protected override void OnInit()
{
CreateFilter<Owner>();
}
public override void Execute(Smark.Service.MethodContext context)
{
if (Services.Loginer == null)
Business.A2BExcetion.SYSTEM_非法操作资源();
context.Execute();
}
}
[Smark.Service.Service]
public class Owner
{
public void MemberAdd( Entities.User user)
{
Services.Member.Add(Services.Loginer.ID, user);
}
public Entities.User LoadInfo()
{
return Services.User.Load(Services.Loginer.ID);
}
public Entities.Account LoadAccount()
{
return Services.User.LoadAccount(Services.Loginer.ID);
}
public void ChangePWD(string newpwd)
{
Services.User.ChangePWD(Services.Loginer.ID);
}
public void ChangeEMail(string newemail)
{
Services.User.ChangeEmail(Services.Loginer.ID, newemail);
}
public IList<Entities.SelectUserBaseInfo> MemberList(string name, [Output]Smark.Core.DataPage datapage)
{
IList<Entities.SelectUserBaseInfo> result = Services.Member.List(Services.Loginer.ID, name, datapage);
return result;
}
public void MemberDelete(string[] userid)
{
Services.Member.Delete(Services.Loginer.ID, userid);
}
}
public class OwnerIntercept:Smark.Service.Intercept
{
protected override void OnInit()
{
CreateFilter<Owner>();
}
public override void Execute(Smark.Service.MethodContext context)
{
if (Services.Loginer == null)
Business.A2BExcetion.SYSTEM_非法操作资源();
context.Execute();
}
}
[Smark.Service.Service]
public class Owner
{
public void MemberAdd( Entities.User user)
{
Services.Member.Add(Services.Loginer.ID, user);
}
public Entities.User LoadInfo()
{
return Services.User.Load(Services.Loginer.ID);
}
public Entities.Account LoadAccount()
{
return Services.User.LoadAccount(Services.Loginer.ID);
}
public void ChangePWD(string newpwd)
{
Services.User.ChangePWD(Services.Loginer.ID);
}
public void ChangeEMail(string newemail)
{
Services.User.ChangeEmail(Services.Loginer.ID, newemail);
}
public IList<Entities.SelectUserBaseInfo> MemberList(string name, [Output]Smark.Core.DataPage datapage)
{
IList<Entities.SelectUserBaseInfo> result = Services.Member.List(Services.Loginer.ID, name, datapage);
return result;
}
public void MemberDelete(string[] userid)
{
Services.Member.Delete(Services.Loginer.ID, userid);
}
}