S3K3针对用户注册案例简单介绍了如何使用 DDD

原文: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,那么就会这样判断



相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...