(fluent) nhibernate conditional table mapping strategy

Posted by grenade on Stack Overflow See other posts from Stack Overflow or by grenade
Published on 2010-03-08T13:21:31Z Indexed on 2010/03/17 8:31 UTC
Read the original article Hit count: 557

I have no control over database schema and have the following (simplified) table structure:

  • CityProfile
    • Id
    • Name
  • CountryProfile
    • Id
    • Name
  • RegionProfile
    • Id
    • Name

I have a .Net enum and class encapsulating the lot:

public enum Scope { Region, Country, City }

public class Profile {
    public Scope Scope { get; set; }
    public int Id { get; set; }
    public string Name { get; set; }
}

I am looking for a mechanism that allows me to map to the correct table, something like:

public class ProfileMap : ClassMap<Profile> {
    public ProfileMap() {
        switch (x => x.Scope) { // <--Invalid code here!
            case Scope.City: Table("CityProfile"); break;
            case Scope.Country: Table("CountryProfile"); break;
            case Scope.Region: Table("RegionProfile"); break;
        }
        Id(x => x.Id);
        Map(x => x.Name);
    }
}

Or have I approached this wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about nhibernate