0

I have a 1-to-many relationship between the RestorableEnvironment and IBaselineEntity objects: a given RestorableEnvironment will have one and only one IBaselineEntityobject, but each IBaselineEntity object may be tied to 0-n RestorableEnvironment objects. However, IBaselineEntity is implemented in one of two ways: via a file or via a database. My classes are (generally) like:

public interface IBaselineEntity
{
    BaselineImage BuildImage();
    //Remainder of interface
}

public class BaselineFile : IBaseline
{
    //implementation
}

public class BaselineDatabase : IBaseline
{
    //implementation
}

public class RestorableEnvironment
{
    public IBaselineEntity BaselineEntity { get; set; }
    //Remainder of class
}

NHibernate needs the concrete implementation of the IBaselineEntity in the references statement. To handle that, I have updated RestorableEnvironment to:

public class RestorableEnvironment
{
    public IBaselineEntity BaselineEntity
    {
        get { return BaselineDatabase ?? BaselineFile; }
        set
        {
            BaselineFile = value as BaselineFile;
            BaselineDatabase = value as BaselineDatabase;
        }
    }

    private BaselineFile _baselineFile;
    public BaselineFile BaselineFile
    {
        get { return _baselineFile; }
        protected set
        {
            _baselineFile = value;
            if (value != null)
                BaselineDatabase = null;
        }
    }

    private BaselineDatabase _baselineDatabase;
    public BaselineDatabase BaselineDatabase 
    {
        get { return _baselineDatabase; }
        protected set
        {
            _baselineDatabase= value;
            if (value != null)
                BaselineFile = null;
        }
    }

    // Remainder of class
}

Now that I have concrete classes, I can now map in NHibernate, but this feels like a hack. Are there any suggestions for an improvement?

David Beckman
  • 605
  • 8
  • 17

1 Answers1

1

map it as any reference

using fluentmapping

// ClassMap<RestorableEnvironment>
RestorableEnvironmentMap()
{
    ReferenceAny(e => e.BaselineEntity)
        .EntityIdentifierColumn("entirtyid")
        .EntityTypeColumn("entitytype")
        .IdentityType<int>()
        .MetaType<string>()
        .AddMetaValue<E1>("e1")
        .AddMetaValue<E2>("e2");
}
Firo
  • 30,626
  • 4
  • 55
  • 94
  • Perfect! For a little more on Meta Mappings, I also saw: http://stackoverflow.com/questions/8989279/mapping-to-multiple-tables-with-fluent-nhibernate – David Beckman Apr 12 '12 at 21:28