Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Nhibernate ClassMaps and Column Order

Our entities have a group of common properties. In order to reduce repetitive mapping, I created a base ClassMap that maps the identities and common properties. For each entity's ClassMap I just subclass the base and it works great. For a new project we are also letting NH generate the DB schema for us. The issue is, the order of the columns is such that the properties from the base ClassMap appear first, followed by anything mapped in the sub class. The requirement for this build is that the columns appear in a specific order.

To get around this I did the following.

public class BaseMap<T> : ClassMap<T> where T : Entity
{
    public BaseMap()
    {
        Id(x => x.Id);
        MapEntity();
        Map(x => x.CommonProperty1);
        Map(x => x.CommonProperty2);
        Map(x => x.CommonProperty3);
    }

    protected virtual void MapEntity()
    {
    }
}

public class SomeEntityMap : BaseMap<SomeEntity>
{
    public SomeEntity()
    {
        base.MapEntity();
    }

    protected override void MapEntity()
    {
        Map(x => x.SomeEntityProperty1);
        Map(x => x.SomeEntityProperty2);
        Map(x => x.SomeEntityProperty3);        
    }
}

This works, but feels like a hack. Aside from the hack factor, is there anything here that could be problematic?

like image 603
Roco72 Avatar asked Feb 26 '23 05:02

Roco72


2 Answers

If you made the base class and map method abstract it would feel less hacky...

public abstract class BaseMap<T> : ClassMap<T> where T : Entity
{
    public BaseMap()
    {
        Id(x => x.Id);
        MapEntity();
        Map(x => x.CommonProperty1);
        Map(x => x.CommonProperty2);
        Map(x => x.CommonProperty3);
    }

    protected abstract void MapEntity();

}

public class SomeEntityMap : BaseMap<SomeEntity>
{
    protected override void MapEntity()
    {
        Map(x => x.SomeEntityProperty1);
        Map(x => x.SomeEntityProperty2);
        Map(x => x.SomeEntityProperty3);        
    }
}

This would keep the common property columns at the end of the table. Be aware that foreign key columns will still get added after those. I don't think there is any way to have full control of the column order w/ fluent, unless you hand-modify the create schema scripts.

like image 103
dotjoe Avatar answered Mar 05 '23 09:03

dotjoe


I just had to implement something similar myself.

Assuming that you have

public class SomeEntity : Entity
{ 
    ...
}

A less 'hack-like' way would be:

public abstract class BaseMap<T> : ClassMap<T> where T : Entity
{
    public BaseMap()
    {
        Id(x => x.Id);
        Map(x => x.CommonProperty1);
        Map(x => x.CommonProperty2);
        Map(x => x.CommonProperty3);
    }
}

public class SomeEntityMap : BaseMap<SomeEntity>
{
    public SomeEntity()
    {
        Map(x => x.SomeEntityProperty1);
        Map(x => x.SomeEntityProperty2);
        Map(x => x.SomeEntityProperty3);        
    }
}

Same result in the end, but your not using overridden methods to add mappings. It'll be taken care of automagically.

like image 36
mimix Avatar answered Mar 05 '23 09:03

mimix