Skip to content

Commit

Permalink
feat: add ServiceCollectionExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
doddgu committed Sep 8, 2021
1 parent 2c30c3b commit 4ab9db8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions MASA.Utils.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{4F908878-0
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Data.DataAnnotations.Tests", "test\MASA.Utils.Data.DataAnnotations.Tests\MASA.Utils.Data.DataAnnotations.Tests.csproj", "{1D6302D8-DA31-4782-A076-AC586A286253}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MASA.Utils.Extensions.DependencyInjection", "src\Extensions\MASA.Utils.Extensions.DependencyInjection\MASA.Utils.Extensions.DependencyInjection.csproj", "{00E275C2-3B6A-4F48-AE0B-738AA669226D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -65,6 +67,10 @@ Global
{1D6302D8-DA31-4782-A076-AC586A286253}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D6302D8-DA31-4782-A076-AC586A286253}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D6302D8-DA31-4782-A076-AC586A286253}.Release|Any CPU.Build.0 = Release|Any CPU
{00E275C2-3B6A-4F48-AE0B-738AA669226D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{00E275C2-3B6A-4F48-AE0B-738AA669226D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00E275C2-3B6A-4F48-AE0B-738AA669226D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00E275C2-3B6A-4F48-AE0B-738AA669226D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -81,6 +87,7 @@ Global
{F844C2A1-C36D-400E-A0D8-7658EF9C3B93} = {D956582F-4071-47E6-A8E7-4C5A83770045}
{98E5FEB6-71FA-43A5-9411-BE7C95B59AE5} = {F844C2A1-C36D-400E-A0D8-7658EF9C3B93}
{1D6302D8-DA31-4782-A076-AC586A286253} = {4F908878-0EB8-43E4-96E4-8B1F32E9B635}
{00E275C2-3B6A-4F48-AE0B-738AA669226D} = {B2DA607D-4A39-4F0C-A9B3-DD9A061B0B4E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D7DAA0E6-098F-4B18-8775-64FDA96F1FF0}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0-preview.7.21377.19" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Reflection;

namespace Microsoft.Extensions.DependencyInjection;
public static class ServiceCollectionExtensions
{
/// <summary>
/// Auto add all service to IoC, lifecycle is scoped
/// </summary>
/// <param name="services"></param>
/// <param name="suffix">default is Service</param>
public static IServiceCollection AddServices(this IServiceCollection services, string suffix = "Service", bool autoFire = false)
{
Assembly
.GetCallingAssembly()
.GetTypes()
.Where(t => t.Name.EndsWith(suffix))
.ToList()
.ForEach(serviceType => AddScoped(services, serviceType, autoFire));

return services;
}

/// <summary>
/// Auto add all service to IoC, lifecycle is scoped
/// </summary>
/// <param name="services"></param>
/// <param name="suffix">default is Service</param>
public static IServiceCollection AddServices<TService>(this IServiceCollection services, bool autoFire = false)
{
var serviceType = typeof(TService);

Assembly
.GetCallingAssembly()
.GetTypes()
.Where(t => t.BaseType == serviceType)
.ToList()
.ForEach(serviceType => AddScoped(services, serviceType, autoFire));

return services;
}

private static void AddScoped(IServiceCollection services, Type serviceType, bool autoFire)
{
services.AddScoped(serviceType);

if (autoFire) services.BuildServiceProvider().GetService(serviceType);
}
}

0 comments on commit 4ab9db8

Please sign in to comment.