public void SetConstructInstanceInterceptor_SetNewInstance_ShouldBeSetOnceTime()
        {
            // arrange
            var calls = new List <string>();

            GlobalDbFactoriesProvider.Set("bbb", new TestDbProviderFactory());
            GlobalDbFactoriesProvider.SetConstructInstanceInterceptor(
                instance =>
            {
                if (!(instance is TestDbProviderFactory))
                {
                    return(instance);
                }

                calls.Add("ExecuteConstructInstanceInterceptor");

                var new_instance = new TestDbProviderFactory2();
                GlobalDbFactoriesProvider.Set("bbb", new_instance);

                return(new_instance);
            });


            // act
            var result_1 = GlobalDbFactoriesProvider.Get("bbb");
            var result_2 = GlobalDbFactoriesProvider.Get("bbb");


            // assert
            result_1.Should().BeOfType <TestDbProviderFactory2>();
            result_2.Should().BeOfType <TestDbProviderFactory2>();
            calls.Should().BeEquivalentTo("ExecuteConstructInstanceInterceptor");
        }
예제 #2
0
        private static void ReplaceProviderFactories()
        {
#if NETFRAMEWORK
            var table = GetDbProviderFactoryConfigTable();
            if (table == null)
            {
                return;
            }

            foreach (var row in table.Rows.Cast <DataRow>().ToList())
            {
                DbProviderFactory factory;
                try
                {
                    factory = DbProviderFactories.GetFactory(row);
                }
                // ReSharper disable once CatchAllClause
                catch (Exception)
                {
                    continue;
                }

                if (factory is ProfiledDbProviderFactory)
                {
                    continue;
                }

                var proxy_type = typeof(ProfiledDbProviderFactory <>).MakeGenericType(factory.GetType());

                var wrapped_provider_row = table.NewRow();
                wrapped_provider_row["Name"]                  = row["Name"];
                wrapped_provider_row["Description"]           = row["Description"];
                wrapped_provider_row["InvariantName"]         = row["InvariantName"];
                wrapped_provider_row["AssemblyQualifiedName"] = proxy_type.AssemblyQualifiedName;

                table.Rows.Remove(row);
                table.Rows.Add(wrapped_provider_row);
            }
#endif
#if NETSTANDARD
            GlobalDbFactoriesProvider.SetConstructInstanceInterceptor(
                instance =>
            {
                if (instance is ProfiledDbProviderFactory)
                {
                    return(instance);
                }

                var new_instance = (DbProviderFactory)Activator.CreateInstance(typeof(ProfiledDbProviderFactory <>).MakeGenericType(instance.GetType()));

                GlobalDbFactoriesProvider.Set(instance.GetType().Namespace, new_instance);

                return(new_instance);
            });
#endif
        }
        public void Get_BuiltInSqlClientFactory_ShouldBeCorrectlyType()
        {
            // arrange


            // act
            var result = GlobalDbFactoriesProvider.Get("System.Data.SqlClient");


            // assert
            result.Should().BeOfType <SqlClientFactory>();
        }
예제 #4
0
        public static DbProviderFactory TryGetProviderFactory(this DbConnection connection)
        {
            if (connection is ProfiledDbConnection wrapped_db_connection)
            {
                return(wrapped_db_connection.InnerProviderFactory);
            }

#if !NETSTANDARD
            return(DbProviderFactories.GetFactory(connection));
#else
            return(GlobalDbFactoriesProvider.Get(connection));
#endif
        }
        public void Set_ShouldBeCorrectlyType()
        {
            // arrange


            // act
            GlobalDbFactoriesProvider.Set("aaa", new TestDbProviderFactory());
            var result = GlobalDbFactoriesProvider.Get("aaa");


            // assert
            result.Should().BeOfType <TestDbProviderFactory>();
        }