forked from masastack/MASA.Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ServiceCollectionExtensions
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...ASA.Utils.Extensions.DependencyInjection/MASA.Utils.Extensions.DependencyInjection.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
48 changes: 48 additions & 0 deletions
48
src/Extensions/MASA.Utils.Extensions.DependencyInjection/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |