mirror of https://github.com/abpframework/abp.git
17 changed files with 335 additions and 15 deletions
@ -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; |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
namespace Volo.DependencyInjection |
||||
|
{ |
||||
|
public interface ISingletonDependency |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
namespace Volo.DependencyInjection |
||||
|
{ |
||||
|
public interface ITransientDependency |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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> |
||||
@ -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")] |
||||
@ -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" |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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 |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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")] |
||||
@ -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> |
||||
@ -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…
Reference in new issue