Skip to content

Commit

Permalink
Make more services singletons
Browse files Browse the repository at this point in the history
Issue #6924

This makes services singletons based on:
- ILogger is now singleton
- There is only one database provider per container
  • Loading branch information
ajcvickers committed Apr 5, 2017
1 parent 67548dc commit a8127eb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,35 @@ private static readonly IDictionary<Type, ServiceCharacteristics> _relationalSer
{ typeof(IMigrationsIdGenerator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ISqlGenerationHelper), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalAnnotationProvider), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMigrationsAnnotationProvider), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsAnnotationProvider), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMigrationCommandExecutor), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalCommandBuilderFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRawSqlCommandBuilder), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMigrationsModelDiffer), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMigrationsSqlGenerator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalTypeMapper), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IRelationalValueBufferFactoryFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMaterializerFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IShaperCommandContextFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IConditionalRemovingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ISelectExpressionFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IExpressionFragmentTranslator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ISqlTranslatingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IUpdateSqlGenerator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMemberTranslator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMethodCallTranslator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IQuerySqlGeneratorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ICommandBatchPreparer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IModificationCommandBatchFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IIncludeExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationCommandExecutor), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsAssembly), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IBatchExecutor), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRelationalCommandBuilderFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRawSqlCommandBuilder), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ICommandBatchPreparer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsModelDiffer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMigrationsSqlGenerator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRelationalTypeMapper), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRelationalValueBufferFactoryFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMaterializerFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IShaperCommandContextFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IConditionalRemovingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ICompositePredicateExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IIncludeExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ISelectExpressionFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRelationalResultOperatorHandler), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IExpressionFragmentTranslator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ISqlTranslatingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IUpdateSqlGenerator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IModificationCommandBatchFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRelationalConnection), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRelationalDatabaseCreator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IHistoryRepository), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMemberTranslator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMethodCallTranslator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IQuerySqlGeneratorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) }
{ typeof(IHistoryRepository), new ServiceCharacteristics(ServiceLifetime.Scoped) }
};

/// <summary>
Expand Down Expand Up @@ -162,25 +162,25 @@ public override EntityFrameworkServicesBuilder TryAddCoreServices()
.AddDependencySingleton<RelationalSqlGenerationHelperDependencies>()
.AddDependencySingleton<RelationalTypeMapperDependencies>()
.AddDependencySingleton<RelationalCompositeExpressionFragmentTranslatorDependencies>()
.AddDependencySingleton<RelationalModelValidatorDependencies>()
.AddDependencySingleton<UpdateSqlGeneratorDependencies>()
.AddDependencySingleton<QuerySqlGeneratorDependencies>()
.AddDependencySingleton<RelationalCompositeMethodCallTranslatorDependencies>()
.AddDependencySingleton<MigrationsSqlGeneratorDependencies>()
.AddDependencySingleton<MigrationsAnnotationProviderDependencies>()
.AddDependencySingleton<SqlTranslatingExpressionVisitorDependencies>()
.AddDependencySingleton<ParameterNameGeneratorDependencies>()
.AddDependencySingleton<SelectExpressionDependencies>()
.AddDependencySingleton<RelationalValueBufferFactoryDependencies>()
.AddDependencySingleton<RelationalProjectionExpressionVisitorDependencies>()
.AddDependencyScoped<RelationalConventionSetBuilderDependencies>()
.AddDependencyScoped<UpdateSqlGeneratorDependencies>()
.AddDependencyScoped<QuerySqlGeneratorDependencies>()
.AddDependencyScoped<RelationalDatabaseCreatorDependencies>()
.AddDependencyScoped<RelationalCompositeMethodCallTranslatorDependencies>()
.AddDependencyScoped<MigrationsSqlGeneratorDependencies>()
.AddDependencyScoped<HistoryRepositoryDependencies>()
.AddDependencyScoped<MigrationsAnnotationProviderDependencies>()
.AddDependencyScoped<RelationalCompiledQueryCacheKeyGeneratorDependencies>()
.AddDependencyScoped<RelationalModelValidatorDependencies>()
.AddDependencyScoped<SqlTranslatingExpressionVisitorDependencies>()
.AddDependencyScoped<RelationalProjectionExpressionVisitorDependencies>()
.AddDependencyScoped<ParameterNameGeneratorDependencies>()
.AddDependencyScoped<RelationalQueryModelVisitorDependencies>()
.AddDependencyScoped<RelationalEntityQueryableExpressionVisitorDependencies>()
.AddDependencyScoped<RelationalConnectionDependencies>()
.AddDependencyScoped<RelationalDatabaseDependencies>()
.AddDependencyScoped<SelectExpressionDependencies>()
.AddDependencyScoped<RelationalValueBufferFactoryDependencies>()
.AddDependencyScoped<RelationalQueryCompilationContextDependencies>();

