public GlimpseDbDependencyResolver(DbConfiguration originalDbConfiguration)
 {
     // Get the original resolver
     var internalConfigProp = originalDbConfiguration.GetType().GetProperty("InternalConfiguration", BindingFlags.Instance | BindingFlags.NonPublic);
     var internalConfig = internalConfigProp.GetValue(originalDbConfiguration, null);
     var rootResolverProp = internalConfig.GetType().GetProperty("RootResolver", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
     rootResolver = (IDbDependencyResolver)rootResolverProp.GetValue(internalConfig, null);
 }
Exemplo n.º 2
0
            public void ModelCacheKeyFactory_can_be_set()
            {
                var configuration = new DbConfiguration();
                var cacheKeyFactory = new Mock<IDbModelCacheKeyFactory>().Object;

                configuration.ModelCacheKeyFactory = cacheKeyFactory;

                Assert.Same(cacheKeyFactory, configuration.ModelCacheKeyFactory);
            }
Exemplo n.º 3
0
            public void AddDependencyResolver_throws_if_the_configuation_is_locked()
            {
                var configuration = new DbConfiguration();
                configuration.Lock();

                Assert.Equal(
                    Strings.ConfigurationLocked("AddDependencyResolver"),
                    Assert.Throws<InvalidOperationException>(
                        () => configuration.AddDependencyResolver(new Mock<IDbDependencyResolver>().Object)).Message);
            }
Exemplo n.º 4
0
            public void SwitchInRootResolver_leaves_other_resolvers_intact()
            {
                var configuration = new DbConfiguration().InternalConfiguration;
                var mockResolver  = new Mock <IDbDependencyResolver>();

                configuration.AddDependencyResolver(mockResolver.Object);

                configuration.SwitchInRootResolver(new Mock <RootDependencyResolver>().Object);

                configuration.DependencyResolver.GetService <object>("Foo");
                mockResolver.Verify(m => m.GetService(typeof(object), "Foo"));
            }
Exemplo n.º 5
0
            public void SetSpatialProvider_throws_if_no_ProviderInvariantNameAttribute()
            {
                var mockInternalConfiguration = new Mock <InternalConfiguration>(null, null, null, null);
                var spatialServices           = new Mock <DbSpatialServices>().Object;

                var configuration = new DbConfiguration(mockInternalConfiguration.Object);

                Assert.Equal(
                    Strings.DbProviderNameAttributeNotFound(spatialServices.GetType()),
                    Assert.Throws <InvalidOperationException>(
                        () => configuration.AddDbSpatialServices(spatialServices)).Message);
            }
Exemplo n.º 6
0
            public void AddMigrationSqlGenerator_throws_if_no_ProviderInvariantNameAttribute()
            {
                var mockInternalConfiguration = new Mock <InternalConfiguration>(null, null, null, null);
                var generator = new Func <MigrationSqlGenerator>(() => new Mock <MigrationSqlGenerator>().Object);

                var configuration = new DbConfiguration(mockInternalConfiguration.Object);

                Assert.Equal(
                    Strings.DbProviderNameAttributeNotFound(typeof(MigrationSqlGenerator)),
                    Assert.Throws <InvalidOperationException>(
                        () => configuration.AddMigrationSqlGenerator(generator)).Message);
            }
Exemplo n.º 7
0
            public void Throws_if_no_ProviderInvariantNameAttribute()
            {
                var mockInternalConfiguration = new Mock <InternalConfiguration>(null, null, null, null);
                var executionStrategy         = new Func <IExecutionStrategy>(() => new Mock <IExecutionStrategy>().Object);

                var configuration = new DbConfiguration(mockInternalConfiguration.Object);

                Assert.Equal(
                    Strings.DbProviderNameAttributeNotFound(typeof(IExecutionStrategy).FullName),
                    Assert.Throws <InvalidOperationException>(
                        () => configuration.AddExecutionStrategy(executionStrategy, "a")).Message);
            }
Exemplo n.º 8
0
            public void SwitchInRootResolver_swicthes_in_given_root_resolver()
            {
                var configuration    = new DbConfiguration().InternalConfiguration;
                var mockRootResolver = new Mock <RootDependencyResolver>();

                configuration.SwitchInRootResolver(mockRootResolver.Object);

                Assert.Same(mockRootResolver.Object, configuration.RootResolver);

                configuration.DependencyResolver.GetService <object>("Foo");
                mockRootResolver.Verify(m => m.GetService(typeof(object), "Foo"));
            }
Exemplo n.º 9
0
        private static Mock <InternalConfiguration> CreateMockInternalConfiguration(DbConfiguration dbConfiguration = null)
        {
            var mockInternalConfiguration = new Mock <InternalConfiguration>();

            if (dbConfiguration == null)
            {
                var mockDbConfiguration = new Mock <DbConfiguration>();
                mockDbConfiguration.Setup(m => m.InternalConfiguration).Returns(mockInternalConfiguration.Object);
                dbConfiguration = mockDbConfiguration.Object;
            }

            mockInternalConfiguration.Setup(m => m.Owner).Returns(dbConfiguration);
            return(mockInternalConfiguration);
        }
Exemplo n.º 10
0
            public void ResolverSnapshot_returns_copy_of_resolver_chain()
            {
                var configuration = new DbConfiguration().InternalConfiguration;
                var resolver1     = new Mock <IDbDependencyResolver>();

                configuration.AddDependencyResolver(resolver1.Object);

                var snapshot = configuration.ResolverSnapshot;

                var resolver2 = new Mock <IDbDependencyResolver>();

                configuration.AddDependencyResolver(resolver2.Object);

                snapshot.GetService <object>("Foo");
                resolver1.Verify(m => m.GetService(typeof(object), "Foo"), Times.Once());
                resolver2.Verify(m => m.GetService(typeof(object), "Foo"), Times.Never());
            }
        private static Mock <InternalConfiguration> CreateMockInternalConfiguration(
            DbConfiguration dbConfiguration = null, IDbDependencyResolver snapshot = null)
        {
            var mockInternalConfiguration = new Mock <InternalConfiguration>(null, null, null, null);

            if (dbConfiguration == null)
            {
                var mockDbConfiguration = new Mock <DbConfiguration>();
                mockDbConfiguration.Setup(m => m.InternalConfiguration).Returns(mockInternalConfiguration.Object);
                dbConfiguration = mockDbConfiguration.Object;
            }

            mockInternalConfiguration.Setup(m => m.Owner).Returns(dbConfiguration);
            mockInternalConfiguration.Setup(m => m.ResolverSnapshot).Returns(snapshot);

            return(mockInternalConfiguration);
        }
        public virtual void SetConfiguration(DbConfiguration configuration)
        {
            //Contract.Requires(configuration != null);

            configuration = _loader.TryLoadFromConfig(AppConfig.DefaultInstance) ?? configuration;
            _newConfiguration = configuration;

            if (_configuration.Value.GetType() != configuration.GetType())
            {
                if (_configuration.Value.GetType() == typeof(DbConfiguration))
                {
                    throw new InvalidOperationException(Strings.DefaultConfigurationUsedBeforeSet(configuration.GetType().Name));
                }

                throw new InvalidOperationException(
                    Strings.ConfigurationSetTwice(configuration.GetType().Name, _configuration.Value.GetType().Name));
            }
        }
Exemplo n.º 13
0
        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));
            }
        }
