Original: S3K3 for user registration case brief introduction to how to use DDD
S3k3 A simple example of how to use DDD for a user registration case, I'll continue to add to this sample. S3K3 the user model first, because it seems that there is no big difference between him and the anemic model.
First, or by the domain experts to explain the business, he made the user registration successful after the need to improve personal information, including name, birthday, mobile phone number. Users also need to provide some contact information, if the address, postal code and so on. Then we can define the method according to the business. Yesterday Netfocus brother corrected the ambiguity that loginID produced, said that, so today and revised a bit.
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;
}
}
Then the front-end code is simple.
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);
}
}
The above code is well understood, and only a AddressInfo value object is designed.
The next step is to demonstrate user login verification and password changes. The general approach:
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", "account does not exist.") ");
if (user. Password! = form. Get ("Password"))
throw new Exception ("Password", "Password is incorrect.") ");
Formsauthentication.setauthcookie (user. Name, createPersistentCookie);
}
}
Please note that the above code comparison password is the wrong way, because the previous article indicates that the password is too dense. So to modify the first to inject iencryptionservice, then this will be judged