Last active
June 22, 2024 11:38
-
-
Save vkhorikov/5f0c7167250edb17cd2f to your computer and use it in GitHub Desktop.
With primitive obsession
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 > 50) | |
throw new ArgumentException("Name is invalid"); | |
// Validate e-mail | |
if (string.IsNullOrWhiteSpace(email) || email.Length > 100) | |
throw new ArgumentException("E-mail is invalid"); | |
if (!Regex.IsMatch(email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$")) | |
throw new ArgumentException("E-mail is invalid"); | |
Name = name; | |
Email = email; | |
} | |
public void ChangeName(string name) | |
{ | |
// Validate name | |
if (string.IsNullOrWhiteSpace(name) || name.Length > 50) | |
throw new ArgumentException("Name is invalid"); | |
Name = name; | |
} | |
public void ChangeEmail(string email) | |
{ | |
// Validate e-mail | |
if (string.IsNullOrWhiteSpace(email) || email.Length > 100) | |
throw new ArgumentException("E-mail is invalid"); | |
if (!Regex.IsMatch(email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$")) | |
throw new ArgumentException("E-mail is invalid"); | |
Email = email; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerController : Controller | |
{ | |
[HttpPost] | |
public ActionResult CreateCustomer(CustomerInfo customerInfo) | |
{ | |
if (!ModelState.IsValid) | |
return View(customerInfo); | |
Customer customer = new Customer(customerInfo.Name, customerInfo.Email); | |
// Rest of the method | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerInfo | |
{ | |
[Required(ErrorMessage = "Name is required")] | |
[StringLength(50, ErrorMessage = "Name is too long")] | |
public string Name { get; set; } | |
[Required(ErrorMessage = "E-mail is required")] | |
[RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", | |
ErrorMessage = "Invalid e-mail address")] | |
[StringLength(100, ErrorMessage = "E-mail is too long")] | |
public string Email { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment