views:

100

answers:

1

Hi! All examples of fluent nhibernate make such(or similar) call:

c.AddMappingsFromAssembly(typeof(Product).Assembly);

I don't want tu use "typeof(Product).Assembly" as i don't want to have reference to my domain project here ("Procuct" class). In ordinary NHibernate I would just create hbm.xml files and make following entry in web.config:

  <mapping assembly="TestingFluentHN"/>

but this entry does not work with FluentNHibernate. Is there an elegant way of providing assemblies in my session-building method? Preferably from configuration file.

Resources: Context of unwanted code/dependency:

static NHSessionManager()
{
    Configuration c = new Configuration();
    //change following to sth that does not need refernce to domain
    c.AddMappingsFromAssembly(typeof(Product).Assembly);
    c.Configure();      
    sessionFactory = c.BuildSessionFactory();
}

My first idea was to read assemblies names from appSettings and load them:

var assembliesToMap = new List<string>();
foreach (var assemblyName in assembliesToMap)
{
    var assembly = Assembly.LoadFile(assemblyName);
    c.AddMappingsFromAssembly(assembly);
}

but that is my last option. I'm looking for sth build in fluent nhibernate.

+1  A: 

I'm not aware of anything built into fluent nhibernate that will do what you want. You'll probably need to use the method you laid out in the end of your question.

I'm not sure if I'm just not getting the right picture of how your application is laid out, but the whole idea seems a bit misguided. You will need to take a dependency on the domain objects to query the session anyway, and it seems likely that this would be in the same assembly as the session factory's creation. If not, you may consider using dependency injection to provide a session manager (from a project that is aware of the domain objects).

If I'm missing something please let me know.

AlexCuse
Well, i've made more research, i've looked through code of Sharp Architecure to see how guys did that, and i haven't found web.config/app.config way. As you wrote i ended up with injecting an interface that has method returning all assemblies with mappings into my core project. About your doubds about whole idea :) :there are (among other projects) Core, and "our framework" (a reusable part with for instance generic data access - project i want to use in othes solutions). "our framework" deals with NH which needs mapping, but those are in core. "out framework" of course can't depend on Core.
PK
p.s. Sharp Architecture guys do this in similar way. project depending on sharp architecture stuff passes array of assemblies names with mappings. -> NH is inside sharp architecture, entites are in project using sharp arch. Thanks for reply.
PK
I figured that is what you were looking for - sometimes you just need the word you're looking for I guess. Glad you got it working!
AlexCuse