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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值