Custom Connection Provider with NHibernate 2.0 (Beta 2)

.NET Musings

Wandering thoughts of a developer, architect, speaker, and trainer


Custom Connection Provider with NHibernate 2.0 (Beta 2)

I don't like having connection strings in config files, especially smart client config files. However, there are times when it is a necessity. So, the other "deal breaker" for me with NHibernate was to determine if I could create a custom connection provider that would allow me to have an encrypted connection string in the configuration, handle the decryption, then pass the appropriate information to NHibernate core.

Turns out this is a very simple endeavor. Create a class that derives from DriverConnectionProvider, and override one method from the base:

protected override string GetNamedConnectionString(System.Collections.Generic.IDictionary settings)
//do work to return an unencrypted connection string

Simple as that!

Happy Coding!

Comments (3) -

To make that work you need to change connection.provider property in config file. Isn´t it?

I did so:

<property name="connection.provider">BPTDAL.DriverConnectionProviderBPT</property>

But still have this error:

"Could not instantiate connection provider: BPTDAL.DriverConnectionProviderBPT"

What else is missing. Do I need to tell him the assembly name? How?

Thanks in advance,
Andoni Ripoll

And then answer is, write this:

<property name="connection.provider">BPTDAL.DriverConnectionProviderBPT, BPTDAL</property>
Andoni Ripoll

That is correct. Guess it would have helped to put that in the post Smile.  Good catch!

Comments are closed
Managed Windows Shared Hosting by OrcsWeb