Browse Source

Created simple DI extensions.

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent
commit
525f40970f
  1. 44
      Volo.Abp.sln
  2. 2
      src/Volo.Abp/project.json
  3. 51
      src/Volo.DependencyInjection/DependencyInjection/AbpConventionalDependencyInjectionExtensions.cs
  4. 6
      src/Volo.DependencyInjection/DependencyInjection/ISingletonDependency.cs
  5. 7
      src/Volo.DependencyInjection/DependencyInjection/ITransientDependency.cs
  6. 21
      src/Volo.DependencyInjection/Internal/AssemblyHelper.cs
  7. 2
      src/Volo.DependencyInjection/Properties/AssemblyInfo.cs
  8. 6
      src/Volo.DependencyInjection/Volo.DependencyInjection.xproj
  9. 0
      src/Volo.DependencyInjection/project.json
  10. 21
      test/AbpTestBase/AbpTestBase.xproj
  11. 19
      test/AbpTestBase/Properties/AssemblyInfo.cs
  12. 23
      test/AbpTestBase/project.json
  13. 15
      test/Volo.Abp.Tests/Modularity/TestModule.cs
  14. 70
      test/Volo.DependencyInjection.Tests/AbpConventionalDependencyInjectionExtensions_Tests.cs
  15. 19
      test/Volo.DependencyInjection.Tests/Properties/AssemblyInfo.cs
  16. 22
      test/Volo.DependencyInjection.Tests/Volo.DependencyInjection.Tests.xproj
  17. 22
      test/Volo.DependencyInjection.Tests/project.json

44
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

2
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": {

51
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<Type> FilterInjectableTypes(this IEnumerable<Type> types)
{
return types.Where(t =>
{
var typeInfo = t.GetTypeInfo();
return typeInfo.IsClass && !typeInfo.IsAbstract && !typeInfo.IsGenericType;
});
}
}
}

6
src/Volo.DependencyInjection/DependencyInjection/ISingletonDependency.cs

@ -0,0 +1,6 @@
namespace Volo.DependencyInjection
{
public interface ISingletonDependency
{
}
}

7
src/Volo.DependencyInjection/DependencyInjection/ITransientDependency.cs

@ -0,0 +1,7 @@
namespace Volo.DependencyInjection
{
public interface ITransientDependency
{
}
}

21
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<Type> GetAllTypes(Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
return ex.Types;
}
}
}
}

2
src/Volo.Abp.DependencyInjection/Properties/AssemblyInfo.cs → 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

6
src/Volo.Abp.DependencyInjection/Volo.Abp.DependencyInjection.xproj → src/Volo.DependencyInjection/Volo.DependencyInjection.xproj

@ -4,18 +4,16 @@
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>f6f172f4-977b-4e20-bb9d-d4b083624011</ProjectGuid>
<RootNamespace>Volo.Abp.DependencyInjection</RootNamespace>
<RootNamespace>Volo</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
</Project>

0
src/Volo.Abp.DependencyInjection/project.json → src/Volo.DependencyInjection/project.json

21
test/AbpTestBase/AbpTestBase.xproj

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>1020f5fd-6a97-40c2-afca-ebdf641df111</ProjectGuid>
<RootNamespace>AbpTestBase</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

19
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")]

23
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"
}
}
}
}
}

15
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);
}
}
}

70
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
{
}
}
}

19
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")]

22
test/Volo.DependencyInjection.Tests/Volo.DependencyInjection.Tests.xproj

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>d68b762e-2a55-4a9f-9f2f-d4361b0925b0</ProjectGuid>
<RootNamespace>Volo.DependencyInjection.Tests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

22
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"
}
}
}
}
}
Loading…
Cancel
Save