{"id":265719,"date":"2015-09-16T06:30:02","date_gmt":"2015-09-16T02:30:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=265719"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=265719","title":{"rendered":"Functional C#: Primitive obsession (\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u0430\u043c\u0438)"},"content":{"rendered":"<p>       \u042d\u0442\u043e \u0432\u0442\u043e\u0440\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0438\u0437 \u043c\u0438\u043d\u0438\u0446\u0438\u043a\u043b\u0430 \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u0440\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 C#.<\/p>\n<ul>\n<li><a href=\"http:\/\/habrahabr.ru\/post\/266873\/\">Functional C#: Immutability<\/a>  <\/li>\n<li><b>Functional C#: Primitive obsession<\/b>  <\/li>\n<li>Functional C#: Non-nullable reference types (coming soon)  <\/li>\n<li>Functional C#: Handling failures and input errors (coming soon)  <\/li>\n<\/ul>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u0430\u043c\u0438 (Primitive obsession)?<\/h2>\n<p>  \u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e, \u0442\u043e \u044d\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043c\u0435\u043d\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b (string, int \u0438 \u0442.\u043f.). \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0432\u043e\u0442 \u043a\u0430\u043a \u043a\u043b\u0430\u0441\u0441 Customer \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0432 \u0442\u0438\u043f\u0438\u0447\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438:<\/p>\n<pre><code>public class Customer {     public string Name { get; private set; }     public string Email { get; private set; }       public Customer(string name, string email)     {         Name = name;         Email = email;     } } <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0434\u0435\u0441\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0441\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435 \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u0430\u0432\u0438\u043b, \u0432\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u043f\u043e \u0432\u0441\u0435\u043c\u0443 \u043a\u043e\u0434\u0443 \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code>public class Customer {     public string Name { get; private set; }     public string Email { get; private set; }       public Customer(string name, string email)     {         \/\/ Validate name         if (string.IsNullOrWhiteSpace(name) || name.Length &gt; 50)             throw new ArgumentException(\u201cName is invalid\u201d);           \/\/ Validate e-mail         if (string.IsNullOrWhiteSpace(email) || email.Length &gt; 100)             throw new ArgumentException(\u201cE-mail is invalid\u201d);         if (!Regex.IsMatch(email, @\u201d^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$\u201d))             throw new ArgumentException(\u201cE-mail is invalid\u201d);           Name = name;         Email = email;     }       public void ChangeName(string name)     {         \/\/ Validate name         if (string.IsNullOrWhiteSpace(name) || name.Length &gt; 50)             throw new ArgumentException(\u201cName is invalid\u201d);           Name = name;     }       public void ChangeEmail(string email)     {         \/\/ Validate e-mail         if (string.IsNullOrWhiteSpace(email) || email.Length &gt; 100)             throw new ArgumentException(\u201cE-mail is invalid\u201d);         if (!Regex.IsMatch(email, @\u201d^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$\u201d))             throw new ArgumentException(\u201cE-mail is invalid\u201d);           Email = email;     } } <\/code><\/pre>\n<p>  \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u043a\u043e\u0434 \u0438\u043c\u0435\u0435\u0442 \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u044e \u043f\u043e\u043f\u0430\u0434\u0430\u0442\u044c \u0432 application \u0441\u043b\u043e\u0439:<\/p>\n<pre><code>[HttpPost] public ActionResult CreateCustomer(CustomerInfo customerInfo) {     if (!ModelState.IsValid)         return View(customerInfo);       Customer customer = new Customer(customerInfo.Name, customerInfo.Email);     \/\/ Rest of the method }  public class CustomerInfo {     [Required(ErrorMessage = \u201cName is required\u201d)]     [StringLength(50, ErrorMessage = \u201cName is too long\u201d)]     public string Name { get; set; }       [Required(ErrorMessage = \u201cE-mail is required\u201d)]     [RegularExpression(@\u201d^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$\u201d, ErrorMessage = \u201cInvalid e-mail address\u201d)]     [StringLength(100, ErrorMessage = \u201cE-mail is too long\u201d)]     public string Email { get; set; } } <\/code><\/pre>\n<p>  \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0430\u0440\u0443\u0448\u0430\u0435\u0442 <a href=\"http:\/\/enterprisecraftsmanship.com\/2015\/09\/11\/dry-revisited\/\">\u043f\u0440\u0438\u043d\u0446\u0438\u043f DRY<\/a>. \u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e <b>\u043a\u0430\u0436\u0434\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0434\u043e\u043c\u0435\u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u043c\u0435\u0442\u044c \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0442\u0435\u0442\u043d\u044b\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0432 \u043a\u043e\u0434\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/b>. \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 \u043c\u044b \u0438\u043c\u0435\u0435\u043c 3 \u0442\u0430\u043a\u0438\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430.<\/p>\n<h2>\u041a\u0430\u043a \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u0430\u043c\u0438?<\/h2>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u0430\u043c\u0438, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0434\u0432\u0430 \u043d\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u0432 \u0441\u0435\u0431\u0435 \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code>public class Email {     private readonly string _value;       private Email(string value)     {         _value = value;     }       public static Result&lt;Email&gt; Create(string email)     {         if (string.IsNullOrWhiteSpace(email))             return Result.Fail&lt;Email&gt;(\u201cE-mail can\u2019t be empty\u201d);           if (email.Length &gt; 100)             return Result.Fail&lt;Email&gt;(\u201cE-mail is too long\u201d);           if (!Regex.IsMatch(email, @\u201d^([\\w\\.\\-]+)@([\\w\\-]+)((\\.(\\w){2,3})+)$\u201d))             return Result.Fail&lt;Email&gt;(\u201cE-mail is invalid\u201d);           return Result.Ok(new Email(email));     }       public static implicit operator string(Email email)     {         return email._value;     }       public override bool Equals(object obj)     {         Email email = obj as Email;           if (ReferenceEquals(email, null))             return false;           return _value == email._value;     }       public override int GetHashCode()     {         return _value.GetHashCode();     } }  public class CustomerName {     public static Result&lt;CustomerName&gt; Create(string name)     {         if (string.IsNullOrWhiteSpace(name))             return Result.Fail&lt;CustomerName&gt;(\u201cName can\u2019t be empty\u201d);           if (name.Length &gt; 50)             return Result.Fail&lt;CustomerName&gt;(\u201cName is too long\u201d);           return Result.Ok(new CustomerName(name));     }       \/\/ \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u0430 \u0442\u0430\u043a\u0430\u044f \u0436\u0435, \u043a\u0430\u043a Email } <\/code><\/pre>\n<p>  \u0414\u043e\u0441\u0442\u043e\u0438\u043d\u0441\u0442\u0432\u043e \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438, \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0442\u0440\u0430\u0437\u0438\u0442\u044c \u044d\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0434\u0438\u043d\u043e\u0436\u0434\u044b.<\/p>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 Email \u0437\u0430\u043a\u0440\u044b\u0442, \u0442\u0430\u043a \u0447\u0442\u043e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 Create, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442 \u0432\u0441\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e. \u042d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0431\u044b\u0442\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u043c\u0438 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u0441\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u043a\u043b\u0430\u0441\u0441\u0430 Email \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043d\u0430 \u043f\u0440\u043e\u0442\u044f\u0436\u0435\u043d\u0438\u0438 \u0432\u0441\u0435\u0439 \u0438\u0445 \u0436\u0438\u0437\u043d\u0438.<\/p>\n<p>  \u0412\u043e\u0442 \u043a\u0430\u043a \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u043a\u043b\u0430\u0441\u0441\u044b:<\/p>\n<pre><code>[HttpPost] public ActionResult CreateCustomer(CustomerInfo customerInfo) {     Result&lt;Email&gt; emailResult = Email.Create(customerInfo.Email);     Result&lt;CustomerName&gt; nameResult = CustomerName.Create(customerInfo.Name);       if (emailResult.Failure)         ModelState.AddModelError(\u201cEmail\u201d, emailResult.Error);     if (nameResult.Failure)         ModelState.AddModelError(\u201cName\u201d, nameResult.Error);       if (!ModelState.IsValid)         return View(customerInfo);       Customer customer = new Customer(nameResult.Value, emailResult.Value);     \/\/ Rest of the method } <\/code><\/pre>\n<p>  \u042d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b Result \u0438 Result \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0433\u043e\u0432\u043e\u0440\u044f\u0442 \u043d\u0430\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 Create \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0435\u0440\u043f\u0435\u0442\u044c \u043d\u0435\u0443\u0434\u0430\u0447\u0443, \u0438 \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0442\u0430\u043a, \u0442\u043e \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u0443\u0437\u043d\u0430\u0442\u044c \u043f\u0440\u0438\u0447\u0438\u043d\u0443 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e Error.<\/p>\n<p>  \u0412\u043e\u0442 \u043a\u0430\u043a \u043a\u043b\u0430\u0441\u0441 Customer \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u043e\u0441\u043b\u0435 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433\u0430:<\/p>\n<pre><code>public class Customer {     public CustomerName Name { get; private set; }     public Email Email { get; private set; }       public Customer(CustomerName name, Email email)     {         if (name == null)             throw new ArgumentNullException(\u201cname\u201d);         if (email == null)             throw new ArgumentNullException(\u201cemail\u201d);           Name = name;         Email = email;     }       public void ChangeName(CustomerName name)     {         if (name == null)             throw new ArgumentNullException(\u201cname\u201d);           Name = name;     }       public void ChangeEmail(Email email)     {         if (email == null)             throw new ArgumentNullException(\u201cemail\u201d);           Email = email;     } } <\/code><\/pre>\n<p>  \u041f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u0435\u0440\u0435\u0435\u0445\u0430\u043b\u0438 \u0432 Email \u0438 CustomerName. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u043e\u0441\u0442\u0430\u0432\u0448\u0430\u044f\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 null. \u041c\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u0438 \u043e\u0442 \u043d\u0435\u0435 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<\/p>\n<p>  \u0418\u0442\u0430\u043a, \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0434\u0430\u0435\u0442 \u043d\u0430\u043c \u0438\u0437\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u0430\u043c\u0438?<\/p>\n<ul>\n<li>\u041c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0442\u0435\u0442\u043d\u044b\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0437\u043d\u0430\u043d\u0438\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0440\u0435\u0448\u0430\u0435\u043c\u043e\u0439 \u043d\u0430\u0448\u0438\u043c \u043a\u043e\u0434\u043e\u043c. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0439, \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0438\u0441\u0442\u044b\u0439 \u0438 \u00ab\u0441\u0443\u0445\u043e\u0439\u00bb (dry) \u043a\u043e\u0434.  <\/li>\n<li>\u0411\u043e\u043b\u0435\u0435 \u0441\u0442\u0440\u043e\u0433\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0442\u0438\u043f\u043e\u0432. \u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u043d\u0430\u0441 \u0441 \u0443\u0434\u0432\u043e\u0435\u043d\u043d\u043e\u0439 \u0441\u0438\u043b\u043e\u0439: \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043e\u0448\u0438\u0431\u043e\u0447\u043d\u043e \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0443 \u0442\u0438\u043f\u0430 Email \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 CustomerName, \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d.  <\/li>\n<li>\u041d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 Email \u0438\u043b\u0438 CustomerName, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0431\u044b\u0442\u044c \u043d\u0430 100% \u0443\u0432\u0435\u0440\u0435\u043d\u044b, \u0447\u0442\u043e \u043e\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438.  <\/li>\n<\/ul>\n<p>  \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u0435. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0438\u043c\u0435\u044e\u0442 \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u044e \u00ab\u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c\u00bb \u0438 \u00ab\u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c\u00bb \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0440\u0430\u0437 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438:<\/p>\n<pre><code>public void Process(string oldEmail, string newEmail) {     Result&lt;Email&gt; oldEmailResult = Email.Create(oldEmail);     Result&lt;Email&gt; newEmailResult = Email.Create(newEmail);       if (oldEmailResult.Failure || newEmailResult.Failure)         return;       string oldEmailValue = oldEmailResult.Value;     Customer customer = GetCustomerByEmail(oldEmailValue);     customer.Email = newEmailResult.Value; } <\/code><\/pre>\n<p>  \u041b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0432\u043e \u0432\u0441\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044f \u0438\u0445 \u0432 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0434\u043e\u043c\u0435\u043d\u0430, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0431\u0430\u0437\u0443 \u0438\u043b\u0438 \u0440\u0435\u043d\u0434\u0435\u0440\u044f\u0442\u0441\u044f \u0432 HTML. \u0412 \u0432\u0430\u0448\u0438\u0445 \u0434\u043e\u043c\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u0441\u0442\u0430\u0440\u0430\u0439\u0442\u0435\u0441\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0435 \u0442\u0438\u043f\u044b, \u043a\u043e\u0434 \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0438 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c:<\/p>\n<pre><code>public void Process(Email oldEmail, Email newEmail) {     Customer customer = GetCustomerByEmail(oldEmail);     customer.Email = newEmail; } <\/code><\/pre>\n<p>  <\/p>\n<h2>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f<\/h2>\n<p>  \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432-\u043e\u0431\u0435\u0440\u0442\u043e\u043a \u0432 C# \u2014 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0435 \u043d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043a\u0430\u043a \u0432 \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u0432 F#. \u042d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u0432 C# 7 \u0435\u0441\u043b\u0438 \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d pattern matching \u0438 record types \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u044f\u0437\u044b\u043a\u0430. \u0414\u043e \u0442\u043e\u0433\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430, \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u043c\u0435\u0442\u044c \u0434\u0435\u043b\u043e \u0441 \u043d\u0435\u0443\u043a\u043b\u044e\u0436\u0435\u0441\u0442\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430.<\/p>\n<p>  \u0418\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043d\u0435 \u0441\u0442\u043e\u044f\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u0442\u044c \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044b\u043c\u0438. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0442\u0438\u043f \u00abmoney amount\u00bb \u0441 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0438\u043d\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u043c, \u0433\u043e\u0432\u043e\u0440\u044f\u0449\u0438\u043c \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0435\u043d\u0435\u0433 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c, \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0439 decimal. \u042d\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u043b\u043e\u0433\u0438\u043a\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438, \u043d\u043e \u0434\u0430\u0436\u0435 \u043d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u044d\u0442\u043e, \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c, \u0434\u0430\u0436\u0435 \u0432 \u0434\u043e\u043b\u0433\u043e\u0441\u0440\u043e\u0447\u043d\u043e\u0439 \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u0435.<\/p>\n<p>  \u041a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u043e, \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u0437\u0434\u0440\u0430\u0432\u043e\u0433\u043e \u0441\u043c\u044b\u0441\u043b\u0430 \u0438 \u0432\u0437\u0432\u0435\u0448\u0438\u0432\u0430\u0439\u0442\u0435 \u043f\u043b\u044e\u0441\u044b \u0438 \u043c\u0438\u043d\u0443\u0441\u044b \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>  \u0421 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c\u0438 \u0438 \u043d\u0435\u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u043c\u044b \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u043c \u0431\u043b\u0438\u0436\u0435 \u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 C# \u0432 \u0431\u043e\u043b\u0435\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u0442\u0438\u043b\u0435. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u043e\u0431\u0441\u0443\u0434\u0438\u043c \u043a\u0430\u043a \u043e\u0431\u043b\u0435\u0433\u0447\u0438\u0442\u044c \u00ab\u043e\u0448\u0438\u0431\u043a\u0443 \u043d\u0430 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434 \u0434\u043e\u043b\u043b\u0430\u0440\u043e\u0432\u00bb (mitigate the billion dollar mistake).<\/p>\n<h2>\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438<\/h2>\n<p>  <\/p>\n<ul>\n<li><a href=\"https:\/\/gist.github.com\/vkhorikov\/5f0c7167250edb17cd2f\">\u041a\u043e\u0434 \u0441 primitive obsession<\/a>  <\/li>\n<li><a href=\"https:\/\/gist.github.com\/vkhorikov\/063953c90f5d60ddc24b\">\u041a\u043e\u0434 \u0431\u0435\u0437 primitive obsession<\/a>  <\/li>\n<\/ul>\n<p>  <\/p>\n<h2>\u041e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435<\/h2>\n<p>  <\/p>\n<ul>\n<li><a href=\"http:\/\/habrahabr.ru\/post\/266873\/\">Functional C#: Immutability<\/a>  <\/li>\n<li><b>Functional C#: Primitive obsession<\/b>  <\/li>\n<li>Functional C#: Non-nullable reference types (coming soon)  <\/li>\n<li>Functional C#: Handling failures and input errors (coming soon)  <\/li>\n<\/ul>\n<p>  \u0410\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438: <a href=\"http:\/\/enterprisecraftsmanship.com\/2015\/03\/07\/functional-c-primitive-obsession\/\">Functional C#: Immutability<\/a>               <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/266937\/\"> http:\/\/habrahabr.ru\/post\/266937\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u042d\u0442\u043e \u0432\u0442\u043e\u0440\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0438\u0437 \u043c\u0438\u043d\u0438\u0446\u0438\u043a\u043b\u0430 \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u0440\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 C#.<\/p>\n<ul>\n<li><a href=\"http:\/\/habrahabr.ru\/post\/266873\/\">Functional C#: Immutability<\/a>  <\/li>\n<li><b>Functional C#: Primitive obsession<\/b>  <\/li>\n<li>Functional C#: Non-nullable reference types (coming soon)  <\/li>\n<li>Functional C#: Handling failures and input errors (coming soon)  <\/li>\n<\/ul>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-265719","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/265719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=265719"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/265719\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=265719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=265719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=265719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}