|
|
@ -1,4 +1,5 @@ |
|
|
using Shouldly; |
|
|
using System.Linq; |
|
|
|
|
|
using Shouldly; |
|
|
using Volo.DependencyInjection; |
|
|
using Volo.DependencyInjection; |
|
|
using Xunit; |
|
|
using Xunit; |
|
|
|
|
|
|
|
|
@ -43,16 +44,92 @@ namespace Microsoft.Extensions.DependencyInjection |
|
|
_services.ShouldContainScoped(typeof(MyScopedClass)); |
|
|
_services.ShouldContainScoped(typeof(MyScopedClass)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void Should_Register_Scoped_With_Dependency_Attribute() |
|
|
|
|
|
{ |
|
|
|
|
|
//Act
|
|
|
|
|
|
_services.AddType(typeof(MyScopedClassWithDependencyAttribute)); |
|
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
|
_services.ShouldContainScoped(typeof(MyScopedClassWithDependencyAttribute)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void Dependency_Attribute_Should_Override_Interface_Lifetimes() |
|
|
|
|
|
{ |
|
|
|
|
|
//Act
|
|
|
|
|
|
_services.AddType(typeof(MyScopedClassWithDependencyAttribute2)); |
|
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
|
_services.ShouldContainScoped(typeof(MyScopedClassWithDependencyAttribute2)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Fact] |
|
|
public void Should_Register_For_Exposed_Services() |
|
|
public void Should_Register_For_Exposed_Services() |
|
|
{ |
|
|
{ |
|
|
|
|
|
//Act
|
|
|
_services.AddType(typeof(MyServiceWithExposeList)); |
|
|
_services.AddType(typeof(MyServiceWithExposeList)); |
|
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
_services.ShouldContain(typeof(IMyService1), typeof(MyServiceWithExposeList), ServiceLifetime.Transient); |
|
|
_services.ShouldContain(typeof(IMyService1), typeof(MyServiceWithExposeList), ServiceLifetime.Transient); |
|
|
_services.ShouldContain(typeof(IMyService2), typeof(MyServiceWithExposeList), ServiceLifetime.Transient); |
|
|
_services.ShouldContain(typeof(IMyService2), typeof(MyServiceWithExposeList), ServiceLifetime.Transient); |
|
|
_services.ShouldNotContain(typeof(MyServiceWithExposeList)); |
|
|
_services.ShouldNotContain(typeof(MyServiceWithExposeList)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void Should_Register_Multiple_Implementation_For_Same_Service() |
|
|
|
|
|
{ |
|
|
|
|
|
//Act
|
|
|
|
|
|
|
|
|
|
|
|
_services.AddTypes(typeof(FirstImplOfMyService), typeof(SecondImplOfMyService)); |
|
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
|
|
|
|
|
|
|
//Check descriptons in service collection
|
|
|
|
|
|
var descriptions = _services.Where(s => s.ServiceType == typeof(IMyService)).ToList(); |
|
|
|
|
|
descriptions.Count.ShouldBe(2); |
|
|
|
|
|
descriptions[0].ImplementationType.ShouldBe(typeof(FirstImplOfMyService)); |
|
|
|
|
|
descriptions[1].ImplementationType.ShouldBe(typeof(SecondImplOfMyService)); |
|
|
|
|
|
|
|
|
|
|
|
//Check from service provider
|
|
|
|
|
|
var serviceProvider = _services.BuildServiceProvider(); |
|
|
|
|
|
|
|
|
|
|
|
//Default service should be second one
|
|
|
|
|
|
serviceProvider.GetRequiredService<IMyService>().ShouldBeOfType(typeof(SecondImplOfMyService)); |
|
|
|
|
|
|
|
|
|
|
|
//Should also get all services
|
|
|
|
|
|
var instances = serviceProvider.GetServices<IMyService>().ToList(); |
|
|
|
|
|
instances.Count.ShouldBe(2); |
|
|
|
|
|
instances[0].ShouldBeOfType(typeof(FirstImplOfMyService)); |
|
|
|
|
|
instances[1].ShouldBeOfType(typeof(SecondImplOfMyService)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void Should_Not_Register_Second_Implementation_For_Same_Service_If_Second_Is_Marked_As_TryRegister() |
|
|
|
|
|
{ |
|
|
|
|
|
//Act
|
|
|
|
|
|
|
|
|
|
|
|
_services.AddTypes(typeof(FirstImplOfMyService), typeof(TryRegisterImplOfMyService)); |
|
|
|
|
|
|
|
|
|
|
|
//Assert
|
|
|
|
|
|
|
|
|
|
|
|
//Check descriptons in service collection
|
|
|
|
|
|
var descriptions = _services.Where(s => s.ServiceType == typeof(IMyService)).ToList(); |
|
|
|
|
|
descriptions.Count.ShouldBe(1); |
|
|
|
|
|
descriptions[0].ImplementationType.ShouldBe(typeof(FirstImplOfMyService)); |
|
|
|
|
|
|
|
|
|
|
|
//Check from service provider
|
|
|
|
|
|
var serviceProvider = _services.BuildServiceProvider(); |
|
|
|
|
|
|
|
|
|
|
|
//Default service should be second one
|
|
|
|
|
|
serviceProvider.GetRequiredService<IMyService>().ShouldBeOfType(typeof(FirstImplOfMyService)); |
|
|
|
|
|
|
|
|
|
|
|
//Should also get all services
|
|
|
|
|
|
var instances = serviceProvider.GetServices<IMyService>().ToList(); |
|
|
|
|
|
instances.Count.ShouldBe(1); |
|
|
|
|
|
instances[0].ShouldBeOfType(typeof(FirstImplOfMyService)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Fact] |
|
|
public void AddObjectAccessor_Test() |
|
|
public void AddObjectAccessor_Test() |
|
|
{ |
|
|
{ |
|
|
@ -90,6 +167,18 @@ namespace Microsoft.Extensions.DependencyInjection |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Dependency(ServiceLifetime.Scoped)] |
|
|
|
|
|
public class MyScopedClassWithDependencyAttribute |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Dependency(ServiceLifetime.Scoped)] //Attribute overrides interface
|
|
|
|
|
|
public class MyScopedClassWithDependencyAttribute2 : ITransientDependency |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public interface IMyService1 |
|
|
public interface IMyService1 |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
@ -99,13 +188,31 @@ namespace Microsoft.Extensions.DependencyInjection |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[ExposeServices(typeof(IMyService1), typeof(IMyService2))] |
|
|
[ExposeServices(typeof(IMyService1), typeof(IMyService2))] |
|
|
public class MyServiceWithExposeList : IMyService1, IMyService2, ITransientDependency |
|
|
public class MyServiceWithExposeList : IMyService1, IMyService2, ITransientDependency |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public interface IMyService : ITransientDependency |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public class FirstImplOfMyService : IMyService |
|
|
|
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public class SecondImplOfMyService : IMyService |
|
|
|
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Dependency(TryRegister = true)] |
|
|
|
|
|
public class TryRegisterImplOfMyService : IMyService |
|
|
|
|
|
{ |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public class MyEmptyClass |
|
|
public class MyEmptyClass |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
|