Search code examples
nhibernatefluent-nhibernatemappinghierarchylegacy

How to map this legacy table with NHibernate (Fluent)?


New to NHibernate. Having trouble wrapping my head around how to map this legacy table.

CREATE TABLE [dbo].[CmnAddress](
[addressId] [int] NOT NULL,
[objectType] [varchar](63) NULL,
[objectId] [int] NULL,
[addressType] [varchar](7) NULL,
[recordStatus] [char](1) NULL,
[fromDate] [int] NULL,
[toDate] [int] NULL,
[onStreet] [varchar](254) NULL,
[atStreet] [varchar](254) NULL,
[unit] [varchar](30) NULL,
[city] [varchar](254) NULL,
[state] [varchar](30) NULL,
[zipCode] [varchar](30) NULL,
)

There is also a "CmnPerson" table that I have mapped to a Person class. I need the Person class to contain a list of Addresses, where the objectType column contains "CmnPerson" and the objectId field matches my Person.Id ("CmnPerson.personId") field.

I am also later going to have to create a Contact class that also contains a list of Addresses where the objectType column contains "CmnContact".

I'm having a very tough time figuring out if I should be using the Any mapping or class-hierarchy-per-table with discrimination on subcolumns? Or if either of those will even meet my needs.

Can anyone show my how to map this Address class? Fluent config would be preferable.

ADDED INFO:

The following classes and mappings almost work, but the Addresses list return ALL rows from the CmnAddress table with a matching objectId, regardless of the objectType field's value. I think I could use an ApplyFilter on the HasMany mapping for Person.Addresses, but that doesn't seem like the "right" way.

More added info: I was able to resolve this last issue by chaining AlwaysSelectWithValue() on after the call to DiscriminateSubClassesOnColumn(...)

public class Person
{
    public virtual int Id { get; private set; }
    public virtual string LastName { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string MiddleName { get; set; }
    public virtual string Gender { get; set; }

    public virtual IList<PassClient> PassClients { get; set; }
    public virtual IList<PersonAddress> Addresses { get; set; }
}

public class PersonMap : ClassMap<Person>
{
    public PersonMap() {
        Table("CmnPerson");
        Id(x => x.Id).Column("personId");
        Map(x => x.LastName);
        Map(x => x.FirstName);
        Map(x => x.MiddleName);
        Map(x => x.Gender);

        HasMany(x => x.PassClients).KeyColumn("personId");
        HasMany(x => x.Addresses).KeyColumn("objectId");
    }
}

abstract public class Address
{
    public virtual int Id { get; private set; }
    public virtual string StreetNo { get; set; }
    public virtual string OnStreet { get; set; }
    public virtual string Unit { get; set; }
    public virtual string City { get; set; }
    public virtual string State { get; set; }
    public virtual string ZipCode { get; set; }
}

public class PersonAddress : Address { 
    public virtual Person Person { get; set; }
}

public class AddressMap : ClassMap<Address>
{
    public AddressMap() {
        Table("CmnAddress");

        Id(x => x.Id).Column("addressId");
        Map(x => x.StreetNo);
        Map(x => x.OnStreet);
        Map(x => x.Unit);
        Map(x => x.City);
        Map(x => x.State);
        Map(x => x.ZipCode);

        DiscriminateSubClassesOnColumn("objectType").AlwaysSelectWithValue();
    }
}

public class PersonAddressMap : SubclassMap<PersonAddress>
{
  public PersonAddressMap() {
        DiscriminatorValue("CmnPerson");

        References(x => x.Person).Column("objectId");
  }
}

Solution

  • You can not use the Any-mapping when it's a has many relation. It can be used, when an address can point to different kinds of objects - like a person, an order or other things not related.

    To map the hierarachy you can do it like this:

    public class CmnAddressMap : ClassMap<CmnAddress>
    {
        public CmnAddressMap()
        {
            Id(x => x.addressId);
            Map(x => x...);
    
            DiscriminateSubClassesOnColumn("objectType");
        }
    }
    
    public class PersonAdressMap : SubclassMap<PersonAddress>
    {
        public PersonAdressMap()
        {
            DiscriminatorValue("objectType1");
        }
    }
    
    public class ContactAdressMap : SubclassMap<ContactAddress>
    {
        public ContactAdressMap()
        {
            DiscriminatorValue("objectType2");
        }
    }
    

    Have an abstract CmnAddress with all the fields (map all the fields in the CmnAdressMap for example) and two subclasses named PersonAddress and ContactAddress.

    And the person should then have a collection like IList which should mapped with HasMany. And you should be done.