Exemplo n.º 14
0
            public void ResolverSnapshot_returns_resolvers_in_correct_order()
            {
                var callOrder = "";

                var configResolver = new Mock <IDbDependencyResolver>();

                configResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Config");
                var configChain = new ResolverChain();

                configChain.Add(configResolver.Object);

                var rootResolver = new Mock <RootDependencyResolver>();

                rootResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Root");

                var configuration = new DbConfiguration(
                    new InternalConfiguration(
                        configChain,
                        new ResolverChain(),
                        rootResolver.Object,
                        new Mock <AppConfigDependencyResolver>().Object)).InternalConfiguration;

                var normalResolver = new Mock <IDbDependencyResolver>();

                normalResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Normal");
                configuration.AddDependencyResolver(normalResolver.Object);

                var overrideResolver = new Mock <IDbDependencyResolver>();

                overrideResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Override");
                configuration.AddDependencyResolver(overrideResolver.Object, overrideConfigFile: true);

                configuration.ResolverSnapshot.GetService <object>("Foo");

                Assert.Equal(" Override Config Normal Root", callOrder);
            }
Exemplo n.º 15
0
            public void SwitchInRootResolver_swicthes_in_given_root_resolver()
            {
                var configuration = new DbConfiguration();
                var mockRootResolver = new Mock<RootDependencyResolver>();
                configuration.SwitchInRootResolver(mockRootResolver.Object);

                Assert.Same(mockRootResolver.Object, configuration.RootResolver);

                configuration.DependencyResolver.GetService<object>("Foo");
                mockRootResolver.Verify(m => m.GetService(typeof(object), "Foo"));
            }
Exemplo n.º 16
0
            public void RootResolver_returns_the_root_resolver()
            {
                var rootResolver = new RootDependencyResolver();

                var config = new DbConfiguration(new Mock<ResolverChain>().Object, new Mock<ResolverChain>().Object, rootResolver);

                Assert.Same(rootResolver, config.RootResolver);
            }
