Created
December 7, 2018 17:07
-
-
Save milannankov/b7d7372a0c524f74648f6221c4fdbce4 to your computer and use it in GitHub Desktop.
value-objects-classes
This file contains 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 | |
{ | |
private List<CompanyAddress> addresses = new List<CompanyAddress>(); | |
public Company(Guid id, string name) | |
{ | |
Assertions.AssertNotNullAndNotEmpty(name, "Must provide name"); | |
this.Id = id; | |
this.Name = name; | |
} | |
public Guid Id { get; } | |
public string Name { get; } | |
public IEnumerable<CompanyAddress> Addresses | |
{ | |
get | |
{ | |
return this.addresses; | |
} | |
} | |
public void AssignAddress(CompanyAddress address) | |
{ | |
Assertions.AssertNotNull(address, "Must provide address"); | |
var exists = this.addresses.Contains(address); | |
if (!exists) | |
{ | |
this.addresses.Add(address); | |
} | |
} | |
} |
This file contains 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 CompanyAddress : ValueObject | |
{ | |
public CompanyAddress(string city, string addressLine1) | |
{ | |
Assertions.AssertNotNullAndNotEmpty(city, "Must provide city"); | |
Assertions.AssertNotNullAndNotEmpty(addressLine1, "Must provide address line"); | |
this.City = city; | |
this.AddressLine1 = addressLine1; | |
} | |
public string City { get; } | |
public string AddressLine1 { get; } | |
protected override IEnumerable<object> GetEqualityComponents() | |
{ | |
yield return this.City; | |
yield return this.AddressLine1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment