-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add extension method to ease registering classes in bootstrap installers
- Loading branch information
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/Cortside.Common.BootStrap/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,22 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Cortside.Common.BootStrap { | ||
public static class ServiceCollectionExtensions { | ||
public static IServiceCollection RegisterClassesWithSuffix<T>(this IServiceCollection services, string suffix) where T : class { | ||
typeof(T).GetTypeInfo().Assembly.GetTypes() | ||
.Where(x => (x.Name.EndsWith(suffix, StringComparison.InvariantCulture)) | ||
&& x.GetTypeInfo().IsClass | ||
&& !x.GetTypeInfo().IsAbstract | ||
&& x.GetInterfaces().Length > 0) | ||
.ToList().ForEach(x => { | ||
x.GetInterfaces().ToList() | ||
.ForEach(i => services.AddScoped(i, x)); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
} |