Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Last active August 5, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkhorikov/c8b8928aa3f0f24a62194be7d7410261 to your computer and use it in GitHub Desktop.
Save vkhorikov/c8b8928aa3f0f24a62194be7d7410261 to your computer and use it in GitHub Desktop.
Always valid vs not always valid domain model
public class Company
{
public void AssignDelivery(Delivery delivery)
{
if (!delivery.IsValid())
throw new Exception();
_deliveries.Add(delivery);
}
public void PostponeDelivery(Delivery delivery)
{
if (_deliveries.Contains(delivery))
{
_deliveries.Remove(delivery);
}
}
}
public class Company
{
[Required]
[MaxLength(200)]
public string Name { get; set; }
[Phone]
public string Phone { get; set; }
public void AssignDelivery(Delivery delivery)
{
/* ... */
}
public void PostponeDelivery(Delivery delivery)
{
/* ... */
}
}
public class CompanyDto
{
[Required]
[MaxLength(200)]
public string Name { get; set; }
[Phone]
public string Phone { get; set; }
}
public class Company
{
public void AssignDelivery(Delivery delivery)
{
/* ... */
}
public void PostponeDelivery(Delivery delivery)
{
/* ... */
}
}
public class Phone : ValueObject<Phone>
{
private string _phone;
private Phone(string phone)
{
_phone = phone;
}
public static Result<Phone> Create(string phone)
{
if (string.IsNullOrWhiteSpace(phone))
return Result.Fail<Phone>(phone);
return Result.Ok(new Phone(phone));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment