Create a gist now

Instantly share code, notes, and snippets.

@vkhorikov /1.cs
Last active Aug 5, 2017

What would you like to do?
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