return base.TryAddCoreServices();
Expand Down
24 changes: 12 additions & 12 deletions src/EFCore/Infrastructure/EntityFrameworkServicesBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ private static readonly IDictionary<Type, ServiceCharacteristics> _coreServices
{ typeof(ISingletonOptionsInitialzer), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ILoggingOptions), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ISensitiveDataLogger<>), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IModelValidator), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ILogger<>), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ICompiledQueryCache), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IQueryAnnotationExtractor), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IEntityTrackingInfoFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ISubQueryMemberPushDownExpressionVisitor), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(ITaskBlockingExpressionVisitor), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IMemberAccessBindingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(INavigationRewritingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IQuerySourceTracingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IExpressionPrinter), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IProjectionExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Singleton) },
{ typeof(IKeyPropagator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(INavigationFixer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ILocalViewListener), new ServiceCharacteristics(ServiceLifetime.Scoped) },
Expand All @@ -78,26 +89,15 @@ private static readonly IDictionary<Type, ServiceCharacteristics> _coreServices
{ typeof(IDbContextServices), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IValueGeneratorSelector), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IConventionSetBuilder), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IModelValidator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IExecutionStrategyFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ICompiledQueryCache), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IAsyncQueryProvider), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IQueryCompiler), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IQueryAnnotationExtractor), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IQueryOptimizer), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IEntityTrackingInfoFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ISubQueryMemberPushDownExpressionVisitor), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ITaskBlockingExpressionVisitor), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IEntityResultFindingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IMemberAccessBindingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(INavigationRewritingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IQuerySourceTracingExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IRequiresMaterializationExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IExpressionPrinter), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IQueryCompilationContextFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ICompiledQueryCacheKeyGenerator), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IResultOperatorHandler), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IProjectionExpressionVisitorFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IModel), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(ICurrentDbContext), new ServiceCharacteristics(ServiceLifetime.Scoped) },
{ typeof(IDbContextOptions), new ServiceCharacteristics(ServiceLifetime.Scoped) },
Expand Down Expand Up @@ -244,11 +244,11 @@ public virtual EntityFrameworkServicesBuilder TryAddCoreServices()
.AddDependencySingleton<ResultOperatorHandlerDependencies>()
.AddDependencySingleton<ModelSourceDependencies>()
.AddDependencySingleton<ValueGeneratorCacheDependencies>()
.AddDependencySingleton<ModelValidatorDependencies>()
.AddDependency(typeof(SensitiveDataLoggerDependencies<>), ServiceLifetime.Singleton)
.AddDependencyScoped<ExecutionStrategyContextDependencies>()
.AddDependencyScoped<CompiledQueryCacheKeyGeneratorDependencies>()
.AddDependencyScoped<QueryContextDependencies>()
.AddDependencyScoped<ModelValidatorDependencies>()
.AddDependencyScoped<ValueGeneratorSelectorDependencies>()
.AddDependencyScoped<EntityQueryModelVisitorDependencies>()
.AddDependencyScoped<DatabaseDependencies>()
Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.Sqlite.FunctionalTests/BadDataSqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private NorthwindContext CreateContext(params object[] values)
var context = new NorthwindContext(
_fixture.BuildOptions(
new ServiceCollection()
.AddScoped<IRelationalCommandBuilderFactory, BadDataCommandBuilderFactory>()));
.AddSingleton<IRelationalCommandBuilderFactory, BadDataCommandBuilderFactory>()));

var badDataCommandBuilderFactory
= (BadDataCommandBuilderFactory)context.GetService<IRelationalCommandBuilderFactory>();
Expand Down

0 comments on commit a8127eb

Please sign in to comment.