Hi all,
I ve got an employee that holds several addresses in a collection.
public class Employee
{
public string Name { get; set; }
public AddressCollection Addresses { get; }
}
public class AddressCollection : IEnumerable<Address>
{
private readonly Employee employee;
private IList<Address> items = new List<Address>();
public void AddAddress(Address address)
{
if (items.Contains(address)) return;
address.Employee = employee;
items.Add(address);
}
public void RemoveAddress(Address address)
{
items.Removce(address);
}
/// further IEnumerable implementations
}
public class Address
{
public AddressType Type { get; set; }
public City City { get; set; }
public string Street { get; set; }
public DateTime ValidFrom { get; set; }
public DateTime ValidTo { get; set; }
}
public enum AddressType
{
Default,
ShippingAddress
}
Only one address of a certain type can be valid at a time. So when adding an address of Type Default valid from 1-1-2009 to 15-1-2009 and then another address of Type Default valid from 10-1-2009 to 15-1-2009 an exception needs to be thrown.
What would be the best approach from a DDD perspective? Of course within the AddAddress method I could iterate through the existing addresses and throw an exception.
But since this business rule needs to be checked on the presentation layer to show a message to the user, shouldn't I use a specification for this which I can use internally as well as in the presentation layer?
How would you solve this?