public virtual void SetConfiguration(InternalConfiguration configuration) { DebugCheck.NotNull(configuration); var configurationType = _loader.TryLoadFromConfig(AppConfig.DefaultInstance); if (configurationType != null) { configuration = configurationType .CreateInstance <DbConfiguration>(Strings.CreateInstance_BadDbConfigurationType) .InternalConfiguration; } _newConfiguration = configuration.Owner; if (_configuration.Value.Owner.GetType() != configuration.Owner.GetType()) { if (_configuration.Value.Owner.GetType() == typeof(DbConfiguration)) { throw new InvalidOperationException(Strings.DefaultConfigurationUsedBeforeSet(configuration.Owner.GetType().Name)); } throw new InvalidOperationException( Strings.ConfigurationSetTwice(configuration.Owner.GetType().Name, _configuration.Value.Owner.GetType().Name)); } }
public void TryLoadFromConfig_throws_if_type_does_not_extend_DbConfiguration() { var mockConfig = new Mock<AppConfig>(new ConnectionStringSettingsCollection()); mockConfig.Setup(m => m.ConfigurationTypeName).Returns(typeof(Random).AssemblyQualifiedName); var loader = new DbConfigurationLoader(); Assert.Equal( Strings.CreateInstance_BadDbConfigurationType(typeof(Random).ToString(), typeof(DbConfiguration).ToString()), Assert.Throws<InvalidOperationException>(() => loader.TryLoadFromConfig(mockConfig.Object)).Message); }
public void TryLoadFromConfig_throws_if_type_cannot_be_loaded() { var mockConfig = new Mock<AppConfig>(new ConnectionStringSettingsCollection()); mockConfig.Setup(m => m.ConfigurationTypeName).Returns("I.Is.Not.A.Type"); var loader = new DbConfigurationLoader(); Assert.Equal( Strings.DbConfigurationTypeNotFound("I.Is.Not.A.Type"), Assert.Throws<InvalidOperationException>(() => loader.TryLoadFromConfig(mockConfig.Object)).Message); }
public void TryLoadFromConfig_throws_if_type_does_not_extend_DbConfiguration() { var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection()); mockConfig.Setup(m => m.ConfigurationTypeName).Returns(typeof(Random).AssemblyQualifiedName); var loader = new DbConfigurationLoader(); Assert.Equal( Strings.CreateInstance_BadDbConfigurationType(typeof(Random).ToString(), typeof(DbConfiguration).ToString()), Assert.Throws <InvalidOperationException>(() => loader.TryLoadFromConfig(mockConfig.Object)).Message); }
public void TryLoadFromConfig_throws_if_type_cannot_be_loaded() { var mockConfig = new Mock <AppConfig>(new ConnectionStringSettingsCollection()); mockConfig.Setup(m => m.ConfigurationTypeName).Returns("I.Is.Not.A.Type"); var loader = new DbConfigurationLoader(); Assert.Equal( Strings.DbConfigurationTypeNotFound("I.Is.Not.A.Type"), Assert.Throws <InvalidOperationException>(() => loader.TryLoadFromConfig(mockConfig.Object)).Message); }
public virtual void SetConfiguration(InternalConfiguration configuration, bool lookInConfig = true) { DebugCheck.NotNull(configuration); if (lookInConfig) { configuration = _loader.TryLoadFromConfig(AppConfig.DefaultInstance) ?? configuration; } _newConfiguration = configuration.Owner; if (_configuration.Value.Owner.GetType() != configuration.Owner.GetType()) { if (_configuration.Value.Owner.GetType() == typeof(DbConfiguration)) { throw new InvalidOperationException(Strings.DefaultConfigurationUsedBeforeSet(configuration.Owner.GetType().Name)); } throw new InvalidOperationException( Strings.ConfigurationSetTwice(configuration.Owner.GetType().Name, _configuration.Value.Owner.GetType().Name)); } }