S3K3针对用户注册案例简单介绍了如何使用 DDD
2014-08-05 11:19
597 查看
原文:S3K3针对用户注册案例简单介绍了如何使用 DDD
S3K3针对用户注册案例简单介绍了如何使用 DDD,接下来我将继续针对这个例子做一下补充。s3k3 先将User模型丰富起来,因为目前看上去他和贫血模型还没有啥大的区别。
首先还是由领域专家来说明业务,他由提出了用户注册成功后需要完善个人信息,这些信息包括姓名、生日、手机号。还需要用户提供一些联系信息,如果地址,邮编等。那么我们就可以根据业务定义方法了。昨天netfocus兄指正了loginid所产生的歧义,表示认为,所以今天一并修改了一下。
public class AddressInfo
{
public AddressInfo(string province, string city, string address, string postcode)
{
this.Province = province;
this.City = city;
this.Address = address;
this.Postcode = postcode;
}
public string Province { get; private set; }
public string City { get; private set; }
public string Address { get; private set; }
public string Postcode { get; private set; }
}
public class User
{
public User(string name, string password, string email)
{
this.Name = name;
this.Password = password;
this.Email = email;
}
public string Id { get; private set; }
public string Name { get; private set; }
public string Password { get; private set; }
public string RealName { get; private set; }
public string Email { get; private set; }
public string Cellphone { get; private set; }
public string Birthday { get; private set; }
public AddressInfo Address { get; private set; }
public void UpdateBasicInfo(string realName, string birthday, string cellphone)
{
this.RealName = realName;
this.Birthday = birthday;
this.Cellphone = cellphone;
}
public void UpdateAddress(AddressInfo address)
{
this.Address = address;
}
}
那么前端的代码也很简单
public class UserController
{
private readonly IUserRepository _userRepository;
public void SetProfile(FormCollection form)
{
var user = _userRepository.Get(form.Get("id"));
user.UpdateBasicInfo(form.Get("name"), form.Get("birthday"), form.Get("cellphone"));
}
public void SetAddress(FormCollection form)
{
var user = _userRepository.Get(form.Get("id"));
var address = new AddressInfo(form.Get("province"), form.Get("city"),
form.Get("address"), form.Get("postcode"));
user.UpdateAddress(address);
}
}
以上的代码很好理解,只是设计了一个AddressInfo的值对象。
接下来将演示一下用户登录验证和修改密码。一般的做法:
public interface IUserRepository
{
User GetByName(string loginId);
}
public class UserController
{
private readonly IUserRepository _userRepository;
public UserController(IUserRepository userRepository)
{
this._userRepository = userRepository;
}
public void Logon(FormCollection form)
{
User user = _userRepository.GetByName(form.Get("LoginId"));
if (user == null)
throw new Exception("loginId", "账号不存在。");
if (user.Password != form.Get("Password"))
throw new Exception("password", "密码不正确。");
FormsAuthentication.SetAuthCookie(user.Name, createPersistentCookie);
}
}
请注意上述代码比较密码是错误的方式,因为上一篇说明了密码是加过密的。所以要修改一下,首先要注入IEncryptionService,那么就会这样判断
S3K3针对用户注册案例简单介绍了如何使用 DDD,接下来我将继续针对这个例子做一下补充。s3k3 先将User模型丰富起来,因为目前看上去他和贫血模型还没有啥大的区别。
首先还是由领域专家来说明业务,他由提出了用户注册成功后需要完善个人信息,这些信息包括姓名、生日、手机号。还需要用户提供一些联系信息,如果地址,邮编等。那么我们就可以根据业务定义方法了。昨天netfocus兄指正了loginid所产生的歧义,表示认为,所以今天一并修改了一下。
public class AddressInfo
{
public AddressInfo(string province, string city, string address, string postcode)
{
this.Province = province;
this.City = city;
this.Address = address;
this.Postcode = postcode;
}
public string Province { get; private set; }
public string City { get; private set; }
public string Address { get; private set; }
public string Postcode { get; private set; }
}
public class User
{
public User(string name, string password, string email)
{
this.Name = name;
this.Password = password;
this.Email = email;
}
public string Id { get; private set; }
public string Name { get; private set; }
public string Password { get; private set; }
public string RealName { get; private set; }
public string Email { get; private set; }
public string Cellphone { get; private set; }
public string Birthday { get; private set; }
public AddressInfo Address { get; private set; }
public void UpdateBasicInfo(string realName, string birthday, string cellphone)
{
this.RealName = realName;
this.Birthday = birthday;
this.Cellphone = cellphone;
}
public void UpdateAddress(AddressInfo address)
{
this.Address = address;
}
}
那么前端的代码也很简单
public class UserController
{
private readonly IUserRepository _userRepository;
public void SetProfile(FormCollection form)
{
var user = _userRepository.Get(form.Get("id"));
user.UpdateBasicInfo(form.Get("name"), form.Get("birthday"), form.Get("cellphone"));
}
public void SetAddress(FormCollection form)
{
var user = _userRepository.Get(form.Get("id"));
var address = new AddressInfo(form.Get("province"), form.Get("city"),
form.Get("address"), form.Get("postcode"));
user.UpdateAddress(address);
}
}
以上的代码很好理解,只是设计了一个AddressInfo的值对象。
接下来将演示一下用户登录验证和修改密码。一般的做法:
public interface IUserRepository
{
User GetByName(string loginId);
}
public class UserController
{
private readonly IUserRepository _userRepository;
public UserController(IUserRepository userRepository)
{
this._userRepository = userRepository;
}
public void Logon(FormCollection form)
{
User user = _userRepository.GetByName(form.Get("LoginId"));
if (user == null)
throw new Exception("loginId", "账号不存在。");
if (user.Password != form.Get("Password"))
throw new Exception("password", "密码不正确。");
FormsAuthentication.SetAuthCookie(user.Name, createPersistentCookie);
}
}
请注意上述代码比较密码是错误的方式,因为上一篇说明了密码是加过密的。所以要修改一下,首先要注入IEncryptionService,那么就会这样判断
相关文章推荐
- 如何在用户注册或登录时使用验证码图片
- 一步一步SharePoint 2007之十八:解决允许使用简单密码注册用户的问题
- 网络学习(二十二)简单介绍如何破解Windows XP 用户密码
- 文章简单介绍如何在iOS中使用libxml
- ASP.NET jQuery 实例12 通过使用jQuery validation插件简单实现用户注册页面验证功能
- 解决“允许使用简单密码注册用户”后出现的问题
- 简单介绍VC++6.0下如何使用压缩库ZLIB(转)
- c#如何使用webservice、存储过程及存储过程的创建(简单模仿类似QQ统计用户在线时间为例)
- 简单介绍如何使用amCharts
- 如何在Domino中使用文本文件注册用户
- 一步一步SharePoint 2007之十八:解决允许使用简单密码注册用户的问题
- 如何在Domino中使用文本文件注册用户
- 简单介绍VC++6.0下如何使用压缩库ZLIB
- c#如何使用webservice、存储过程及存储过程的创建(简单模仿类似QQ统计用户在线时间为例)
- 使用MD5加密注册用户密码的简单示例
- XNA Game Studio是一套有着强大功能和简单界面的游戏制作平台,游戏开发商和游戏玩家都可以使用这套工具开发针对Windows XP以及Xbox360的游戏,XNA Game Studio分为两种版本,一种是面向初学者的EXPRESS版本,还有一种是面向专业用户的专业版。用户使用EXPRESS版本开发游戏完全免费,并且可以随意在PC上发行,不过用该工具开发的游戏若是在360上网络发行,就需要交纳99美元的年费。
- [转]简单介绍如何使用robotium进行自动化测试
- 通过一个简单的例子,介绍如何使用LDAP(转)
- 简单介绍如何使用robotium进行自动化测试
- ASP.NET jQuery 食谱12 (通过使用jQuery validation插件简单实现用户注册页面验证功能)