开发者

How do I inject a connection string in to my nhibernate configuration?

I need to be able to configure my connection string at run time, preferably from a config file would be the simplest solution.开发者_Python百科 However, I can't use ConnectionString.FromAppSetting nor ConnectionString.FromConnectionStringWithKey. My data-access assembly is referenced from console apps, win forms, and web. And while I can add "Settings" to my project, it's only a dll and won't bring that app.config over with it to the actual applications build destination.

I don't expect this is too uncommon. What is typically the way this is handled? Is this a prime example of when to use dependency injection (one that I could configure from a separate file?) I'm feeding the connection string to my 'unit of work' object, but I'd like to do it statically as to build the config and sessionfactory only once.

I can definitely clarify if needed, but I'm not sure the best way to ask.


You can use hibernate.cfg.xml (it works on every plattaform). Alternatively you can do the following

  1. Inherit DriverConnectionProvider
  2. override the ConnectionString property, and read the connection string from anywhere.
  3. Supply your new connection provider to your nhibernate configuration in code or in xml.

You can use ServiceLocation inside your DriverConnectionProvider or you can use the EnhancedBytecodeProvider on unhaddins. Search Enhanced bytecodeprovider and Fabio Maulo on google.

Here is an example: Dynamically change user info in connection string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