-->
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.  [ 1 post ] 
Author Message
 Post subject: Combining configuration parameter specification sources
PostPosted: Wed May 17, 2006 10:19 am 
Newbie

Joined: Thu May 04, 2006 4:23 am
Posts: 16
Location: Germany
Hi,

there is a risk of potential trouble for new users of NHibernate-1.1 :
In some cases it might be advantageous using SIMULTANESLY
multiple of the possible ways to specify configuration parameters:
a1) as entries in App.config
a2) as entries in hibernate.cfg.xml
b) by adding name-value-entries into cfg.Properties
where [Configuration cfg = new Configuration;]

The documentation "Chapter 2. ISessionFactory Configuration"
proposes using the statement:
"cfg.Properties = props;" to include the programmatically addded configuration properties.
BUT this statement dicards all configuration property specifications previously collected from file App.config!
The docu gives no hint about that!

Correct is the call:
"cfg.addProperties(props)".
This method merges both sets of configuration properties.
Entries with the same name are overriden by "props".

The implementation in class Configuration is:

public Configuration AddProperties( IDictionary properties )
{
foreach( DictionaryEntry de in properties )
{
this.properties.Add( de.Key, de.Value );
}
return this;
}


An cautious programmer might ask:
>> But what happened if cfg.properties was "null"? <<
(Which is not the case due to the call of "Reset()" in the only constructor of Configuration.)
Or: >> Is it garanteed, that cfg.addProperties(props) will always work (even if cfg.Properties might be null some day [who knows what happens in the future] ) <<


My fail-safe work-around for this possibility is :

class HibernateStarter { // ...
protected static void mergeConfigurationProperties(Configuration config, IDictionary properties)
{
if (config.Properties == null)
config.Properties = properties;
else
config.AddProperties(properties);
}

Btw.
Is there a mechanismus in C# to specify that a variable has always a non-null value (after the constructor has finished its work for the surrounding object)?
The compiler could then prove, that this invariant is true.

Another issue:
Is it true, that "hibernate.cfg.xml" is not read when an App.config file (renamed according to the assembly name) is found?
What were the reasons for this decision?

My proposal is to merge the configuration property specification sets of
1) App.config AND
2) hibernate.cfg.xml

Regards,
Michael


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

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.