1

I'm taking a complicated legacy schema and mapping it with Fluent NHibernate. The schema is wacky enough that I've given up on automapping; the relationships between tables are weird and complicated, and it would involve a ton of exceptions.

The thing is, the simple properties are totally normal; a table's Title column maps to that entity's Title property, and so on. But because I've opted out of global automapping, there doesn't seem to be a way to avoid mapping each of my string and integer properties on every class. I find myself wanting something like

class SomeMapping : ClassMap<SomeEntity>
{
    public SomeMapping()
    {
        MapEverythingSimple();
    }
}

Before I build something complicated that reflectively emits lambda expressions (or somesuch), am I just missing an obvious feature?

1
  • It takes like 20 seconds to a minute to write a Map, and once they are done, it's not like they change often. I use Fluent against a 10yo legacy database, we write Maps manually, no issues at all. intellisense makes it pretty quick, and we have default conventions setup to avoid writing all the .Access, .Identity, etc. Commented Jun 1, 2011 at 3:24

4 Answers 4

1

How about using automapping and then overrides where things don't fit conventions? I don't think it's too much of a burden. You'll need to specify the complex relationships that don't fit a convention somewhere anyhow.

Sign up to request clarification or add additional context in comments.

4 Comments

I explored that and it's a reasonable idea, but I feel like I'll end up with a huge mess of conventions, exceptions to those conventions, and so on, and it would be easier for me to handle relationships in a naive, class-by-class basis. But you might be right. I'll give it a shot.
I'd suggest using conventions only where they are used quite often. What I did was use a mapping override file (one for each class or set of classes) and essentially did the class-by-class mapping, but only for the complex parts.
I ended up doing this, although it was not pretty.
Yeah, not surprising. It's much easier with a greenfield app
0

Or you can try NHibernate Mapping Generator to generate NHibernate mapping files and corresponding domain classes from existing DB tables: http://nmg.codeplex.com/

Comments

0

Use the trial version of Visual NHibernate to quickly generate your entity classes and Fluent mappings, then take it from there. Disclaimer: I work for Slyce Software.

1 Comment

Another option, but only works with the XML, no fluent support :( is NHibernate Designer - mindscapehq.com/products/nhdesigner
0

This is not what I ended up doing, but for posterity, here's how you can map properties automatically without using an automapper:

public class PropMap<V> : ClassMap<V>
{
    public PropMap()
    {
        foreach (var propInfo in typeof(V).GetProperties()
            .Where(p => simpleTypes.Contains(p.PropertyType)))
        {
            ParameterExpression param = Expression.Parameter(typeof(V), "x");
            Map(Expression.Lambda<Func<V, object>>(
                Expression.Convert(Expression.MakeMemberAccess(param, propInfo), typeof(object)), param));
        }
    }

    private static readonly Type[] simpleTypes = new[]
    {
        typeof (DateTime),
        typeof (String),
        typeof (int),
        typeof (long),
        typeof (Enum)
    };
}

And then just have the map classes inherit from that. Obviously, it has some serious flaws, and I didn't go with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.