Skip to content

Commit

Permalink
Implement service-injection mechanism for patch/point releases
Browse files Browse the repository at this point in the history
Issue #7465

This change introduces a new class ServiceCollectionMap that is used by providers and by our service-adding methods. The new class builds a map from service type to indexes of the ServiceDescriptors in the list for the given type. This allows all the TryAdd calls that we make to work without scanning the list every time.

In addition, this type has a method for re-writing services to do property injection when we need to add a service dependency without breaking a constructor in a patch or point release. This is used by making a call at the end of our service registrations like so:

```C#
   ...
   .TryAddScoped<IResultOperatorHandler, ResultOperatorHandler>()
   .DoPatchInjection<IValueGeneratorSelector>();
```

Also, ReplaceService code has been updated to ensure that service-injection is also run on any replaced services, since these services may also inherit from our base classes.
  • Loading branch information
ajcvickers committed Feb 3, 2017
1 parent 994500c commit 4112c26
Show file tree
Hide file tree
Showing 20 changed files with 1,438 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,21 @@ public static IServiceCollection AddEntityFrameworkInMemoryDatabase([NotNull] th
{
Check.NotNull(serviceCollection, nameof(serviceCollection));

serviceCollection.TryAddEnumerable(ServiceDescriptor
.Singleton<IDatabaseProvider, DatabaseProvider<InMemoryOptionsExtension>>());
var serviceCollectionMap = new ServiceCollectionMap(serviceCollection)
.TryAddSingletonEnumerable<IDatabaseProvider, DatabaseProvider<InMemoryOptionsExtension>>()
.TryAddSingleton<IInMemoryStoreSource, InMemoryStoreSource>()
.TryAddSingleton<IInMemoryTableFactory, InMemoryTableFactory>()
.TryAddScoped<IValueGeneratorSelector, InMemoryValueGeneratorSelector>()
.TryAddScoped<IInMemoryDatabase, InMemoryDatabase>()
.TryAddScoped<IDatabase>(p => p.GetService<IInMemoryDatabase>())
.TryAddScoped<IDbContextTransactionManager, InMemoryTransactionManager>()
.TryAddScoped<IDatabaseCreator, InMemoryDatabaseCreator>()
.TryAddScoped<IMaterializerFactory, MaterializerFactory>()
.TryAddScoped<IQueryContextFactory, InMemoryQueryContextFactory>()
.TryAddScoped<IEntityQueryModelVisitorFactory, InMemoryQueryModelVisitorFactory>()
.TryAddScoped<IEntityQueryableExpressionVisitorFactory, InMemoryEntityQueryableExpressionVisitorFactory>();

serviceCollection.TryAdd(new ServiceCollection()
.AddSingleton<IInMemoryStoreSource, InMemoryStoreSource>()
.AddSingleton<IInMemoryTableFactory, InMemoryTableFactory>()
.AddScoped<IValueGeneratorSelector, InMemoryValueGeneratorSelector>()
.AddScoped<IInMemoryDatabase, InMemoryDatabase>()
.AddScoped<IDatabase>(p => p.GetService<IInMemoryDatabase>())
.AddScoped<IDbContextTransactionManager, InMemoryTransactionManager>()
.AddScoped<IDatabaseCreator, InMemoryDatabaseCreator>()
.AddScoped<IMaterializerFactory, MaterializerFactory>()
.AddScoped<IQueryContextFactory, InMemoryQueryContextFactory>()
.AddScoped<IEntityQueryModelVisitorFactory, InMemoryQueryModelVisitorFactory>()
.AddScoped<IEntityQueryableExpressionVisitorFactory, InMemoryEntityQueryableExpressionVisitorFactory>());

ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(serviceCollection);
ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(serviceCollectionMap);

return serviceCollection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

// Intentionally in this namespace since this is for use by other relational providers rather than
// by top-level app developers.
Expand All @@ -37,64 +36,57 @@ public static class ServiceCollectionRelationalProviderInfrastructure
/// providers after registering provider-specific services to fill-in the remaining services with
/// Entity Framework defaults.
/// </summary>
/// <param name="serviceCollection"> The <see cref="IServiceCollection" /> to add services to. </param>
public static void TryAddDefaultRelationalServices([NotNull] IServiceCollection serviceCollection)
/// <param name="serviceCollectionMap"> The <see cref="IServiceCollection" /> to add services to. </param>
public static void TryAddDefaultRelationalServices([NotNull] ServiceCollectionMap serviceCollectionMap)
{
Check.NotNull(serviceCollection, nameof(serviceCollection));
Check.NotNull(serviceCollectionMap, nameof(serviceCollectionMap));

serviceCollection
.TryAdd(new ServiceCollection()
.AddSingleton(s => new DiagnosticListener("Microsoft.EntityFrameworkCore"))
.AddSingleton<DiagnosticSource>(s => s.GetService<DiagnosticListener>())
.AddSingleton<IParameterNameGeneratorFactory, ParameterNameGeneratorFactory>()
.AddSingleton<IComparer<ModificationCommand>, ModificationCommandComparer>()
.AddSingleton<IMigrationsIdGenerator, MigrationsIdGenerator>()
.AddSingleton<IKeyValueIndexFactorySource, KeyValueIndexFactorySource>()
.AddSingleton<IModelSource, RelationalModelSource>()
.AddScoped<IMigrationsAnnotationProvider, MigrationsAnnotationProvider>()
.AddScoped<IModelValidator, RelationalModelValidator>()
.AddScoped<IMigrator, Migrator>()
.AddScoped<IMigrationCommandExecutor, MigrationCommandExecutor>()
.AddScoped<IMigrationsAssembly, MigrationsAssembly>()
.AddScoped<IDatabase, RelationalDatabase>()
.AddScoped<IBatchExecutor, BatchExecutor>()
.AddScoped<IValueGeneratorSelector, RelationalValueGeneratorSelector>()
.AddScoped<IRelationalCommandBuilderFactory, RelationalCommandBuilderFactory>()
.AddScoped<IRawSqlCommandBuilder, RawSqlCommandBuilder>()
.AddScoped<ICommandBatchPreparer, CommandBatchPreparer>()
.AddScoped<IMigrationsModelDiffer, MigrationsModelDiffer>()
.AddScoped<IMigrationsSqlGenerator, MigrationsSqlGenerator>()
.AddScoped<IExecutionStrategyFactory, RelationalExecutionStrategyFactory>()
.AddScoped<IRelationalTypeMapper, RelationalTypeMapper>()
.AddScoped<IRelationalValueBufferFactoryFactory, TypedRelationalValueBufferFactoryFactory>()
.AddScoped<IDatabaseCreator>(p => p.GetService<IRelationalDatabaseCreator>())
.AddScoped<IDbContextTransactionManager>(p => p.GetService<IRelationalConnection>())
.AddScoped<IMaterializerFactory, MaterializerFactory>()
.AddScoped<IShaperCommandContextFactory, ShaperCommandContextFactory>()
.AddScoped<IConditionalRemovingExpressionVisitorFactory, ConditionalRemovingExpressionVisitorFactory>()
.AddScoped<ICompositePredicateExpressionVisitorFactory, CompositePredicateExpressionVisitorFactory>()
.AddScoped<IIncludeExpressionVisitorFactory, IncludeExpressionVisitorFactory>()
.AddScoped<IQueryFlattenerFactory, QueryFlattenerFactory>()
.AddScoped<ISelectExpressionFactory, SelectExpressionFactory>()
.AddScoped<IExpressionPrinter, RelationalExpressionPrinter>()
.AddScoped<IRelationalResultOperatorHandler, RelationalResultOperatorHandler>()
.AddScoped<IQueryContextFactory, RelationalQueryContextFactory>()
.AddScoped<IQueryCompilationContextFactory, RelationalQueryCompilationContextFactory>()
.AddScoped<IEntityQueryableExpressionVisitorFactory, RelationalEntityQueryableExpressionVisitorFactory>()
.AddScoped<IEntityQueryModelVisitorFactory, RelationalQueryModelVisitorFactory>()
.AddScoped<IProjectionExpressionVisitorFactory, RelationalProjectionExpressionVisitorFactory>()
.AddScoped<ICompiledQueryCacheKeyGenerator, RelationalCompiledQueryCacheKeyGenerator>()
.AddScoped<IExpressionFragmentTranslator, RelationalCompositeExpressionFragmentTranslator>()
.AddScoped<ISqlTranslatingExpressionVisitorFactory, SqlTranslatingExpressionVisitorFactory>());
serviceCollectionMap
.TryAddSingleton(s => new DiagnosticListener("Microsoft.EntityFrameworkCore"))
.TryAddSingleton<DiagnosticSource>(s => s.GetService<DiagnosticListener>())
.TryAddSingleton<IParameterNameGeneratorFactory, ParameterNameGeneratorFactory>()
.TryAddSingleton<IComparer<ModificationCommand>, ModificationCommandComparer>()
.TryAddSingleton<IMigrationsIdGenerator, MigrationsIdGenerator>()
.TryAddSingleton<IKeyValueIndexFactorySource, KeyValueIndexFactorySource>()
.TryAddSingleton<IModelSource, RelationalModelSource>()
.TryAddScoped<IMigrationsAnnotationProvider, MigrationsAnnotationProvider>()
.TryAddScoped<IModelValidator, RelationalModelValidator>()
.TryAddScoped<IMigrator, Migrator>()
.TryAddScoped<IMigrationCommandExecutor, MigrationCommandExecutor>()
.TryAddScoped<IMigrationsAssembly, MigrationsAssembly>()
.TryAddScoped<IDatabase, RelationalDatabase>()
.TryAddScoped<IBatchExecutor, BatchExecutor>()
.TryAddScoped<IValueGeneratorSelector, RelationalValueGeneratorSelector>()
.TryAddScoped<IRelationalCommandBuilderFactory, RelationalCommandBuilderFactory>()
.TryAddScoped<IRawSqlCommandBuilder, RawSqlCommandBuilder>()
.TryAddScoped<ICommandBatchPreparer, CommandBatchPreparer>()
.TryAddScoped<IMigrationsModelDiffer, MigrationsModelDiffer>()
.TryAddScoped<IMigrationsSqlGenerator, MigrationsSqlGenerator>()
.TryAddScoped<IExecutionStrategyFactory, RelationalExecutionStrategyFactory>()
.TryAddScoped<IRelationalTypeMapper, RelationalTypeMapper>()
.TryAddScoped<IRelationalValueBufferFactoryFactory, TypedRelationalValueBufferFactoryFactory>()
.TryAddScoped<IDatabaseCreator>(p => p.GetService<IRelationalDatabaseCreator>())
.TryAddScoped<IDbContextTransactionManager>(p => p.GetService<IRelationalConnection>())
.TryAddScoped<IMaterializerFactory, MaterializerFactory>()
.TryAddScoped<IShaperCommandContextFactory, ShaperCommandContextFactory>()
.TryAddScoped<IConditionalRemovingExpressionVisitorFactory, ConditionalRemovingExpressionVisitorFactory>()
.TryAddScoped<ICompositePredicateExpressionVisitorFactory, CompositePredicateExpressionVisitorFactory>()
.TryAddScoped<IIncludeExpressionVisitorFactory, IncludeExpressionVisitorFactory>()
.TryAddScoped<IQueryFlattenerFactory, QueryFlattenerFactory>()
.TryAddScoped<ISelectExpressionFactory, SelectExpressionFactory>()
.TryAddScoped<IExpressionPrinter, RelationalExpressionPrinter>()
.TryAddScoped<IRelationalResultOperatorHandler, RelationalResultOperatorHandler>()
.TryAddScoped<IQueryContextFactory, RelationalQueryContextFactory>()
.TryAddScoped<IQueryCompilationContextFactory, RelationalQueryCompilationContextFactory>()
.TryAddScoped<IEntityQueryableExpressionVisitorFactory, RelationalEntityQueryableExpressionVisitorFactory>()
.TryAddScoped<IEntityQueryModelVisitorFactory, RelationalQueryModelVisitorFactory>()
.TryAddScoped<IProjectionExpressionVisitorFactory, RelationalProjectionExpressionVisitorFactory>()
.TryAddScoped<ICompiledQueryCacheKeyGenerator, RelationalCompiledQueryCacheKeyGenerator>()
.TryAddScoped<IExpressionFragmentTranslator, RelationalCompositeExpressionFragmentTranslator>()
.TryAddScoped<ISqlTranslatingExpressionVisitorFactory, SqlTranslatingExpressionVisitorFactory>()
.TryAddScoped<RelationalConnectionDependencies, RelationalConnectionDependencies>();

// Add service dependencies parameter classes.
// These are added as concrete types because the classes are sealed and the registrations should
// not be changed by provider or application code.
serviceCollection
.TryAdd(new ServiceCollection()
.AddScoped<RelationalConnectionDependencies>());

ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(serviceCollection);
ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(serviceCollectionMap);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

Expand All @@ -17,13 +18,22 @@ protected EntityFrameworkServiceCollectionExtensionsTest(TestHelpers testHelpers
}

[Fact]
public virtual void Repeated_calls_to_add_do_not_modify_collection()
public void Calling_AddEntityFramework_explicitly_does_not_change_services()
{
var expectedCollection = AddServices(new ServiceCollection());
var services1 = AddServices(new ServiceCollection());
var services2 = AddServices(new ServiceCollection());

ServiceCollectionProviderInfrastructure.TryAddDefaultEntityFrameworkServices(new ServiceCollectionMap(services2));

var actualCollection = AddServices(AddServices(new ServiceCollection()));
AssertServicesSame(services1, services2);
}

AssertServicesSame(expectedCollection, actualCollection);
[Fact]
public virtual void Repeated_calls_to_add_do_not_modify_collection()
{
AssertServicesSame(
AddServices(new ServiceCollection()),
AddServices(AddServices(new ServiceCollection())));
}

protected virtual void AssertServicesSame(IServiceCollection services1, IServiceCollection services2)
Expand Down
Loading

0 comments on commit 4112c26

Please sign in to comment.