Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Last active January 29, 2022 14:02
Show Gist options
  • Save vkhorikov/063953c90f5d60ddc24b to your computer and use it in GitHub Desktop.
Save vkhorikov/063953c90f5d60ddc24b to your computer and use it in GitHub Desktop.
Without primitive obsession
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("name");
if (email == null)
throw new ArgumentNullException("email");
Name = name;
Email = email;
}
public void ChangeName(CustomerName name)
{
if (name == null)
throw new ArgumentNullException("name");
Name = name;
}
public void ChangeEmail(Email email)
{
if (email == null)
throw new ArgumentNullException("email");
Email = email;
}
}
public class CustomerController : Controller
{
[HttpPost]
public ActionResult CreateCustomer(CustomerInfo customerInfo)
{
Result<Email> emailResult = Email.Create(customerInfo.Email);
Result<CustomerName> nameResult = CustomerName.Create(customerInfo.Name);
if (emailResult.Failure)
ModelState.AddModelError("Email", emailResult.Error);
if (nameResult.Failure)
ModelState.AddModelError("Name", nameResult.Error);
if (!ModelState.IsValid)
return View(customerInfo);
Customer customer = new Customer(nameResult.Value, emailResult.Value);
// Rest of the method
}
}
public class CustomerInfo
{
public string Name { get; set; }
public string Email { get; set; }
}
public class CustomerName
{
private readonly string _value;
private CustomerName(string value)
{
_value = value;
}
public static Result<CustomerName> Create(string name)
{
if (string.IsNullOrWhiteSpace(name))
return Result.Fail<CustomerName>("Name can't be empty");
if (name.Length > 50)
return Result.Fail<CustomerName>("Name is too long");
return Result.Ok(new CustomerName(name));
}
public static implicit operator string(CustomerName name)
{
return name._value;
}
public override bool Equals(object obj)
{
CustomerName name = obj as CustomerName;
if (ReferenceEquals(name, null))
return false;
return _value == name._value;
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
}
public class Email
{
private readonly string _value;
private Email(string value)
{
_value = value;
}
public static Result<Email> Create(string email)
{
if (string.IsNullOrWhiteSpace(email))
return Result.Fail<Email>("E-mail can't be empty");
if (email.Length > 100)
return Result.Fail<Email>("E-mail is too long");
if (!Regex.IsMatch(email, @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"))
return Result.Fail<Email>("E-mail is invalid");
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment