I have the following Customer class:
public class Customer : EntityBase<Customer>
{
public virtual int ID { get; set; }
public virtual CustomerType CustomerType { get; set; }
public virtual string CompanyName { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual ICollection<Address> BillingAddresses { get; set; }
public virtual ICollection<Address> ShippingAddresses { get; set; }
}
The Address class looks like this:
public class Address : EntityBase<Address>
{
public virtual int ID { get; set; }
public virtual AddressType AddressType { get; set; }
public virtual bool IsDefault { get; set; }
public virtual string Name { get; set; }
public virtual string Line1 { get; set; }
public virtual string Line2 { get; set; }
public virtual string Line3 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string ZipCode { get; set; }
public virtual string Country { get; set; }
public virtual int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
}
I want to be able to have multiple of each kind of address on the customer class, but I am having a hard time figuring that out.
I would like to end up with something like this to get to the default address for each type.
public Address DefaultBillingAddress
{
get
{
return BillingAddresses.First(x => x.IsDefault == true);
}
}
The problem I am having is how do I tell the difference between the AddressType enum on each ICollection?
You are capturing the concept of “address type” in two different ways.
You have your
AddressTypeenum onAddress, and you also have separateIt’s certainly possible with this model to end up with something in BillingAddresses with AddressType.Shipping and vice versa.
I would suggest simplifying your object model to
and providing helper methods on
Customerthat return Billing and Shipping addresses, respectively, from Addresses.Having said that, I’m not sure what you mean by
Each
ICollectionshould reference the sameAddressTypeenum.Using my suggested approach, you could write