S3K3 for user registration A simple example of how to use DDD

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.