From 3d82e6daeb6a9e247fb040a8154374240f296733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sun, 4 Dec 2016 18:25:01 +0300 Subject: [PATCH] Added scoped dependency. --- Volo.Abp.sln | 2 +- ...ConventionalDependencyInjectionExtensions.cs | 5 ++++- .../DependencyInjection/IScopedDependency.cs | 6 ++++++ ...tionalDependencyInjectionExtensions_Tests.cs | 17 ++++++++++++++++- .../ServiceCollectionShouldlyExtensions.cs | 13 +++++++++++-- 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 src/Volo.DependencyInjection/DependencyInjection/IScopedDependency.cs diff --git a/Volo.Abp.sln b/Volo.Abp.sln index 81d733760c..a2731b0256 100644 --- a/Volo.Abp.sln +++ b/Volo.Abp.sln @@ -101,7 +101,7 @@ Global {82B41A0A-6068-410F-9C6B-2508CA763E21} = {447C8A77-E5F0-4538-8687-7383196D04EA} {D68B762E-2A55-4A9F-9F2F-D4361B0925B0} = {82B41A0A-6068-410F-9C6B-2508CA763E21} {37087D1B-3693-4E96-983D-A69F210BDE53} = {447C8A77-E5F0-4538-8687-7383196D04EA} - {1020F5FD-6A97-40C2-AFCA-EBDF641DF111} = {82B41A0A-6068-410F-9C6B-2508CA763E21} + {1020F5FD-6A97-40C2-AFCA-EBDF641DF111} = {447C8A77-E5F0-4538-8687-7383196D04EA} {02BE03BA-3411-448C-AB61-CB36407CC49A} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} {B1D860BB-6EC6-4BAE-ADAA-C2AEC2FFB510} = {37087D1B-3693-4E96-983D-A69F210BDE53} {A3A3B258-B3D5-4FDE-9D84-CAA8CBB70586} = {447C8A77-E5F0-4538-8687-7383196D04EA} diff --git a/src/Volo.DependencyInjection/DependencyInjection/AbpConventionalDependencyInjectionExtensions.cs b/src/Volo.DependencyInjection/DependencyInjection/AbpConventionalDependencyInjectionExtensions.cs index 02034d2e8c..842bc53cb2 100644 --- a/src/Volo.DependencyInjection/DependencyInjection/AbpConventionalDependencyInjectionExtensions.cs +++ b/src/Volo.DependencyInjection/DependencyInjection/AbpConventionalDependencyInjectionExtensions.cs @@ -43,7 +43,10 @@ namespace Volo.DependencyInjection services.AddSingleton(type); } - //TODO: Register scoped + if (typeof(IScopedDependency).GetTypeInfo().IsAssignableFrom(type)) + { + services.AddScoped(type); + } } private static IEnumerable FilterInjectableTypes(this IEnumerable types) diff --git a/src/Volo.DependencyInjection/DependencyInjection/IScopedDependency.cs b/src/Volo.DependencyInjection/DependencyInjection/IScopedDependency.cs new file mode 100644 index 0000000000..f3764f2ccd --- /dev/null +++ b/src/Volo.DependencyInjection/DependencyInjection/IScopedDependency.cs @@ -0,0 +1,6 @@ +namespace Volo.DependencyInjection +{ + public interface IScopedDependency + { + } +} \ No newline at end of file diff --git a/test/Volo.DependencyInjection.Tests/AbpConventionalDependencyInjectionExtensions_Tests.cs b/test/Volo.DependencyInjection.Tests/AbpConventionalDependencyInjectionExtensions_Tests.cs index 9df939c484..4d192ef531 100644 --- a/test/Volo.DependencyInjection.Tests/AbpConventionalDependencyInjectionExtensions_Tests.cs +++ b/test/Volo.DependencyInjection.Tests/AbpConventionalDependencyInjectionExtensions_Tests.cs @@ -31,7 +31,17 @@ namespace Volo.DependencyInjection.Tests //Assert _services.ShouldContainSingleton(typeof(MySingletonClass)); } - + + [Fact] + public void Should_Register_Scoped() + { + //Act + _services.AddType(typeof(MyScopedClass)); + + //Assert + _services.ShouldContainScoped(typeof(MyScopedClass)); + } + public class MyTransientClass : ITransientDependency { @@ -41,5 +51,10 @@ namespace Volo.DependencyInjection.Tests { } + + public class MyScopedClass : IScopedDependency + { + + } } } diff --git a/test/Volo.DependencyInjection.Tests/ServiceCollectionShouldlyExtensions.cs b/test/Volo.DependencyInjection.Tests/ServiceCollectionShouldlyExtensions.cs index 5a5f7bda93..9761102b35 100644 --- a/test/Volo.DependencyInjection.Tests/ServiceCollectionShouldlyExtensions.cs +++ b/test/Volo.DependencyInjection.Tests/ServiceCollectionShouldlyExtensions.cs @@ -13,7 +13,6 @@ namespace Volo.DependencyInjection.Tests serviceDescriptor.ShouldNotBeNull(); serviceDescriptor.ImplementationType.ShouldBe(type); - serviceDescriptor.ShouldNotBeNull(); serviceDescriptor.ImplementationFactory.ShouldBeNull(); serviceDescriptor.ImplementationInstance.ShouldBeNull(); serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Transient); @@ -25,10 +24,20 @@ namespace Volo.DependencyInjection.Tests serviceDescriptor.ShouldNotBeNull(); serviceDescriptor.ImplementationType.ShouldBe(type); - serviceDescriptor.ShouldNotBeNull(); serviceDescriptor.ImplementationFactory.ShouldBeNull(); serviceDescriptor.ImplementationInstance.ShouldBeNull(); serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Singleton); } + + public static void ShouldContainScoped(this IServiceCollection services, Type type) + { + var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == type); + + serviceDescriptor.ShouldNotBeNull(); + serviceDescriptor.ImplementationType.ShouldBe(type); + serviceDescriptor.ImplementationFactory.ShouldBeNull(); + serviceDescriptor.ImplementationInstance.ShouldBeNull(); + serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Scoped); + } } } \ No newline at end of file