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

Use TryAdd in AddXxx methods to only add services if not already present. #1180

Closed
wants to merge 1 commit into from
Closed
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 @@ -20,7 +20,7 @@ public static EntityServicesBuilder AddAzureTableStorage([NotNull] this EntitySe
{
Check.NotNull(builder, "builder");

builder.ServiceCollection
builder.ServiceCollection.TryAdd(new ServiceCollection()
.AddSingleton<AtsQueryFactory>()
.AddSingleton<TableEntityAdapterFactory>()
.AddSingleton<AtsValueReaderFactory>()
Expand All @@ -31,7 +31,7 @@ public static EntityServicesBuilder AddAzureTableStorage([NotNull] this EntitySe
.AddScoped<AtsDatabase>()
.AddScoped<AtsDataStore>()
.AddScoped<AtsConnection>()
.AddScoped<AtsDataStoreCreator>();
.AddScoped<AtsDataStoreCreator>());

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static EntityServicesBuilder AddInMemoryStore([NotNull] this EntityServic
{
Check.NotNull(builder, "builder");

builder.ServiceCollection
builder.ServiceCollection.TryAdd(new ServiceCollection()
.AddSingleton<InMemoryValueGeneratorCache>()
.AddSingleton<InMemoryValueGeneratorSelector>()
.AddSingleton<SimpleValueGeneratorFactory<InMemoryValueGenerator>>()
Expand All @@ -28,7 +28,7 @@ public static EntityServicesBuilder AddInMemoryStore([NotNull] this EntityServic
.AddScoped<InMemoryDatabaseFacade>()
.AddScoped<InMemoryDataStore>()
.AddScoped<InMemoryConnection>()
.AddScoped<InMemoryDataStoreCreator>();
.AddScoped<InMemoryDataStoreCreator>());

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public static EntityServicesBuilder AddMigrations([NotNull] this EntityServicesB

builder
.AddRelational().ServiceCollection
.AddScoped<MigrationAssembly>()
.AddScoped<HistoryRepository>()
.AddScoped(MigrationsDataStoreServices.MigratorFactory);
.TryAdd(new ServiceCollection()
.AddScoped<MigrationAssembly>()
.AddScoped<HistoryRepository>()
.AddScoped(MigrationsDataStoreServices.MigratorFactory));

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static EntityServicesBuilder AddRedis([NotNull] this EntityServicesBuilde
{
Check.NotNull(builder, "builder");

builder.ServiceCollection
builder.ServiceCollection.TryAdd(new ServiceCollection()
.AddSingleton<RedisValueGeneratorSelector>()
.AddSingleton<RedisValueGeneratorCache>()
.AddSingleton<RedisValueGeneratorFactory>()
Expand All @@ -25,7 +25,7 @@ public static EntityServicesBuilder AddRedis([NotNull] this EntityServicesBuilde
.AddScoped<RedisDataStoreServices>()
.AddScoped<RedisConnection>()
.AddScoped<RedisDataStoreCreator>()
.AddScoped<RedisDatabase>();
.AddScoped<RedisDatabase>());

return builder;
}
Expand Down
10 changes: 10 additions & 0 deletions src/EntityFramework.Redis/RedisDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -21,6 +22,15 @@ public class RedisDataStore : DataStore
{
private readonly DbContextService<RedisDatabase> _database;

/// <summary>
/// This constructor is intended only for use when creating test doubles that will override members
/// with mocked or faked behavior. Use of this constructor for other purposes may result in unexpected
/// behavior including but not limited to throwing <see cref="NullReferenceException" />.
/// </summary>
protected RedisDataStore()
{
}

public RedisDataStore(
[NotNull] StateManager stateManager,
[NotNull] DbContextService<IModel> model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public static EntityServicesBuilder AddRelational([NotNull] this EntityServicesB
{
Check.NotNull(builder, "builder");

builder.ServiceCollection
builder.ServiceCollection.TryAdd(new ServiceCollection()
.AddSingleton<RelationalObjectArrayValueReaderFactory>()
.AddSingleton<RelationalTypedValueReaderFactory>()
.AddSingleton<ParameterNameGeneratorFactory>()
.AddSingleton<ModificationCommandComparer>()
.AddSingleton<GraphFactory, BidirectionalAdjacencyListGraphFactory>();
.AddSingleton<GraphFactory, BidirectionalAdjacencyListGraphFactory>());

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
using Microsoft.Data.Entity.Migrations.Infrastructure;
using Microsoft.Data.Entity.Relational;
using Microsoft.Data.Entity.Sqlite;
using Microsoft.Data.Entity.Sqlite.Utilities;
using Microsoft.Data.Entity.Sqlite.Metadata;
using Microsoft.Data.Entity.Sqlite.Migrations;
using Microsoft.Data.Entity.Sqlite.Utilities;
using Microsoft.Data.Entity.Storage;

// ReSharper disable once CheckNamespace
Expand All @@ -24,30 +24,31 @@ public static EntityServicesBuilder AddSqlite([NotNull] this EntityServicesBuild

builder
.AddMigrations().ServiceCollection
.AddSingleton<SqliteValueGeneratorCache>()
.AddSingleton<SqliteValueGeneratorSelector>()
.AddSingleton<SqliteSqlGenerator>()
.AddSingleton<SqlStatementExecutor>()
.AddSingleton<SqliteTypeMapper>()
.AddSingleton<SqliteModificationCommandBatchFactory>()
.AddSingleton<SqliteCommandBatchPreparer>()
.AddSingleton<SqliteMetadataExtensionProvider>()
.AddSingleton<SqliteMigrationOperationFactory>()
.AddScoped<SqliteBatchExecutor>()
.AddScoped<DataStoreSource, SqliteDataStoreSource>()
.AddScoped<SqliteDataStoreServices>()
.AddScoped<SqliteDataStore>()
.AddScoped<SqliteConnection>()
.AddScoped<SqliteMigrationOperationSqlGeneratorFactory>()
.AddScoped<SqliteDataStoreCreator>()
.AddScoped<SqliteMigrator>()
.AddScoped<SqliteDatabase>()
// TODO: Move to an AddMigrations extension method?
// Issue #556
.AddScoped<SqliteMigrationOperationProcessor>()
.AddScoped<SqliteModelDiffer>()
.AddScoped<MigrationAssembly>()
.AddScoped<HistoryRepository>();
.TryAdd(new ServiceCollection()
.AddSingleton<SqliteValueGeneratorCache>()
.AddSingleton<SqliteValueGeneratorSelector>()
.AddSingleton<SqliteSqlGenerator>()
.AddSingleton<SqlStatementExecutor>()
.AddSingleton<SqliteTypeMapper>()
.AddSingleton<SqliteModificationCommandBatchFactory>()
.AddSingleton<SqliteCommandBatchPreparer>()
.AddSingleton<SqliteMetadataExtensionProvider>()
.AddSingleton<SqliteMigrationOperationFactory>()
.AddScoped<SqliteBatchExecutor>()
.AddScoped<DataStoreSource, SqliteDataStoreSource>()
.AddScoped<SqliteDataStoreServices>()
.AddScoped<SqliteDataStore>()
.AddScoped<SqliteConnection>()
.AddScoped<SqliteMigrationOperationSqlGeneratorFactory>()
.AddScoped<SqliteDataStoreCreator>()
.AddScoped<SqliteMigrator>()
.AddScoped<SqliteDatabase>()
// TODO: Move to an AddMigrations extension method?
// Issue #556
.AddScoped<SqliteMigrationOperationProcessor>()
.AddScoped<SqliteModelDiffer>()
.AddScoped<MigrationAssembly>()
.AddScoped<HistoryRepository>());

return builder;
}
Expand Down
11 changes: 10 additions & 1 deletion src/EntityFramework.SQLite/SQLiteDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using JetBrains.Annotations;
using Microsoft.Data.Entity.ChangeTracking;
using Microsoft.Data.Entity.Infrastructure;
Expand All @@ -11,13 +12,21 @@
using Microsoft.Data.Entity.Relational.Query.Methods;
using Microsoft.Data.Entity.Sqlite.Query;
using Microsoft.Data.Entity.Sqlite.Utilities;
using Microsoft.Data.Entity.Utilities;
using Microsoft.Framework.Logging;

namespace Microsoft.Data.Entity.Sqlite
{
public class SqliteDataStore : RelationalDataStore
{
/// <summary>
/// This constructor is intended only for use when creating test doubles that will override members
/// with mocked or faked behavior. Use of this constructor for other purposes may result in unexpected
/// behavior including but not limited to throwing <see cref="NullReferenceException" />.
/// </summary>
protected SqliteDataStore()
{
}

public SqliteDataStore(
[NotNull] StateManager stateManager,
[NotNull] DbContextService<IModel> model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using JetBrains.Annotations;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Identity;
using Microsoft.Data.Entity.Migrations.Infrastructure;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Relational;
using Microsoft.Data.Entity.SqlServer;
using Microsoft.Data.Entity.SqlServer.Metadata;
Expand All @@ -22,31 +22,30 @@ public static EntityServicesBuilder AddSqlServer([NotNull] this EntityServicesBu
{
Check.NotNull(builder, "builder");

builder.AddRelational().ServiceCollection
.AddSingleton<SqlServerValueGeneratorCache>()
.AddSingleton<SqlServerValueGeneratorSelector>()
.AddSingleton<SimpleValueGeneratorFactory<SequentialGuidValueGenerator>>()
.AddSingleton<SqlServerSequenceValueGeneratorFactory>()
.AddSingleton<SqlServerSqlGenerator>()
.AddSingleton<SqlStatementExecutor>()
.AddSingleton<SqlServerTypeMapper>()
.AddSingleton<SqlServerModificationCommandBatchFactory>()
.AddSingleton<SqlServerCommandBatchPreparer>()
.AddSingleton<SqlServerMetadataExtensionProvider>()
.AddSingleton<SqlServerMigrationOperationFactory>()
.AddScoped<SqlServerBatchExecutor>()
.AddScoped<DataStoreSource, SqlServerDataStoreSource>()
.AddScoped<SqlServerDataStoreServices>()
.AddScoped<SqlServerDataStore>()
.AddScoped<SqlServerConnection>()
.AddScoped<SqlServerMigrationOperationProcessor>()
.AddScoped<SqlServerModelDiffer>()
.AddScoped<SqlServerDatabase>()
.AddScoped<SqlServerMigrationOperationSqlGeneratorFactory>()
.AddScoped<SqlServerDataStoreCreator>()
.AddScoped<MigrationAssembly>()
.AddScoped<HistoryRepository>()
.AddScoped<SqlServerMigrator>();
builder.AddMigrations().ServiceCollection
.TryAdd(new ServiceCollection()
.AddSingleton<SqlServerValueGeneratorCache>()
.AddSingleton<SqlServerValueGeneratorSelector>()
.AddSingleton<SimpleValueGeneratorFactory<SequentialGuidValueGenerator>>()
.AddSingleton<SqlServerSequenceValueGeneratorFactory>()
.AddSingleton<SqlServerSqlGenerator>()
.AddSingleton<SqlStatementExecutor>()
.AddSingleton<SqlServerTypeMapper>()
.AddSingleton<SqlServerModificationCommandBatchFactory>()
.AddSingleton<SqlServerCommandBatchPreparer>()
.AddSingleton<SqlServerMetadataExtensionProvider>()
.AddSingleton<SqlServerMigrationOperationFactory>()
.AddScoped<SqlServerBatchExecutor>()
.AddScoped<DataStoreSource, SqlServerDataStoreSource>()
.AddScoped<SqlServerDataStoreServices>()
.AddScoped<SqlServerDataStore>()
.AddScoped<SqlServerConnection>()
.AddScoped<SqlServerMigrationOperationProcessor>()
.AddScoped<SqlServerModelDiffer>()
.AddScoped<SqlServerDatabase>()
.AddScoped<SqlServerMigrationOperationSqlGeneratorFactory>()
.AddScoped<SqlServerDataStoreCreator>()
.AddScoped<SqlServerMigrator>());

return builder;
}
Expand Down
10 changes: 10 additions & 0 deletions src/EntityFramework.SqlServer/SqlServerDataStore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using JetBrains.Annotations;
using Microsoft.Data.Entity.ChangeTracking;
using Microsoft.Data.Entity.Infrastructure;
Expand All @@ -18,6 +19,15 @@ namespace Microsoft.Data.Entity.SqlServer
{
public class SqlServerDataStore : RelationalDataStore
{
/// <summary>
/// This constructor is intended only for use when creating test doubles that will override members
/// with mocked or faked behavior. Use of this constructor for other purposes may result in unexpected
/// behavior including but not limited to throwing <see cref="NullReferenceException" />.
/// </summary>
protected SqlServerDataStore()
{
}

public SqlServerDataStore(
[NotNull] StateManager stateManager,
[NotNull] DbContextService<IModel> model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.ChangeTracking;
Expand Down Expand Up @@ -31,7 +29,7 @@ public static EntityServicesBuilder AddEntityFramework(
{
Check.NotNull(serviceCollection, "serviceCollection");

serviceCollection
serviceCollection.TryAdd(new ServiceCollection()
.AddSingleton<IModelSource, DefaultModelSource>()
.AddSingleton<ModelBuilderFactory>()
.AddSingleton<SimpleValueGeneratorFactory<TemporaryValueGenerator>>()
Expand Down Expand Up @@ -77,45 +75,14 @@ public static EntityServicesBuilder AddEntityFramework(
.AddScoped(DataStoreServices.ConnectionFactory)
.AddScoped(DataStoreServices.DatabaseFactory)
.AddScoped(DataStoreServices.ValueGeneratorCacheFactory)
.AddScoped(DataStoreServices.DataStoreCreatorFactory);

EnsureLowLevelServices(serviceCollection);
.AddScoped(DataStoreServices.DataStoreCreatorFactory)
.AddSingleton<ILoggerFactory, LoggerFactory>()
.AddTypeActivator()
.AddOptions());

return new EntityServicesBuilder(serviceCollection, configuration);
}

private static void EnsureLowLevelServices(IServiceCollection serviceCollection)
{
var requiredServices = new List<Tuple<Type, Action<IServiceCollection>>>
{
Tuple.Create<Type, Action<IServiceCollection>>(typeof(ILoggerFactory), c => c.AddSingleton<ILoggerFactory, LoggerFactory>()),
Tuple.Create<Type, Action<IServiceCollection>>(typeof(ITypeActivator), c => c.AddTypeActivator()),
Tuple.Create<Type, Action<IServiceCollection>>(typeof(IOptions<>), c => c.AddOptions()),
};

foreach (var descriptor in serviceCollection)
{
foreach (var serviceTuple in requiredServices)
{
if (serviceTuple.Item1 == descriptor.ServiceType)
{
requiredServices.Remove(serviceTuple);
break;
}
}

if (!requiredServices.Any())
{
break;
}
}

foreach (var serviceTuple in requiredServices)
{
serviceTuple.Item2(serviceCollection);
}
}

public static EntityServicesBuilder AddDbContext<TContext>(
[NotNull] this EntityServicesBuilder builder,
[CanBeNull] Action<DbContextOptions> optionsAction = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Data.Entity.Storage;
using Microsoft.Data.Entity.Tests;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback;
using Xunit;

namespace Microsoft.Data.Entity.InMemory.Tests
Expand Down Expand Up @@ -33,5 +34,22 @@ public void Services_wire_up_correctly()
Assert.NotNull(scopedProvider.GetRequiredService<DataStoreSource>());
}
}

[Fact]
public void AddInMemoryStore_does_not_replace_services_already_registered()
{
var services = new ServiceCollection()
.AddSingleton<InMemoryDataStore, FakeInMemoryDataStore>();

services.AddEntityFramework().AddInMemoryStore();

var serviceProvider = services.BuildServiceProvider();

Assert.IsType<FakeInMemoryDataStore>(serviceProvider.GetRequiredService<InMemoryDataStore>());
}

private class FakeInMemoryDataStore : InMemoryDataStore
{
}
}
}
Loading