http://fabiomaulo.blogspot.com/2009/09/configure-sessionfactory-providers.html

On Thu, Apr 29, 2010 at 8:28 PM, dragon77 <[email protected]>wrote:

> Hello,
>  I'm new to ASP.NET and NHibernate.  I am trying to minimize the
> number of changes to my configuration files when deploying to
> different environments.  The databases that the domain objects are
> attached to are named similarly in each environment.  Each databases
> is suffixed by the environment it is meant for.  All of these
> deployments are on the same machine as are all the databases.
>
> What I want to do is have a property in the Web.Config that defines
> the environment I want the app to run against and have the domain
> objects obey appropriately.
>
> Say I have objects A, B and C each object maps to a  particular entity
> in separate databases:
> A -> A_DEV.dbo.SOME_ENTITY
> B -> B_DEV.dbo.SOME_ENTITY
> C -> C_DEV.dbo.SOME_ENTITY
>
> In my configuration file I have a property defined as
> <add key="BUILD_ENVIRONMENT" value="DEV"/>
>
> Each of the above objects/entities have respective *.cfg.xml for each
> of their connection information. ie
> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
>  <session-factory>
>    <property
> name="connection.provider">NHibernate.Connection.DriverConnectionProvider</
> property>
>    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</
> property>
>    <property
> name="connection.driver_class">NHibernate.Driver.SqlClientDriver</
> property>
>
>    <property
> name="connection.connection_string">Server=SERVER_NAME;Database=A_DEV;User
> Id=USER_NAME;Password=PASSWORD</property>
>    <property name="show_sql">false</property>
>    <property
>
> name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory,
> NHibernate.ByteCode.Castle</property>
>
>  </session-factory>
> </hibernate-configuration>
>
> Currently, each "cfg.xml" file is defined separately.  I know that I
> can put this information directly into Web.Config.  If I do so, I
> would be eliminating the number of files to edit for each
> environment.  However, I lose the flexibility of swapping out
> definitions as need be.
>
> The problem is the deployment process isn't controlled by me otherwise
> I'd have a script that copied the right configuration for a particular
> environment.  Therefore, eliminating the need to edit anything at
> all.  However, that is not how things are done in my current
> situation.  The current process is to modify the Web.Config file in
> place to alter the databases it needs to point to.
>
> My question:
>  If I put the definitions inside Web.Config, is it possible for me to
> use some kind of token mechanism to substitute the environment
> definition in the connection string for each of the entities?
>
> Can I do something like this?
>
> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
>  <session-factory>
>    <property
> name="connection.provider">NHibernate.Connection.DriverConnectionProvider</
> property>
>    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</
> property>
>    <property
> name="connection.driver_class">NHibernate.Driver.SqlClientDriver</
> property>
>
>    <property
>
> name="connection.connection_string">Server=SERVER_NAME;Database=A_{BUILD_ENVIRONMENT};User
> Id=USER_NAME;Password=PASSWORD</property>
>    <property name="show_sql">false</property>
>    <property
>
> name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory,
> NHibernate.ByteCode.Castle</property>
>
>  </session-factory>
> </hibernate-configuration>
>
>
> Note that {BUILD_ENVIRONMENT} refers to the property defined elsewhere
> in Web.Config
> <add key="BUILD_ENVIRONMENT" value="DEV"/>
>
>
> -- I found a post that suggested that I write a
> DriverConnectionProvider class that inspects the connection string and
> replaces it with whatever I want.  It sounds tempting but I come from
> ant ANT and Java world that I can define a property and use it later
> on in the same configuration file.  I can't seem to find information
> on how to do that in ASP.NET.
>
>
>
>
> Thanks,
> Michael
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>


-- 
Fabio Maulo

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to