-
-
Save vkhorikov/c8b8928aa3f0f24a62194be7d7410261 to your computer and use it in GitHub Desktop.
Always valid vs not always valid domain model
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 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); | |
} | |
} | |
} |
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 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) | |
{ | |
/* ... */ | |
} | |
} |
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 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) | |
{ | |
/* ... */ | |
} | |
} |
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 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