-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use TryAdd in AddXxx methods to only add services if not already pres…
…ent. Issue #956 and also see pull request #1129. This change updates all of our AddXxx methods to use TryAdd when adding to the service collection such that only services that are not already registered will be registered. This also means that the code we had to check the service collection for "hosting" services is no longer needed. Note that services for which it is expected that multiple instances can be registered (e.g. listeners and data sources) must not use TryAdd since otherwise only the first registered will be used.
- Loading branch information
1 parent
134d72e
commit a365c55
Showing
16 changed files
with
252 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/EntityFramework.Migrations.Tests/MigrationsEntityServicesBuilderExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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 Microsoft.Data.Entity.Migrations.Infrastructure; | ||
using Microsoft.Framework.DependencyInjection; | ||
using Microsoft.Framework.DependencyInjection.Fallback; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.Entity.Migrations.Tests | ||
{ | ||
public class MigrationsEntityServicesBuilderExtensionsTest | ||
{ | ||
[Fact] | ||
public void AddMigrations_does_not_replace_services_already_registered() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddSingleton<MigrationAssembly, FakeMigrationAssembly>(); | ||
|
||
services.AddEntityFramework().AddMigrations(); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
Assert.IsType<FakeMigrationAssembly>(serviceProvider.GetRequiredService<MigrationAssembly>()); | ||
} | ||
|
||
private class FakeMigrationAssembly : MigrationAssembly | ||
{ | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
test/EntityFramework.Redis.Tests/RedisEntityServicesBuilderExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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 Microsoft.Framework.DependencyInjection; | ||
using Microsoft.Framework.DependencyInjection.Fallback; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.Entity.Redis.Tests | ||
{ | ||
public class RedisEntityServicesBuilderExtensionsTest | ||
{ | ||
[Fact] | ||
public void AddRedis_does_not_replace_services_already_registered() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddSingleton<RedisDataStore, FakeRedisDataStore>(); | ||
|
||
services.AddEntityFramework().AddRedis(); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
Assert.IsType<FakeRedisDataStore>(serviceProvider.GetRequiredService<RedisDataStore>()); | ||
} | ||
|
||
private class FakeRedisDataStore : RedisDataStore | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test/EntityFramework.Relational.Tests/RelationalEntityServicesBuilderExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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 Microsoft.Data.Entity.Relational.Update; | ||
using Microsoft.Framework.DependencyInjection; | ||
using Microsoft.Framework.DependencyInjection.Fallback; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.Entity.Relational.Tests | ||
{ | ||
public class RelationalEntityServicesBuilderExtensionsTest | ||
{ | ||
[Fact] | ||
public void AddRelational_does_not_replace_services_already_registered() | ||
{ | ||
var services = new ServiceCollection() | ||
.AddSingleton<ModificationCommandComparer, FakeModificationCommandComparer>(); | ||
|
||
services.AddEntityFramework().AddRelational(); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
Assert.IsType<FakeModificationCommandComparer>(serviceProvider.GetRequiredService<ModificationCommandComparer>()); | ||
} | ||
|
||
private class FakeModificationCommandComparer : ModificationCommandComparer | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.