0

I have a project with my mappings and entities stored in other class libraries and NHibernate layers in another project. In my testing project I would like to add these mapping via fluently configure... Mappings... via assebly and not individually. In my code below you can see I added just one entity.. But I would like to configure it to scan my other assemblies. I am sure I am just missing the obvious here.. any pointers would be greatly appreciated...

 [Test]
    public void Can_generate_schemaFluently()
    {
        var cfg = new Configuration();  
        cfg.Configure();

        Configuration configuration = null;
        ISessionFactory SessionFactory = null;
        ISession session = null;

        SessionFactory = Fluently.Configure(cfg)
            *** WOULD LIKE TO ADD MY ASSEBLIES and autoscan for objects instead ***
          .Mappings(m => m.FluentMappings
                        .Add(typeof(StudentEOMap))
                  )
           .ExposeConfiguration(x => configuration = x)
            .BuildSessionFactory();

        session = SessionFactory.OpenSession();

        object id;
        using (var tx = session.BeginTransaction())
        {
            var result = session.Get<StudentEO>(1541057);
            tx.Commit();
            Assert.AreEqual(result.StudId, 1541057);
        }
        session.Close();

    }
Warren LaFrance
  • 552
  • 7
  • 20

1 Answers1

2

AutoMapping

If you want to filter through types, you can use the IAutomappingConfiguration and derive from DefaultAutomappingConfiguration like this:

public class StandardConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type)
    {
        // Entity is the base type of all entities
        return typeof(Entity).IsAssignableFrom(type);
    }
}

You can also use DefaultAutomappingConfiguration if you have no need to filter. But my further example uses the StandardConfiguration.

Change your configuration like this, to populate your types to FluentNHibernate:

SessionFactory = Fluently.Configure(cfg)
    .Mappings(m => MapMyTypes(m))
    .ExposeConfiguration(x => configuration = x)
    .BuildSessionFactory();

And the MapMyTypes method should look like this:

private void MapMyTypes(MappingConfiguration m)
{
    m.AutoMappings.Add(AutoMap.Assemblies(new StandardConfiguration(), 
        Assembly.GetAssembly(typeof(Entity)), 
        Assembly.GetAssembly(typeof(OtherAssemblyEntity)))
    );
}

You can add multiple Assemblies and all get filtered through the StandardConfiguration.

Edit

FluentMappings

It seems that i misread your question. To add mappings you can use a similar method to achieve that but without a IAutomappingConfiguration. Just change the MapMyTypes method to:

private void MapMyTypes(MappingConfiguration m)
{
    m.FluentMappings.AddFromAssembly(Assembly.GetAssembly(typeof(EntityMap)));
}

Combine

You can also combine the FluentMapping and the AutoMapping like this:

private Action<MappingConfiguration> MapMyTypes()
{
    return m =>
    {
        MapFluent(m);
        MapAuto(m);
    };
}
Rabban
  • 2,451
  • 1
  • 19
  • 21
  • I have a class library that contains my mappings... and a class library that contains my nhibernate code and of course a testing lib. In the testing lib I want to scan my class lib that contains all my entity mappings.. Looking at your example it looks like I still need to provide each entity mapping... I am just not seeing the solution here.. Sorry :( Do you have link to documentation of what your referring to by chance? – Warren LaFrance Feb 08 '17 at 16:57
  • @WarrenLaFrance You just need to give the Assembly where your mappings are defined. The Assembly is just selected with one of your mapping classes. So there is no need to select all your classes by yourself. `Assembly.GetAssembly(typeof(EntityMap))` just selects the assembly, not the class. – Rabban Feb 08 '17 at 17:00
  • Ok, that is perfect.. Thanks a million for the pointers... cfg {NHibernate.Cfg.Configuration} NHibernate.Cfg.Configuration - ClassMappings Count = 3 – Warren LaFrance Feb 08 '17 at 17:42