Skip to content

Instantly share code, notes, and snippets.

@fitzchak
Created January 15, 2013 15:43
Show Gist options
  • Save fitzchak/4539561 to your computer and use it in GitHub Desktop.
Save fitzchak/4539561 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Config;
using System.Data.Entity.Core.Common;
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Migrations.Sql;
using System.Data.Entity.SqlServer;
using System.Reflection;
using HibernatingRhinos.Profiler.Appender.EntityFramework;
using HibernatingRhinos.Profiler.Appender.ProfiledDataAccess;
namespace HibernatingRhinos.Profiler.IntegrationTests.EntityFramework6Beta2
{
public class ProfiledDbConfiguration : DbConfiguration
{
public ProfiledDbConfiguration()
{
AddDependencyResolver(new ProfiledDbDependencyResolver(this));
}
}
public class ProfiledDbDependencyResolver : IDbDependencyResolver
{
private readonly IDbDependencyResolver rootResolver;
#if DEBUG
public static HashSet<string> types = new HashSet<string>();
#endif
public ProfiledDbDependencyResolver(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);
}
public object GetService(Type type, object key)
{
#if DEBUG
types.Add(type.Name);
#endif
if (type == typeof(IDbProviderFactoryService))
{
var innerFactoryService = (IDbProviderFactoryService) rootResolver.GetService(type, key);
return new ProfiledDbProviderFactoryService(innerFactoryService);
}
if (type == typeof (DbProviderServices))
{
var inner = (DbProviderServices)rootResolver.GetService(type, key);
var appender = new EntityFrameworkAppender(typeof (SqlProviderServices).Name);
var profiledDbProviderServicesType = EntityFrameworkProfiler.CompiledAssembly.GetType("HibernatingRhinos.Profiler.Appender.EntityFramework.ProfiledDbProviderServices");
if (profiledDbProviderServicesType == null)
throw new InvalidOperationException("Could not get the profiled DbProviderServices.");
return Activator.CreateInstance(profiledDbProviderServicesType, new object[] {inner, appender});
}
if (type == typeof(MigrationSqlGenerator))
{
if (rootResolver.GetService(type, key) is SqlServerMigrationSqlGenerator)
{
return new ProfiledMigrationSqlGenerator();
}
}
return null;
}
}
public class ProfiledDbProviderFactoryService : IDbProviderFactoryService
{
private readonly IDbProviderFactoryService innerFactoryService;
public ProfiledDbProviderFactoryService(IDbProviderFactoryService innerFactoryService)
{
this.innerFactoryService = innerFactoryService;
}
public DbProviderFactory GetProviderFactory(DbConnection connection)
{
if (connection is ProfiledConnection)
{
var connectionType = connection.GetType();
if (connectionType.IsGenericType)
{
var innerProviderFactory = connectionType.GetGenericArguments()[0];
var profiledDbProviderFactory = EntityFrameworkProfiler.CompiledAssembly.GetType("HibernatingRhinos.Profiler.Appender.EntityFramework.ProfiledDbProviderFactory`1").MakeGenericType(innerProviderFactory);
return (DbProviderFactory) Activator.CreateInstance(profiledDbProviderFactory);
}
}
if (connection is EntityConnection)
return innerFactoryService.GetProviderFactory(connection);
throw new InvalidOperationException("Should have ProfiledConnection but got " + connection.GetType().FullName + ".If you got here, you probably need to modify the above code in order to satisfy the requirements of your application. This code indented to support EF 6 alpha 2.");
}
}
public class ProfiledMigrationSqlGenerator : SqlServerMigrationSqlGenerator
{
protected override DbConnection CreateConnection()
{
return DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment