简体   繁体   English

温莎城堡/ ActiveRecord / NHibernate:如何拦截/修改连接字符串

[英]Castle Windsor / ActiveRecord / NHibernate: How to intercept/modify connection string

I have consolidated the connection string information for a number of C# .NET solutions that are in my possession. 我已经为我拥有的许多C#.NET解决方案合并了连接字符串信息。 Previously, each project was storing its connection string in its own format, requiring me to modify several files for each installation of the software. 以前,每个项目都以自己的格式存储其连接字符串,要求我为每次安装该软件修改多个文件。

Only one remaining solution is giving me trouble. 仅剩下的一种解决方案给我带来了麻烦。 This particular solution uses Castle Windsor 2.0, ActiveRecord 2.0 and NHibernate 2.1. 该特定解决方案使用Castle Windsor 2.0,ActiveRecord 2.0和NHibernate 2.1。 The code reads its configuration from an XML file. 该代码从XML文件读取其配置。 I wish to remove the connection string from the config file and set it programmatically in the code. 我希望从配置文件中删除连接字符串,并以编程方式在代码中进行设置。

Here is the relevant section of code that initiates Windsor: 这是启动Windsor的相关代码部分:

windsorContainer = new WindsorContainer(new XmlInterpreter(xmlFileName));
windsorContainer.Resolve<IWindsorConfigurator>().Configure(windsorContainer);
logger = windsorContainer.Resolve<ILogger>();

Here are the contents of the XML file: 这是XML文件的内容:

<?xml version="1.0"?>
<configuration>
  <properties>
    <connectionString>Server=*****;Database=*****;User Id=*****;Password=*****</connectionString>
  </properties>
  <facilities>
    <facility id="logging" type="Castle.Facilities.Logging.LoggingFacility, Castle.Facilities.Logging" loggingApi="log4net" configFile="Configs/log4net.config" />
    <facility id="atm" type="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility, Castle.Facilities.AutomaticTransactionManagement" />
    <facility id="arfacility" type="Castle.Facilities.ActiveRecordIntegration.ActiveRecordFacility, Castle.Facilities.ActiveRecordIntegration" isDebug="false" isWeb="false">
      <!-- Configure the namespaces for the models using Active Record Integration -->
      <assemblies>
        <item>ChronoSteril.Application</item>
      </assemblies>
      <config>
        <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
        <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
        <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
        <add key="connection.connection_string" value="#{connectionString}" />
        <add key="hibernate.cache.provider_class" value="NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache" />
        <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
        <add key="hibernate.expiration" value="60" />
      </config>
    </facility>
  </facilities>
  <components>
    <component id="windsorConfigurator" service="ChronoSteril.Application.IWindsorConfigurator, ChronoSteril.Application" type="ChronoSteril.WinApp.ClarionIntegrationWindsorConfigurator, ChronoSteril.WinApp" />
  </components>

I am not familiar with Windsor. 我对温莎不熟悉。 During my Google tour, I did see some code that adds facilities programmatically, but those examples were not valid for my version of Windsor ( I assume ). 在我的Google之旅中,我确实看到了一些以编程方式添加功能的代码,但是这些示例对于我的Windsor版本无效( 我假设 )。

Question: Can anyone guide me in removing the connection string information from the XML file and allow me to set it in the code? 问题:谁能指导我从XML文件中删除连接字符串信息,并允许我在代码中进行设置?

Thank you! 谢谢!

I managed to accomplish my intention. 我设法实现了我的意图。 It is not ideal, but will work until the code base is rewritten. 这不是理想的方法,但是在重写代码库之前都可以使用。 ( I cannot wait to drop the existing code like a bad dream. ) 我迫不及待地想像丢掉梦一样丢掉现有的代码。

Patrick's comment, under my initial question, let me to refine my search criteria, which yielded the thread located here . 在我的第一个问题下,帕特里克(Patrick)的评论让我完善了搜索条件,从而得出了位于此处的主题。

My XML file remains the same, except that I use bogus values for the connection string information. 我的XML文件保持不变,除了我对连接字符串信息使用虚假值。 I will never need to modify these, and they do not reveal any valid connection information. 我将永远不需要修改它们,并且它们不会显示任何有效的连接信息。 This was my intention. 这是我的意图。 I still have not discovered how to successfully remove the ActiveRecord configuration from the XML file and configure using code. 我仍然没有发现如何从XML文件成功删除ActiveRecord配置并使用代码进行配置。

I now call a method that contains the following code: 我现在调用一个包含以下代码的方法:

ISessionFactoryHolder sessionFactoryHolder = ActiveRecordMediator.GetSessionFactoryHolder();
NHibernate.Cfg.Configuration configuration = sessionFactoryHolder.GetConfiguration(typeof(ActiveRecordBase));
connectionString = ReadConnectionString();
configuration.SetProperty("connection.connection_string", connectionString);

This works for me. 这对我有用。 I hope that it can also help someone else who is in the same position as I was. 我希望它也可以帮助其他位置与我相同的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM