tags:

views:

141

answers:

1

I would like to build a DLL (which should be accessed from a web service and possibly from another application through automation).

Is there any possibility using NHibernate inside this dll (so accessing the dll through automation would work) ?

I am already using NHibernate in a rich client application and it is very handy , but I have to make some changes to the app.config for this. All the other tutorials I see are using NHibernate directly on the web service - and are changing the web.config accordingly.

+3  A: 

If you configure NHibernate in code rather than using app.config or web.config you should be able to avoid the problem you describe. For example you could use Fluent NHibernate's Fluent Configuration feature to configure NHibernate and thus avoid use of both web.config and hibernate.cfg.xml, which potentially could cause some trouble as well.

I am currently using this approach in a web app, where the data access layer is in a separate assembly and the web assembly has no reference to NHibernate and needs no modification to web.config, nor is a hibernate.cfg.xml file used.

Here is an example of a Fluent configuration:

sessionFactory = Fluently.Configure()
    .Mappings(x => x
        .FluentMappings.AddFromAssemblyOf<FooMap>()
        .ConventionDiscovery.AddFromAssemblyOf<BarConvention>()
        )
    .Database(MsSqlConfiguration.MsSql2005.ConnectionString(x => x
        .Database("YourDbName")
        .Server(@".\SQLEXPRESS")
        .TrustedConnection())
        .ShowSql())
    .BuildSessionFactory();

Update:

The same goal should be possible to achieve using only standard NHibernate, by using their programmatic configuration possibilities. Instead of using the web.config or such to configure your database connection etc. you could pass an IDictionary instance to Configuration.SetProperties() when you create your session factory.

Something like this:

Configuration config = new Configuration();

IDictionary properties = new Hashtable();
properties["hibernate.dialect"] = "NHibernate.Dialect.MsSql2005Dialect";
// more properties here ...

config.SetProperties(properties);

Chapter 3 of the docs has some info about this, but it is a bit on the short side.

Erik Öjebo
well, I've never used Fluent NHibernate - but this looks good.I would prefer an answer without Fluent. We are currently using the mapping attributes and then we are generating the files - but if I don't get any other solution here - your answer will be accepted for sure...
bernhardrusch
thank you for your further investigation !
bernhardrusch
No problem, glad I could help
Erik Öjebo