-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Using nHib in a .NET class library project
PostPosted: Tue Apr 17, 2007 10:35 am 
Newbie

Joined: Wed Jun 28, 2006 6:34 am
Posts: 11
NHibernate v 1.0.4
Visual Studio 2005 / C# / sp1
Oracle 10g
ODP latest version

I would like to be able to create an application (Win Forms and Web Forms) which can use a class library dll where all of the nHib functionality resides in the class library.

I've tried creating a class library this way, including persistent classes, hbm.xml mapping files (as embedded resource), app.config (as Build Action = None or Embedde Resource) in dll. I've tested the code by changing the class library into a console app and it works correctly, so I know all the nHib mappings work. I then changed type back to class library and compiled dll.

The class library includes a simple static function which returns a .NET List of objects from my class, so all the nHib access is done there.

I created a separate test project which has no knowledge of nHib but references only my own dll above. Then I try and use the class library dll I get an error saying that the AddAssembly method of the Configuration class in the dll can't set the dialect. It looks like the class library can't see the App.Config file in the dll.

Any ideas?

Thanks

Eric


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 10:57 am 
Newbie

Joined: Mon Apr 16, 2007 10:24 am
Posts: 5
That's the way .Net works, the App.config of the 'start-up' application is used. By default there is no such thing as per dll configuration.

The way I do it:
Code:
NHibernate.Cfg.Configuration config = new Hibernate.Cfg.Configuration();
config.Configure("nhibernate.cfg.xml");


nhibernate.cfg.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="hibernate.connection.connection_string">...</property>
    <property name="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="hibernate.dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
  </session-factory>
</hibernate-configuration>



Configure the .cfg.xml file in your project so VS will copy it to the output dir.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 17, 2007 1:02 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
You can embed your configuration file in the class library and then open the configuration with this overload on the Configuration object:


Configure NHibernate using a resource contained in an Assembly.
Code:
public Configuration Configure(Assembly,string);


where string is the resource name.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 7:05 am 
Newbie

Joined: Wed Jun 28, 2006 6:34 am
Posts: 11
[i]This is partially succesful as I no longer get any errors, but it looks like Hibernate can't see my mapping files which are also in the class library dll

Eric[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 7:17 am 
Newbie

Joined: Wed Jun 28, 2006 6:34 am
Posts: 11
(1) Using the nhibernate.cfg.xml file is partially succesful as I no longer get any errors, but it looks like Hibernate can't see my mapping files which are also in the class library dll.

(2) The Configure(Assembly,string); method may work but I'm not sure how to reference an assembly in a sepaarte class library dll.

Eric


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 10:36 am 
Newbie

Joined: Wed Jun 28, 2006 6:34 am
Posts: 11
I've managed to get this to work and I thought you may like to know how:

I've now got an xml config file in my class library dll:
nhibernate.cfg.xml

and my mapping files
<classname>.hbm.xml
...etc

The config and mapping files are set with Build Action = None and Copy to output directory = Copy if newer

I have a separate service class in the class library dll with a static method which is used to open a NHibernate session get data and return to calling program. The NHibernate code looks like this:

ISession session;
Configuration cfg;
cfg = new Configuration();
cfg.Configure("nhibernate.cfg.xml");
cfg.AddFile("<classname>.hbm.xml");

ISessionFactory factory = cfg.BuildSessionFactory();
session = factory.OpenSession();
...etc

This means the config and mappings are loaded from external xml files held in the same directory as the deployed dll. The testing application has no knowledge of either NHibernate or the database. The code above may need to be changed to provide a more specific location for the xml files rather than just the deployment directory.

Eric


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 18, 2007 1:21 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
Yes, if you want to deploy the config and mapping files then that is a strategy you can follow. I was under the impression that you didnt want to deploy the files. I usually will embed the mapping files but not the config file. Its better to be able to tweak it on the fly.

Either way, I personnally do not like having to add all of my mapping files into the NH config. It becomes a maintenance headache. You have to make sure all of mapping files are set as embedded resources so that they get included in the assembly. Section 3.1 in the doucmentation discusses this technique.

Here is a technique for loading the assembly with the mapping files and the config file if they are all in one assembly

Code:
//create a new configuration
NH.Configuration factoryConfiguration = new NH.Configuration();
                  
//load up the assembly that contains the mappings files
AssemblyName libraryName = new AssemblyName();
libraryName.Name = "someAssemblyName";
Assembly configurationAssembly = Assembly.Load(libraryName);
                  
//configure the configuration with the specified configuration assembly
factoryConfiguration.Configure(configurationAssembly, "configurationResourceName");


Of course, you can avoid all this and use Castle's NHibernate Integration facitlity. This is the easiest and my most preffered approach.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.