0

I'm trying to create a parent entity (Policy) with two child-entity collections (ExpressionRules and ThresholdRules) where the types of the child-entities are the same and exist in the same table. For example:

class Policy
{
    ICollection<Rule> ExpressionRules { get; set; }
    ICollection<Rule> ThresholdRules { get; set; }
}

class Rule
{
    public virtual int SequenceNumber { get; set; }
    public virtual ICondition Condition { get; set; }
}

interface ICondition
{
}

class ExpressionCondition : ICondition
{
    public virtual string Expression { get; set; }
}

class ThresholdCondition : ICondition
{
    public virtual int Threshold { get; set; }
}

In practice each set of rules will have different ICondition implementers inside them. ICondition mappings have discriminators according to which the correct implementation is loaded. But the rules in the two sets are identical and I would like them to be mapped to the same table.

Is this possible?

(If you add mapping examples please use xml-mappings if possible.)

joniba
  • 3,339
  • 4
  • 35
  • 49
  • Can you include an actual implementation example? NHibernate doesn't map via interfaces so its hard to give advice without concrete examples of what you are trying to do. – Fourth Mar 07 '12 at 15:39
  • I added some concretes that more closely resemble the real world example I am trying to solve. Hope that makes it clearer. – joniba Mar 07 '12 at 18:27

1 Answers1

1

Seems like you just want one set of children and on the children you need to have a base class (called GrandChild) that can get discriminated. Something like:

class Parent
{
    ICollection<Child> Children { get; }
}

class Child
{
    public virtual bool SomeFlag { get; set; }
    public virtual GrandChild GrandChild { get; set; }
}

class GrandChild{}

class GrandChild1 : GrandChild
{
    public virtual string SomeProperty { get; set; }
}

class GrandChild2 : GrandChild
{
    public virtual int SomeOtherProperty { get; set; }
}
Fourth
  • 9,163
  • 1
  • 23
  • 28
  • Actually I really do want two distinctly different sets, as on a business level they are used in different ways. But I guess I can do as you suggest and filter in my code. It just means a lot of filtering code strewn about my parent entity. – joniba Mar 07 '12 at 18:39
  • Thats business logic you are trying to force onto nhibernate - nhibernate should just hydrate the data and then the parent should have logic to only return "child with these grand children" or whatever. – Fourth Mar 07 '12 at 19:05
  • Yes this is what I did in the end. – joniba Mar 20 '12 at 17:02