public string GetConnectionString()
        {
            var adminConnectionString = AdminConnectionString();

            if (string.IsNullOrEmpty(adminConnectionString))
            {
                throw new ArgumentException($"No connection string named '{ConnectionStringName}' was found in the 'connectionStrings' section of the application configuration file.");
            }

            var connectionStringBuilder = _connectionStringBuilderFactory.Get();

            connectionStringBuilder.ConnectionString = adminConnectionString;

            // Override the Database Name, format if string coming in has a format replacement token,
            // otherwise use database name set in the Initial Catalog.
            connectionStringBuilder.DatabaseName = connectionStringBuilder.DatabaseName.IsFormatString()
                ? string.Format(
                connectionStringBuilder.DatabaseName,
                _adminDatabaseNameReplacementTokenProvider.GetReplacementToken())
                : connectionStringBuilder.DatabaseName;

            return(connectionStringBuilder.ConnectionString);

            string AdminConnectionString()
            {
                if (_configConnectionStringsProvider.Count == 0)
                {
                    throw new ArgumentException("No connection strings were found in the configuration file.");
                }

                return(_configConnectionStringsProvider.GetConnectionString(ConnectionStringName));
            }
        }
Пример #2
0
        /// <summary>
        /// Gets the connection string with the tokens already replaced using a configured named connection string.
        /// </summary>
        /// <returns>The connection string.</returns>
        public string GetConnectionString()
        {
            var connectionStringBuilder = _dbConnectionStringBuilderAdapterFactory.Get();

            string protoTypeConnectionString = PrototypeConnectionString();

            if (string.IsNullOrEmpty(protoTypeConnectionString))
            {
                throw new ArgumentException(
                          $"No connection string named '{_prototypeConnectionStringName}' was found in the 'connectionStrings' section of the application configuration file.");
            }

            connectionStringBuilder.ConnectionString = protoTypeConnectionString;

            // Override the Database Name, format if string coming in has a format replacement token,
            // otherwise use database name set in the Initial Catalog.
            connectionStringBuilder.DatabaseName = connectionStringBuilder.DatabaseName.IsFormatString()
                ? string.Format(
                connectionStringBuilder.DatabaseName,
                _databaseReplacementTokenProvider.GetDatabaseNameReplacementToken())
                : connectionStringBuilder.DatabaseName;

            // Override the Server Name, format if string coming in has a format replacement token,
            // otherwise use server name set in the Data Source.
            connectionStringBuilder.ServerName = connectionStringBuilder.ServerName.IsFormatString()
                ? string.Format(
                connectionStringBuilder.ServerName,
                _databaseReplacementTokenProvider.GetServerNameReplacementToken())
                : connectionStringBuilder.ServerName;

            return(connectionStringBuilder.ConnectionString);

            string PrototypeConnectionString()
            {
                if (_configConnectionStringsProvider.Count == 0)
                {
                    throw new ArgumentException("No connection strings were found in the configuration file.");
                }

                if (string.IsNullOrWhiteSpace(_prototypeConnectionStringName))
                {
                    throw new ArgumentNullException("prototypeConnectionStringName");
                }

                return(_configConnectionStringsProvider.GetConnectionString(_prototypeConnectionStringName));
            }
        }
Пример #3
0
        public DatabaseNameBuilder(IConfigConnectionStringsProvider connectionStringsProvider, IDbConnectionStringBuilderAdapterFactory connectionStringBuilderFactory)
        {
            _databaseNameTemplate = new Lazy <string>(
                () =>
            {
                if (!connectionStringsProvider.ConnectionStringProviderByName.ContainsKey("EdFi_Ods"))
                {
                    return(string.Empty);
                }

                var connectionStringBuilder = connectionStringBuilderFactory.Get();

                connectionStringBuilder.ConnectionString = connectionStringsProvider.GetConnectionString("EdFi_Ods");

                return(connectionStringBuilder.DatabaseName);
            });
        }
        protected override void Arrange()
        {
            // Set up mocked dependencies and supplied values
            _databaseReplacementTokenProvider = A.Fake <IDatabaseReplacementTokenProvider>();

            A.CallTo(() => _databaseReplacementTokenProvider.GetDatabaseNameReplacementToken())
            .Returns("Ods");

            _configConnectionStringsProvider = A.Fake <IConfigConnectionStringsProvider>();

            A.CallTo(() => _configConnectionStringsProvider.Count)
            .Returns(1);

            A.CallTo(() => _configConnectionStringsProvider.GetConnectionString("SomeConnectionStringName"))
            .Returns("Server=SomeServer; Database=EdFi_{0}; UID=SomeUser; Password=SomePassword");

            _dbConnectionStringBuilderAdapterFactory = A.Fake <IDbConnectionStringBuilderAdapterFactory>();

            A.CallTo(() => _dbConnectionStringBuilderAdapterFactory.Get()).Returns(new SqlConnectionStringBuilderAdapter());
        }
        protected override void Arrange()
        {
            _databaseReplacementTokenProvider = A.Fake <IDatabaseReplacementTokenProvider>();

            A.CallTo(() => _databaseReplacementTokenProvider.GetServerNameReplacementToken()).Returns("OneDatabase");

            _configConnectionStringsProvider = A.Fake <IConfigConnectionStringsProvider>();

            A.CallTo(() => _configConnectionStringsProvider.Count).Returns(1);

            A.CallTo(() => _configConnectionStringsProvider.GetConnectionString(A <string> .Ignored))
            .Returns("Server=SomeServer; Database=SomeDatabase; UID=SomeUser; Password=SomePassword")
            .Once();

            A.CallTo(() => _configConnectionStringsProvider.GetConnectionString(A <string> .Ignored))
            .Returns("Server=prefix_{0}_suffix; Database=SomeDatabase; UID=SomeUser; Password=SomePassword")
            .Once();

            _dbConnectionStringBuilderAdapterFactory = A.Fake <IDbConnectionStringBuilderAdapterFactory>();

            A.CallTo(() => _dbConnectionStringBuilderAdapterFactory.Get()).Returns(new SqlConnectionStringBuilderAdapter());
        }