Exemplo n.º 17
0
            public void DependencyResolver_returns_the_dependency_resolver_in_use()
            {
                var mockAppConfigChain = new Mock<ResolverChain>();
                var mockNormalChain = new Mock<ResolverChain>();

                var config = new DbConfiguration(
                    mockAppConfigChain.Object, mockNormalChain.Object,
                    new RootDependencyResolver());
                var resolver = (CompositeResolver<ResolverChain, ResolverChain>)config.DependencyResolver;

                Assert.Same(mockAppConfigChain.Object, resolver.First);
                Assert.Same(mockNormalChain.Object, resolver.Second);
            }
Exemplo n.º 18
0
            public void Getting_DefaultConnectionFactory_returns_factory_set_by_legacy_API()
            {
                var configuration = new DbConfiguration();
                var legacyFactory = new Mock<IDbConnectionFactory>().Object;
                var factory = new Mock<IDbConnectionFactory>().Object;

                try
                {
#pragma warning disable 612,618
                    Database.DefaultConnectionFactory = legacyFactory;
#pragma warning restore 612,618

                    configuration.DefaultConnectionFactory = factory;

                    Assert.Same(legacyFactory, configuration.DefaultConnectionFactory);
                }
                finally
                {
                    Database.ResetDefaultConnectionFactory();
                }
            }
Exemplo n.º 19
0
            public void Getting_DefaultConnectionFactory_returns_factory_previously_set()
            {
                var configuration = new DbConfiguration();
                var factory = new Mock<IDbConnectionFactory>().Object;

                configuration.DefaultConnectionFactory = factory;

                Assert.Same(factory, configuration.DefaultConnectionFactory);
            }
            public void ResolverSnapshot_returns_copy_of_resolver_chain()
            {
                var configuration = new DbConfiguration().InternalConfiguration;
                var resolver1 = new Mock<IDbDependencyResolver>();
                configuration.AddDependencyResolver(resolver1.Object);

                var snapshot = configuration.ResolverSnapshot;

                var resolver2 = new Mock<IDbDependencyResolver>();
                configuration.AddDependencyResolver(resolver2.Object);

                snapshot.GetService<object>("Foo");
                resolver1.Verify(m => m.GetService(typeof(object), "Foo"), Times.Once());
                resolver2.Verify(m => m.GetService(typeof(object), "Foo"), Times.Never());
            }
Exemplo n.º 21
0
 public void DefaultConnectionFactory_set_in_code_can_be_overriden_before_config_is_locked()
 {
     Assert.IsType <SqlConnectionFactory>(DbConfiguration.GetService <IDbConnectionFactory>());
     Assert.IsType <DefaultUnitTestsConnectionFactory>(FunctionalTestsConfiguration.OriginalConnectionFactories[0]);
 }
Exemplo n.º 22
0
            public void AddProvider_throws_if_the_configuation_is_locked()
            {
                var configuration = new DbConfiguration();
                configuration.Lock();

                Assert.Equal(
                    Strings.ConfigurationLocked("AddProvider"),
                    Assert.Throws<InvalidOperationException>(
                        () => configuration.AddProvider("Karl", new Mock<DbProviderServices>().Object)).Message);
            }
            public void ReplaceService_wraps_service_and_returns_wrapped_service()
            {
                var originalService = new Mock<IPilkington>().Object;
                var wrappedService = new Mock<IPilkington>().Object;
                
                var resolver = new Mock<IDbDependencyResolver>();
                resolver.Setup(m => m.GetService(typeof(IPilkington), "Foo")).Returns(originalService);

                var internalConfiguration = new DbConfiguration().InternalConfiguration;
                internalConfiguration.AddDependencyResolver(resolver.Object);

                new DbConfigurationEventArgs(internalConfiguration)
                    .ReplaceService<IPilkington>(
                    (s, k) =>
                    {
                        Assert.Same(originalService, s);
                        Assert.Equal("Foo", k);
                        return wrappedService;
                    });

                Assert.Same(wrappedService, internalConfiguration.DependencyResolver.GetService<IPilkington>("Foo"));
                resolver.Verify(m => m.GetService(typeof(IPilkington), "Foo"));
            }
            public void RootResolver_returns_the_root_resolver()
            {
                var rootResolver = new RootDependencyResolver(new MigrationsConfigurationResolver(), new DefaultProviderServicesResolver());

                var config = new DbConfiguration(new Mock<ResolverChain>().Object, new Mock<ResolverChain>().Object, rootResolver);

                Assert.Same(rootResolver, config.RootResolver);
            }
