Rebel.Framework.Persistence.NHibernate.ProviderBootstrapper.ConfigureApplication C# (CSharp) Method

ConfigureApplication() public method

Creates any necessary configuration files/transforms for the provider to operate
public ConfigureApplication ( string providerKey, System.Xml.Linq.XDocument configXml, BendyObject installParams ) : void
providerKey string The provider key for the provider that is being configured
configXml System.Xml.Linq.XDocument The configuration xml file that needs to be written to
installParams BendyObject TODO: This is only a temporary way of passing arbitrary parameters to a provider to create its configuration, /// we need to allow hive providers to return a model for which we display a form/installer for and then pass in that /// model to the installParams
return void
        public override void ConfigureApplication(string providerKey, XDocument configXml, BendyObject installParams)
        {
            dynamic dynamicParams = installParams;
            string dbType = dynamicParams.DatabaseType.ToString();

            var connectionString = "";
            var providerName = "";
            var nhDriver = "";
            //we need to create the connection strings if it's not custom

            switch (dbType)
            {
                case "MSSQL":
                    connectionString = string.Format("Data Source={0}; Initial Catalog={1};User Id={2};Password={3}",
                                                     dynamicParams.Server, dynamicParams.DatabaseName, dynamicParams.Username, dynamicParams.Password);
                    providerName = "System.Data.SqlClient";
                    nhDriver = "MsSql2008";
                    break;
                case "MySQL":
                    connectionString = string.Format("Server={0}; Database={1};Uid={2};Pwd={3}",
                                                     dynamicParams.Server, dynamicParams.DatabaseName, dynamicParams.Username, dynamicParams.Password);
                    providerName = "MySql.Data.MySQLClient";
                    nhDriver = "MySql";
                    break;
                case "SQLCE":
                    connectionString = "Data Source=|DataDirectory|Rebel.sdf";
                    providerName = "System.Data.SqlServerCe.4.0";
                    nhDriver = "MsSqlCe4";
                    break;
                case "Custom":
                    //limiting to MS SQL atm 
                    connectionString = dynamicParams.ConnectionString;
                    providerName = "System.Data.SqlClient";
                    nhDriver = "MsSql2008";
                    break;
            }

            var connstringKey = "";

            var hiveElement = new ProviderConfigurationSection()
                       {
                           ConnectionStringKey = "{0}.ConnString",
                           Driver = SupportedNHDrivers.MsSqlCe4,
                           SessionContext = "web"
                       };

            var elementName = providerKey;
    
            hiveElement.DriverAsString = nhDriver;
            connstringKey = string.Format(hiveElement.ConnectionStringKey, providerKey);
            hiveElement.ConnectionStringKey = connstringKey;

            DeepConfigManager.SerializeProviderConfigSection(configXml, hiveElement, "rebel/persistenceProviderSettings/" + elementName, true);

            //add the connection strings
            var connStrings = new ConnectionStringsSection();
            connStrings.ConnectionStrings.Add(new ConnectionStringSettings(connstringKey, connectionString, providerName));
            //now serialize the connection strings to the config
            var connectionStringElement = DeepConfigManager.SerializeProviderConfigSection(configXml, connStrings, "connectionStrings", false);
            var newConnString = new XElement("add");
            DeepConfigManager.AddPropertiesToElement(connStrings.ConnectionStrings[0], newConnString);
            connectionStringElement.Add(newConnString);
            
            // The following is superceded by the above to support multiple "add" references: DeepConfigManager.SerializeProviderConfigSection(configXml, connStrings.ConnectionStrings[0], "connectionStrings/add", false);
        }

Usage Example

        public void NHibernateBootstrapper_Configure_Application()
        {
            //Arrange

            //create a new web.config file in /plugins for the providers to write to
            var http = new FakeHttpContextFactory("~/test");
            var configFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, "nhibernate.config"));
            var configMgr = new DeepConfigManager(http.HttpContext);
            var configXml = DeepConfigManager.CreateNewConfigFile(configFile, true);
            var installModel = new DatabaseInstallModel()
                {
                    DatabaseType = DatabaseServerType.MSSQL,
                    DatabaseName = "test",
                    Server = "testserver",
                    Username = "******",
                    Password = "******"
                };
            var boot = new ProviderBootstrapper(null, null, new FakeFrameworkContext());


            //Act

            boot.ConfigureApplication("rw-test", "test", configXml, new BendyObject(installModel));

            //Assert

            Assert.AreEqual(@"<configuration>
  <configSections>
    <sectionGroup name=""rebel"">
      <sectionGroup name=""persistenceProviderSettings"">
        <section name=""test"" type=""Rebel.Framework.Persistence.NHibernate.Config.ProviderConfigurationSection, Rebel.Framework.Persistence.NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"" />
      </sectionGroup>
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name=""test.ConnString"" connectionString=""Data Source=testserver; Initial Catalog=test;User Id=testuser;Password=testpass"" providerName=""System.Data.SqlClient"" />
  </connectionStrings>
  <appSettings />
  <rebel>
    <persistenceProviderSettings>
      <test connectionStringKey=""test.ConnString"" sessionContext=""web"" driver=""MsSql2008"" />
    </persistenceProviderSettings>
  </rebel>
</configuration>", configXml.ToString());

        }