Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement service-injection mechanism for patch/point releases #7550

Merged
merged 1 commit into from
Feb 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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