Mapping by code convention in NH 3.2 and table for splited properties

38 views
Skip to first unread message

Alexander Kot

unread,
Aug 29, 2011, 7:35:46 AM8/29/11
to nhu...@googlegroups.com
Hello all

I cannot fined in NH3.2 ConventionModelMapper in  Before/After events place for customizing table used for splited property.
I can define in my explicit mapping
map.Join("MyClassSplit1", mj=>
                                                            {
                                                                mj.Property(x => x.SomethingA1);
                                                                mj.Property(x => x.SomethingA2);
                                                            });
and configure table name, it's PK name, FK name there using mj explicitly.

Is there any place in ConventionModelMapperwhere i can redefine splited table name, define PK name, FK name from splited table to main table and so on?
I cannot fined any event which has IJoinMapper xcustomizer in parameters list....
 

Fabio Maulo

unread,
Aug 29, 2011, 10:53:25 AM8/29/11
to nhu...@googlegroups.com
For FK the way is the same of no splitted properties.
AFIK, PK names can't be changed. You can change just the column name not the name of the index.

As you probably know, by default, the name of the split-table is the name of the split-groupId (in your example "MyClassSplit1"); you can change it explicitly using "mj.Table(whatever)".

If you have a proposal about the API please file a new JIRA ticket.
Thanks.

Alexander Kot

unread,
Aug 31, 2011, 6:06:55 AM8/31/11
to nhu...@googlegroups.com
Hello

I want to do  as more as possible using mapping by code using convention.

When i use table per subclass strategy for mapping inherited classes i can define
Table name, PK column name, FK derived -> base in mapper.BeforeMapJoinedSubclass
An i do this like
mapper.BeforeMapJoinedSubclass += (mi, t, map) =>
{
    string tn = TableNameByEntityName(t.Name);
    map.Table(tn);

    map.Key(cm =>
    {
        cm.Column(PKNameForEntity(GetRootEntityType(t.BaseType).Name));
        cm.ForeignKey(String.Concat("FK_", tn, "_2_", TableNameByEntityName(t.BaseType.Name)));
    });
};
It is enough and there is no need to define something in explicit mapping.
The same i can do in other events when mapping details collections.

But when i want split column for my root class, now I have to do something like

public class MyEntityMap : ClassMapping<MyEntity>
{
    public MyEntityMap()
    {
        ........

        Join("G1", jm =>
        {
            MappingConvention.ConfigureSplit("G1", jm);  //Explicit  call for applying convention
            jm.Property(x => x.JoinedStr);
        });
    }
}

And in my helper class:
public static void ConfigureSplit<T>(string splitGroupId, IJoinMapper<T> joinMapper) where T : class
{
    var t = typeof(T);
    var tn = String.Concat(TableNameByEntityName(t.Name), "_", splitGroupId).ToUpperInvariant();

    joinMapper.Table(tn);

    joinMapper.Key(km =>
        {
            km.Column(PKNameForEntity(t.Name));
            km.ForeignKey(String.Concat("FK_", tn, "_2_", TableNameByEntityName(t.Name)));
        });
}

I want not to call MappingConvention.ConfigureSplit("G1", jm) in explecit mapping
but has event like
mapper.BeforeMapSplitedGroup += (mi, t, map) =>
{
  //where map is IJoinMapper<T>
  //and it can be usefull to have access to split-groupId name via IJoinMapper interface for defining table name
}

It will be analogical to subclasses and detail collections mapping.

Is such description will be enough for JIRA ticket?
Reply all
Reply to author
Forward
0 new messages