Exemplo n.º 25
0
 public void DbConfiguration_cannot_be_set_to_null()
 {
     Assert.Equal(
         "configuration",
         Assert.Throws <ArgumentNullException>(() => DbConfiguration.SetConfiguration(null)).ParamName);
 }
        private static Mock<InternalConfiguration> CreateMockInternalConfiguration(
            DbConfiguration dbConfiguration = null, IDbDependencyResolver snapshot = null)
        {
            var mockInternalConfiguration = new Mock<InternalConfiguration>(null, null, null, null);

            if (dbConfiguration == null)
            {
                var mockDbConfiguration = new Mock<DbConfiguration>();
                mockDbConfiguration.Setup(m => m.InternalConfiguration).Returns(mockInternalConfiguration.Object);
                dbConfiguration = mockDbConfiguration.Object;
            }

            mockInternalConfiguration.Setup(m => m.Owner).Returns(dbConfiguration);
            mockInternalConfiguration.Setup(m => m.ResolverSnapshot).Returns(snapshot);

            return mockInternalConfiguration;
        }
Exemplo n.º 27
0
        /// <summary>
        ///     The Singleton instance of <see cref="DbConfiguration" /> for this app domain. This can be
        ///     set at application start before any Entity Framework features have been used and afterwards
        ///     should be treated as read-only.
        /// </summary>
        public static void SetConfiguration(DbConfiguration configuration)
        {
            Check.NotNull(configuration, "configuration");

            InternalConfiguration.Instance = configuration.InternalConfiguration;
        }
        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));
            }
        }
Exemplo n.º 29
0
            public void GetProvider_returns_provider_added_by_AddProvider()
            {
                var configuration = new DbConfiguration();
                var provider = new Mock<DbProviderServices>().Object;

                configuration.AddProvider("Karl", provider);

                Assert.Same(provider, configuration.GetProvider("Karl"));
            }
Exemplo n.º 30
0
            public void SwitchInRootResolver_leaves_other_resolvers_intact()
            {
                var configuration = new DbConfiguration();
                var mockResolver = new Mock<IDbDependencyResolver>();
                configuration.AddDependencyResolver(mockResolver.Object);

                configuration.SwitchInRootResolver(new Mock<RootDependencyResolver>().Object);

                configuration.DependencyResolver.GetService<object>("Foo");
                mockResolver.Verify(m => m.GetService(typeof(object), "Foo"));
            }
Exemplo n.º 31
0
            public void Setting_DefaultConnectionFactory_throws_if_the_configuation_is_locked()
            {
                var configuration = new DbConfiguration();
                configuration.Lock();

                Assert.Equal(
                    Strings.ConfigurationLocked("DefaultConnectionFactory"),
                    Assert.Throws<InvalidOperationException>(
                        () => configuration.DefaultConnectionFactory = new Mock<IDbConnectionFactory>().Object).Message);
            }
Exemplo n.º 32
0
 public void Delegates_to_internal_configuration()
 {
     Assert.NotNull(DbConfiguration.GetService <IDbCommandInterceptor>(null));
 }
            public void ResolverSnapshot_returns_resolvers_in_correct_order()
            {
                var callOrder = "";

                var configResolver = new Mock<IDbDependencyResolver>();
                configResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Config");
                var configChain = new ResolverChain();
                configChain.Add(configResolver.Object);

                var rootResolver = new Mock<RootDependencyResolver>();
                rootResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Root");

                var configuration = new DbConfiguration(
                    new InternalConfiguration(
                        configChain,
                        new ResolverChain(),
                        rootResolver.Object,
                        new Mock<AppConfigDependencyResolver>().Object)).InternalConfiguration;

                var normalResolver = new Mock<IDbDependencyResolver>();
                normalResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Normal");
                configuration.AddDependencyResolver(normalResolver.Object);

                var overrideResolver = new Mock<IDbDependencyResolver>();
                overrideResolver.Setup(m => m.GetService(typeof(object), "Foo")).Callback(() => callOrder += " Override");
                configuration.AddDependencyResolver(overrideResolver.Object, overrideConfigFile: true);

                configuration.ResolverSnapshot.GetService<object>("Foo");

                Assert.Equal(" Override Config Normal Root", callOrder);
            }
Exemplo n.º 34
0
 public void Default_IDbModelCacheKeyFactory_is_returned_by_default()
 {
     Assert.IsType <DefaultModelCacheKeyFactory>(DbConfiguration.GetService <IDbModelCacheKeyFactory>());
 }
Exemplo n.º 35
0
        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));
            }
        }