On Thu, Feb 12, 2009 at 9:36 AM, Hendry Luk <[email protected]> wrote:

> Unfortunately, it's also mapped to legacy database structure, where we have
> an associative table:
> create table CUSTOMERS_ADDRESSES
> (
>     customer_id int,
>     address_id int,
>     address_type varchar(10)
> )
>
> Where address_type holds 'HOME' or 'WORK' or 'BILLING'.
>


The easiest way I can think of is to create a lazy-load one-to-many
association.

This comes with a tradeoff that when you get an address, it would load
all other addresses as well. While it comes with tradeoffs, it matches the
situatioin. First, the legacy tables already support it. Second, it can be
lazy-loaded (I'm not sure what kind of null support you need). Third, it
maintains the semantics when you want to get/set each address.

That way, it should come like this:

public class Customer
{
    private Address GetAddress(string addressType)
    {
        return this.Addresses.SingleOrDefault(address =>
                                              address.AddressType ==
addressType);
    }

    private void SetAddress(Address address)
    {
        ... // do whatever you like (check before adding, etc.)
    }

    public void HomeAddress
    {
        get { return this.GetAddress("HOME"); }
        set { this.SetAddress(value); }
    }

    ... // and the rest addresses implementation for each type you want

    public ICollection<Address> Addresses { get; set; }
}

HTH.






-- 
Regards,

Maximilian Haru Raditya

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to