diff --git a/Volo.Abp.sln b/Volo.Abp.sln index 46e3eab048..e2c6b120f7 100644 --- a/Volo.Abp.sln +++ b/Volo.Abp.sln @@ -12,12 +12,24 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Volo.Abp", "src\Volo.Abp\Volo.Abp.xproj", "{FC5F7372-EA60-4052-B943-0EE070221CC1}" EndProject -Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Volo.Abp.DependencyInjection", "src\Volo.Abp.DependencyInjection\Volo.Abp.DependencyInjection.xproj", "{F6F172F4-977B-4E20-BB9D-D4B083624011}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{447C8A77-E5F0-4538-8687-7383196D04EA}" EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Volo.Abp.Tests", "test\Volo.Abp.Tests\Volo.Abp.Tests.xproj", "{E9A85F1B-A5CB-4E19-B2BB-A7F97BB25EE1}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Volo", "Volo", "{9A4A646B-CC96-44FB-A717-E50C5C148B54}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Abp", "Abp", "{4C753F64-0C93-4D65-96C2-A40893AFC1E8}" +EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Volo.DependencyInjection", "src\Volo.DependencyInjection\Volo.DependencyInjection.xproj", "{F6F172F4-977B-4E20-BB9D-D4B083624011}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Volo", "Volo", "{82B41A0A-6068-410F-9C6B-2508CA763E21}" +EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Volo.DependencyInjection.Tests", "test\Volo.DependencyInjection.Tests\Volo.DependencyInjection.Tests.xproj", "{D68B762E-2A55-4A9F-9F2F-D4361B0925B0}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Abp", "Abp", "{37087D1B-3693-4E96-983D-A69F210BDE53}" +EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AbpTestBase", "test\AbpTestBase\AbpTestBase.xproj", "{1020F5FD-6A97-40C2-AFCA-EBDF641DF111}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,21 +40,35 @@ Global {FC5F7372-EA60-4052-B943-0EE070221CC1}.Debug|Any CPU.Build.0 = Debug|Any CPU {FC5F7372-EA60-4052-B943-0EE070221CC1}.Release|Any CPU.ActiveCfg = Release|Any CPU {FC5F7372-EA60-4052-B943-0EE070221CC1}.Release|Any CPU.Build.0 = Release|Any CPU - {F6F172F4-977B-4E20-BB9D-D4B083624011}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F6F172F4-977B-4E20-BB9D-D4B083624011}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F6F172F4-977B-4E20-BB9D-D4B083624011}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F6F172F4-977B-4E20-BB9D-D4B083624011}.Release|Any CPU.Build.0 = Release|Any CPU {E9A85F1B-A5CB-4E19-B2BB-A7F97BB25EE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E9A85F1B-A5CB-4E19-B2BB-A7F97BB25EE1}.Debug|Any CPU.Build.0 = Debug|Any CPU {E9A85F1B-A5CB-4E19-B2BB-A7F97BB25EE1}.Release|Any CPU.ActiveCfg = Release|Any CPU {E9A85F1B-A5CB-4E19-B2BB-A7F97BB25EE1}.Release|Any CPU.Build.0 = Release|Any CPU + {F6F172F4-977B-4E20-BB9D-D4B083624011}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F6F172F4-977B-4E20-BB9D-D4B083624011}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F6F172F4-977B-4E20-BB9D-D4B083624011}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F6F172F4-977B-4E20-BB9D-D4B083624011}.Release|Any CPU.Build.0 = Release|Any CPU + {D68B762E-2A55-4A9F-9F2F-D4361B0925B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D68B762E-2A55-4A9F-9F2F-D4361B0925B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D68B762E-2A55-4A9F-9F2F-D4361B0925B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D68B762E-2A55-4A9F-9F2F-D4361B0925B0}.Release|Any CPU.Build.0 = Release|Any CPU + {1020F5FD-6A97-40C2-AFCA-EBDF641DF111}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1020F5FD-6A97-40C2-AFCA-EBDF641DF111}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1020F5FD-6A97-40C2-AFCA-EBDF641DF111}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1020F5FD-6A97-40C2-AFCA-EBDF641DF111}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {FC5F7372-EA60-4052-B943-0EE070221CC1} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6} - {F6F172F4-977B-4E20-BB9D-D4B083624011} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6} - {E9A85F1B-A5CB-4E19-B2BB-A7F97BB25EE1} = {447C8A77-E5F0-4538-8687-7383196D04EA} + {FC5F7372-EA60-4052-B943-0EE070221CC1} = {4C753F64-0C93-4D65-96C2-A40893AFC1E8} + {E9A85F1B-A5CB-4E19-B2BB-A7F97BB25EE1} = {37087D1B-3693-4E96-983D-A69F210BDE53} + {9A4A646B-CC96-44FB-A717-E50C5C148B54} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6} + {4C753F64-0C93-4D65-96C2-A40893AFC1E8} = {5DF0E140-0513-4D0D-BE2E-3D4D85CD70E6} + {F6F172F4-977B-4E20-BB9D-D4B083624011} = {9A4A646B-CC96-44FB-A717-E50C5C148B54} + {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} EndGlobalSection EndGlobal diff --git a/src/Volo.Abp/project.json b/src/Volo.Abp/project.json index 03a4741f16..82eab9070c 100644 --- a/src/Volo.Abp/project.json +++ b/src/Volo.Abp/project.json @@ -3,7 +3,7 @@ "dependencies": { "NETStandard.Library": "1.6.1", - "Volo.Abp.DependencyInjection": "1.0.0-*" + "Volo.DependencyInjection": "1.0.0-*" }, "frameworks": { diff --git a/src/Volo.DependencyInjection/DependencyInjection/AbpConventionalDependencyInjectionExtensions.cs b/src/Volo.DependencyInjection/DependencyInjection/AbpConventionalDependencyInjectionExtensions.cs new file mode 100644 index 0000000000..e8ad85e02f --- /dev/null +++ b/src/Volo.DependencyInjection/DependencyInjection/AbpConventionalDependencyInjectionExtensions.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Microsoft.Extensions.DependencyInjection; +using Volo.Internal; + +namespace Volo.DependencyInjection +{ + public static class AbpConventionalDependencyInjectionExtensions + { + public static void AddAssembly(this IServiceCollection services, Assembly assembly) + { + services.AddTypes(AssemblyHelper.GetAllTypes(assembly).FilterInjectableTypes().ToArray()); + } + + public static void AddTypes(this IServiceCollection services, params Type[] types) + { + foreach (var type in types) + { + services.AddType(type); + } + } + + public static void AddType(this IServiceCollection services, Type type) + { + //TODO: Find exposed services for the type + + if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type)) + { + services.AddTransient(type); + } + + if (typeof(ISingletonDependency).GetTypeInfo().IsAssignableFrom(type)) + { + services.AddSingleton(type); + } + + //TODO: Register scoped + } + + private static IEnumerable FilterInjectableTypes(this IEnumerable types) + { + return types.Where(t => + { + var typeInfo = t.GetTypeInfo(); + return typeInfo.IsClass && !typeInfo.IsAbstract && !typeInfo.IsGenericType; + }); + } + } +} diff --git a/src/Volo.DependencyInjection/DependencyInjection/ISingletonDependency.cs b/src/Volo.DependencyInjection/DependencyInjection/ISingletonDependency.cs new file mode 100644 index 0000000000..81beddee4e --- /dev/null +++ b/src/Volo.DependencyInjection/DependencyInjection/ISingletonDependency.cs @@ -0,0 +1,6 @@ +namespace Volo.DependencyInjection +{ + public interface ISingletonDependency + { + } +} \ No newline at end of file diff --git a/src/Volo.DependencyInjection/DependencyInjection/ITransientDependency.cs b/src/Volo.DependencyInjection/DependencyInjection/ITransientDependency.cs new file mode 100644 index 0000000000..06d00d9de4 --- /dev/null +++ b/src/Volo.DependencyInjection/DependencyInjection/ITransientDependency.cs @@ -0,0 +1,7 @@ +namespace Volo.DependencyInjection +{ + public interface ITransientDependency + { + + } +} diff --git a/src/Volo.DependencyInjection/Internal/AssemblyHelper.cs b/src/Volo.DependencyInjection/Internal/AssemblyHelper.cs new file mode 100644 index 0000000000..bf82776566 --- /dev/null +++ b/src/Volo.DependencyInjection/Internal/AssemblyHelper.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace Volo.Internal +{ + internal static class AssemblyHelper + { + public static IReadOnlyList GetAllTypes(Assembly assembly) + { + try + { + return assembly.GetTypes(); + } + catch (ReflectionTypeLoadException ex) + { + return ex.Types; + } + } + } +} \ No newline at end of file diff --git a/src/Volo.Abp.DependencyInjection/Properties/AssemblyInfo.cs b/src/Volo.DependencyInjection/Properties/AssemblyInfo.cs similarity index 92% rename from src/Volo.Abp.DependencyInjection/Properties/AssemblyInfo.cs rename to src/Volo.DependencyInjection/Properties/AssemblyInfo.cs index 5e243abac0..7115e7edb8 100644 --- a/src/Volo.Abp.DependencyInjection/Properties/AssemblyInfo.cs +++ b/src/Volo.DependencyInjection/Properties/AssemblyInfo.cs @@ -7,7 +7,7 @@ using System.Runtime.InteropServices; // associated with an assembly. [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Volo.Abp.DependencyInjection")] +[assembly: AssemblyProduct("Volo.DependencyInjection")] [assembly: AssemblyTrademark("")] // Setting ComVisible to false makes the types in this assembly not visible diff --git a/src/Volo.Abp.DependencyInjection/Volo.Abp.DependencyInjection.xproj b/src/Volo.DependencyInjection/Volo.DependencyInjection.xproj similarity index 93% rename from src/Volo.Abp.DependencyInjection/Volo.Abp.DependencyInjection.xproj rename to src/Volo.DependencyInjection/Volo.DependencyInjection.xproj index dd0ba7421c..7bc2713b81 100644 --- a/src/Volo.Abp.DependencyInjection/Volo.Abp.DependencyInjection.xproj +++ b/src/Volo.DependencyInjection/Volo.DependencyInjection.xproj @@ -4,18 +4,16 @@ 14.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) - f6f172f4-977b-4e20-bb9d-d4b083624011 - Volo.Abp.DependencyInjection + Volo .\obj .\bin\ v4.6.1 - 2.0 - + \ No newline at end of file diff --git a/src/Volo.Abp.DependencyInjection/project.json b/src/Volo.DependencyInjection/project.json similarity index 100% rename from src/Volo.Abp.DependencyInjection/project.json rename to src/Volo.DependencyInjection/project.json diff --git a/test/AbpTestBase/AbpTestBase.xproj b/test/AbpTestBase/AbpTestBase.xproj new file mode 100644 index 0000000000..ba6cf8a59f --- /dev/null +++ b/test/AbpTestBase/AbpTestBase.xproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + 1020f5fd-6a97-40c2-afca-ebdf641df111 + AbpTestBase + .\obj + .\bin\ + v4.6.1 + + + + 2.0 + + + diff --git a/test/AbpTestBase/Properties/AssemblyInfo.cs b/test/AbpTestBase/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..8d75a28a9d --- /dev/null +++ b/test/AbpTestBase/Properties/AssemblyInfo.cs @@ -0,0 +1,19 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("AbpTestBase")] +[assembly: AssemblyTrademark("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("1020f5fd-6a97-40c2-afca-ebdf641df111")] diff --git a/test/AbpTestBase/project.json b/test/AbpTestBase/project.json new file mode 100644 index 0000000000..3f0d4a8175 --- /dev/null +++ b/test/AbpTestBase/project.json @@ -0,0 +1,23 @@ +{ + "version": "1.0.0-*", + + "dependencies": { + "NETStandard.Library": "1.6.1", + "dotnet-test-xunit": "2.2.0-preview2-build1029", + "NSubstitute": "2.0.0-rc", + "Shouldly": "2.8.2", + "xunit": "2.2.0-beta4-build3444", + "xunit.extensibility.execution": "2.2.0-beta4-build3444" + }, + + "frameworks": { + "netcoreapp1.0": { + "dependencies": { + "Microsoft.NETCore.App": { + "type": "platform", + "version": "1.0.0" + } + } + } + } +} diff --git a/test/Volo.Abp.Tests/Modularity/TestModule.cs b/test/Volo.Abp.Tests/Modularity/TestModule.cs new file mode 100644 index 0000000000..7e67909c44 --- /dev/null +++ b/test/Volo.Abp.Tests/Modularity/TestModule.cs @@ -0,0 +1,15 @@ +using System.Reflection; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; +using Volo.DependencyInjection; + +namespace Volo.Abp.Tests.Modularity +{ + public class TestModule : IAbpModule + { + public void ConfigureServices(IServiceCollection services) + { + services.AddAssembly(typeof(TestModule).GetTypeInfo().Assembly); + } + } +} diff --git a/test/Volo.DependencyInjection.Tests/AbpConventionalDependencyInjectionExtensions_Tests.cs b/test/Volo.DependencyInjection.Tests/AbpConventionalDependencyInjectionExtensions_Tests.cs new file mode 100644 index 0000000000..5c2f4c8b10 --- /dev/null +++ b/test/Volo.DependencyInjection.Tests/AbpConventionalDependencyInjectionExtensions_Tests.cs @@ -0,0 +1,70 @@ +using System; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Xunit; + +namespace Volo.DependencyInjection.Tests +{ + public class AbpConventionalDependencyInjectionExtensions_Tests + { + private readonly IServiceCollection _services; + + public AbpConventionalDependencyInjectionExtensions_Tests() + { + _services = new ServiceCollection(); + } + + [Fact] + public void Should_Register_Transient() + { + //Act + _services.AddType(typeof(MyTransientClass)); + + //Assert + ShouldContainTransient(_services, typeof(MyTransientClass)); + } + + [Fact] + public void Should_Register_Singleton() + { + //Act + _services.AddType(typeof(MySingletonClass)); + + //Assert + ShouldContainSingleton(_services, typeof(MySingletonClass)); + } + + private static void ShouldContainTransient(IServiceCollection services, Type type) + { + var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == type); + + serviceDescriptor.ImplementationType.ShouldBe(type); + serviceDescriptor.ShouldNotBeNull(); + serviceDescriptor.ImplementationFactory.ShouldBeNull(); + serviceDescriptor.ImplementationInstance.ShouldBeNull(); + serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Transient); + } + + private static void ShouldContainSingleton(IServiceCollection services, Type type) + { + var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == type); + + serviceDescriptor.ImplementationType.ShouldBe(type); + serviceDescriptor.ShouldNotBeNull(); + serviceDescriptor.ImplementationFactory.ShouldBeNull(); + serviceDescriptor.ImplementationInstance.ShouldBeNull(); + serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Singleton); + } + + public class MyTransientClass : ITransientDependency + { + + } + + public class MySingletonClass : ISingletonDependency + { + + } + } +} diff --git a/test/Volo.DependencyInjection.Tests/Properties/AssemblyInfo.cs b/test/Volo.DependencyInjection.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..9325f9cb60 --- /dev/null +++ b/test/Volo.DependencyInjection.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,19 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Volo.DependencyInjection.Tests")] +[assembly: AssemblyTrademark("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d68b762e-2a55-4a9f-9f2f-d4361b0925b0")] diff --git a/test/Volo.DependencyInjection.Tests/Volo.DependencyInjection.Tests.xproj b/test/Volo.DependencyInjection.Tests/Volo.DependencyInjection.Tests.xproj new file mode 100644 index 0000000000..5fc164aa1e --- /dev/null +++ b/test/Volo.DependencyInjection.Tests/Volo.DependencyInjection.Tests.xproj @@ -0,0 +1,22 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + d68b762e-2a55-4a9f-9f2f-d4361b0925b0 + Volo.DependencyInjection.Tests + .\obj + .\bin\ + v4.6.1 + + + 2.0 + + + + + + \ No newline at end of file diff --git a/test/Volo.DependencyInjection.Tests/project.json b/test/Volo.DependencyInjection.Tests/project.json new file mode 100644 index 0000000000..9d7ee2932e --- /dev/null +++ b/test/Volo.DependencyInjection.Tests/project.json @@ -0,0 +1,22 @@ +{ + "version": "1.0.0-*", + + "testRunner": "xunit", + + "dependencies": { + "AbpTestBase": "1.0.0-*", + "NETStandard.Library": "1.6.1", + "Volo.DependencyInjection": "1.0.0-*" + }, + + "frameworks": { + "netcoreapp1.0": { + "dependencies": { + "Microsoft.NETCore.App": { + "type": "platform", + "version": "1.0.0" + } + } + } + } +}