-->
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: NHibernate Without App.Config
PostPosted: Sun Oct 09, 2005 3:51 pm 
Hey,
Is there a way to use NHibernate without an App.Config - configuring it with the nhibernate.cfg.xml file or doing it programmatically?
Thanks ahead

--Ram


Top
  
 
 Post subject:
PostPosted: Sun Oct 09, 2005 4:48 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Yes, you can.
Try passing to the Configuration.Properties a hashtable with the values you want.
The keys & values should be the same as you've in the app.config.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 9:16 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
The recommended way to set the properties programmatically is to use the const strings in the class NHibernate.Cfg.Environment and use the method Configuration.SetProperty().

eg:
Code:
cfg.SetProperty(NHibernate.Cfg.Environment.ConnectionString, "Server=(local);initial catalog=nhibernate;Integrated Security=SSPI");


To set the properties through a XML file, use the methods Configuration.Configure(...).

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 12:47 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
you can also pass the assembly with the cfg.xml file to the Configure operation of the Configuration object. Then add in your connection string dynamically.

Heres some sample code.
Code:
NH.Configuration sessionConfiguration = new NH.Configuration();
                  
//load up the assembly that contains the mappings file
AssemblyName libraryName = new AssemblyName();
libraryName.Name = this._configurationAssemblyName;
Assembly configurationAssembly = Assembly.Load(libraryName);
                  
//configure the configuration with the specified configuration file
sessionConfiguration.Configure(configurationAssembly, this._configurationResourceName);

//specify the connection string for the session factory - this could be done through configuration
//but we like to do this dynamically so we can encrypt it
sessionConfiguration.Properties.Add(NH.Environment.ConnectionString, this._connectionString);


Top
 Profile  
 
 Post subject: App.Config
PostPosted: Mon Oct 10, 2005 5:58 pm 
First, thanks for all of your replys!
Second, jnapier - you write in your code snippet that I need to give the Configure method 2 parameters - the Assembly holding the hibernate.cfg.xml file, and the ResourceName - what do you mean by ResourceName?
Third, is there a different option instead of programmatically setting the properties of the Configuration object - i.e - to give it a path to an external configuration file?
Thanks again,

Ram


Top
  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 9:08 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
The ResourceName is the name of the xml file containing the configuration and which is embedded in your assembly; most of the time, it is something like MyAssembly.MyFile.xml. (you can use ILDAsm to see that name...)

You can call Configuration.Configure( string resource ) to use an external xml file...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: App.Config
PostPosted: Tue Oct 11, 2005 2:00 pm 
Newbie

Joined: Tue Oct 11, 2005 1:52 pm
Posts: 2
Worked like a charm KPixel!
It would be nice to have one of the examples in the Quickstart use this method...
Thank you all for your help you'r doing great job!

Ram


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.