views:

24

answers:

1

I'm a big believer in DRY so after setting up ClassMap objects in fluent nhibernate, how can I consume these mappings in code not using Nhibernate?

Edit: Example, I have a class that is mapped but also uses a stored procedure to populate itself and it's children (and their children) in a single database call. When populating objects from the data reader, it would be nice to use the property to column mapping in the ClassMap.

A: 

Perhaps this is something you might be wanting:

I've come up with this in a very quick view. Check the klass variable. I am sorry for not testing it properly.

public static ISessionFactory CreateSessionFactory()
{
    return Fluently
        .Configure()
        .Database(MsSqlConfiguration.MsSql2008.ConnectionString(@"Server=SPO00615\SQLEXPRESS;Database=BOB;Integrated Security=true; "))
        .Mappings(m => GetClassFrom(m))
        .BuildSessionFactory();
}

private static FluentMappingsContainer GetClassFrom(MappingConfiguration m)
{
    FluentMappingsContainer container =
        m.FluentMappings.AddFromAssemblyOf<FactoryTests>();

    var maps = container.PersistenceModel.BuildMappings();

    foreach (var map in maps)
    {
        var klass = map.Classes.GetEnumerator().Current;
    }

    return container;
}
NoProblemBabe