views:

22

answers:

1

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?

A: 

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.

mimix
??... SomeEntityMap will not even contain CommonPropertyX properties, let alone the order the OP is talking about.
Yogesh
Haha. Good call Yogesh. I had SomeEntityMap inherit ClassMap, when it should have been BaseMap. With this change, it does include common properties.
mimix