/// <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)); } }
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()); }
protected override void Act() { _actualDatabaseNameReplacementToken = _databaseReplacementTokenProvider.GetDatabaseNameReplacementToken(); _actualServerNameReplacementToken = _databaseReplacementTokenProvider.GetServerNameReplacementToken(); }
protected override void Act() { _databaseReplacementTokenProvider.GetServerNameReplacementToken(); }
public string GetServerNameReplacementToken() => _next.GetServerNameReplacementToken();