2
votes

We are working on a domain where the requirement is that a MemberAgreement varies based on Network and State combination. MemberAgreement has a Template which can be shared by multiple States for a Network.

The way we are modelling the Entities is

public class MemberAgreement
{
        public Network Network { get; protected set; }
        public List<State> States { get; protected set; }
        public Template Template {get; protected set; }
}

The tables are designed as:

Agreement
---------
Id
NetworkId
StateId
TemplateId

In this table Agreement, NetworkId and TemplateId can repeat for different StateIds.

Now, how do I map this in Fluent NHibernate? We have One to Many relation between Network and State and combination of these two has Many to Many relation with Template.

Help is appreciated.

PC

1

1 Answers

1
votes

The table structure as is will force you to change your MemberAgreement object definition. Every row (unique by ID) is referencing exactly one ReferenceType:

public class MemberAgreement
{
  public virtual Network Network { get; protected set; }
  //  public List<State> States { get; protected set; }
  public virtual State State { get; protected set; }
  public virtual Template Template {get; protected set; }
}

Having this, your mapping will be simplified References(x => x.State). But more issues will come in your business layer, where you have to work with collection: IList<MemberAgreement>.

Other option is to use 6.9. Ternary Associations. Extracted and applied on a Network:

public class Network
{
  ...
  public virtual IDictionary<State, Template> StateTemplates { get; set; }
}

<map name="States" table="Agreement" >
    <key column="NetworkId" />
    <index-many-to-many column="StateId" class="State" />
    <many-to-many column="TempalteId" class="Template" />
</map>

and in Fluent like this :

HasMany(x => x.StateTemplates)
    .KeyColumn("NetworkId")
    .AsMap<State>("StateId")
    .AsTernaryAssociation("TemplateId");