Exemplo n.º 1
0
        /// <summary>
        /// Wraps the provided DbProviderServices class in a MiniProfiler profiled DbService and returns the wrapped service.
        /// </summary>
        /// <param name="services">The DbProviderServices service to wrap.</param>
        /// <returns>A wrapped version of the DbProviderService service.</returns>
        private static DbProviderServices WrapProviderService(DbProviderServices services)
        {
            // First let's check our cache.
            if (ProviderCache.ContainsKey(services))
            {
                return ProviderCache[services];
            }

            // Then let's see if our type is already wrapped.
            var serviceType = services.GetType();
            while (serviceType != null)
            {
                if (serviceType.IsGenericType && serviceType.GetGenericTypeDefinition() == typeof(EFProfiledDbProviderServices<>))
                {
                    // Add it to the cache and return it.
                    ProviderCache[services] = services;
                    return services;
                }

                serviceType = serviceType.BaseType;
            }

            // Finally let's wrap it.
            var genericType = typeof(EFProfiledDbProviderServices<>);
            Type[] typeArgs = { services.GetType() };
            var createdType = genericType.MakeGenericType(typeArgs);
            var instanceProperty = createdType.GetField("Instance", BindingFlags.Public | BindingFlags.Static);
            var instance = instanceProperty.GetValue(null) as DbProviderServices;
            ProviderCache[services] = instance;
            return instance;
        }
Exemplo n.º 2
0
        private Func <System.Data.Entity.Core.Common.DbProviderManifest, System.Data.Entity.Core.Common.CommandTrees.DbCommandTree, System.Data.Entity.Core.Common.DbCommandDefinition> GetCreateDbCommandDefinitionFunction(string providerInvariantName)
        {
            Func <System.Data.Entity.Core.Common.DbProviderManifest, System.Data.Entity.Core.Common.CommandTrees.DbCommandTree, System.Data.Entity.Core.Common.DbCommandDefinition> result;

            lock (this.createDbCommandDefinitionFunctions)
            {
                if (!this.createDbCommandDefinitionFunctions.TryGetValue(providerInvariantName, out result))
                {
                    System.Data.Entity.Core.Common.DbProviderServices ps = GetProviderServicesByName(providerInvariantName);

                    // create a delegate to call CreateDbCommandDefinition using reflection
                    // (method is protected, but part of public API)
                    MethodInfo createCommandDefinitionMethodInfo = ps.GetType().GetMethod("CreateDbCommandDefinition", BindingFlags.NonPublic | BindingFlags.Instance);
                    result = (Func <System.Data.Entity.Core.Common.DbProviderManifest, System.Data.Entity.Core.Common.CommandTrees.DbCommandTree, System.Data.Entity.Core.Common.DbCommandDefinition>)Delegate.CreateDelegate(
                        typeof(Func <System.Data.Entity.Core.Common.DbProviderManifest, System.Data.Entity.Core.Common.CommandTrees.DbCommandTree, System.Data.Entity.Core.Common.DbCommandDefinition>),
                        ps,
                        createCommandDefinitionMethodInfo);
                    this.createDbCommandDefinitionFunctions[providerInvariantName] = result;
                }
            }

            return(result);
        }