-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from seesharper/support-overriding-services-v…
…ia-servicecollection Allow services to be overridden using ServiceCollection
- Loading branch information
Showing
3 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
src/LightInject.Microsoft.DependencyInjection.Tests/DefaultServiceTests.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,98 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace LightInject.Microsoft.DependencyInjection.Tests; | ||
|
||
public class DefaultServiceTests | ||
{ | ||
[Fact] | ||
public void ShouldOverrideDefaultRegistrationInServiceContainer() | ||
{ | ||
var container = new ServiceContainer(options => options.WithMicrosoftSettings()); | ||
container.RegisterTransient<IFoo, Foo>(); | ||
var serviceCollection = new ServiceCollection(); | ||
|
||
serviceCollection.AddSingleton<IFoo, AnotherFoo>(); | ||
|
||
var provider = container.CreateServiceProvider(serviceCollection); | ||
|
||
var foo = provider.GetRequiredService<IFoo>(); | ||
|
||
Assert.IsType<AnotherFoo>(foo); | ||
} | ||
|
||
[Fact] | ||
public void ShouldOverrideNamedRegistrationInServiceContainer() | ||
{ | ||
var container = new ServiceContainer(options => options.WithMicrosoftSettings()); | ||
container.RegisterTransient<IFoo, Foo>("Foo"); | ||
var serviceCollection = new ServiceCollection(); | ||
|
||
serviceCollection.AddSingleton<IFoo, AnotherFoo>(); | ||
|
||
var provider = container.CreateServiceProvider(serviceCollection); | ||
|
||
var foo = provider.GetRequiredService<IFoo>(); | ||
|
||
Assert.IsType<AnotherFoo>(foo); | ||
} | ||
|
||
[Fact] | ||
public void ShouldOverrideMultipleNamedRegistrationInServiceContainer() | ||
{ | ||
var container = new ServiceContainer(options => options.WithMicrosoftSettings()); | ||
container.RegisterTransient<IFoo, Foo>("Foo1"); | ||
container.RegisterTransient<IFoo, Foo>("Foo2"); | ||
var serviceCollection = new ServiceCollection(); | ||
|
||
serviceCollection.AddSingleton<IFoo, AnotherFoo>(); | ||
|
||
var provider = container.CreateServiceProvider(serviceCollection); | ||
|
||
var foo = provider.GetRequiredService<IFoo>(); | ||
|
||
Assert.IsType<AnotherFoo>(foo); | ||
} | ||
|
||
[Fact] | ||
public void ShouldOverrideNamedAndDefaultRegistrationInServiceContainer() | ||
{ | ||
var container = new ServiceContainer(options => options.WithMicrosoftSettings()); | ||
container.RegisterTransient<IFoo, Foo>(); | ||
container.RegisterTransient<IFoo, Foo>("Foo"); | ||
var serviceCollection = new ServiceCollection(); | ||
|
||
serviceCollection.AddSingleton<IFoo, AnotherFoo>(); | ||
|
||
var provider = container.CreateServiceProvider(serviceCollection); | ||
|
||
var foo = provider.GetRequiredService<IFoo>(); | ||
|
||
Assert.IsType<AnotherFoo>(foo); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ShouldUseLast() | ||
{ | ||
var container = new ServiceContainer(options => options.WithMicrosoftSettings()); | ||
container.RegisterTransient<IFoo, Foo>("Foo1"); | ||
container.RegisterTransient<IFoo, Foo>("Foo2"); | ||
var serviceCollection = new ServiceCollection(); | ||
|
||
serviceCollection.AddSingleton<IFoo, AnotherFoo>(); | ||
|
||
var provider = container.CreateServiceProvider(serviceCollection); | ||
|
||
var foo = provider.GetRequiredService<IFoo>(); | ||
|
||
Assert.IsType<AnotherFoo>(foo); | ||
} | ||
|
||
public interface IFoo { } | ||
|
||
public class Foo : IFoo { } | ||
|
||
public class AnotherFoo : IFoo { } | ||
} |
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