Skip to content

Commit

Permalink
add extension method to ease registering classes in bootstrap installers
Browse files Browse the repository at this point in the history
  • Loading branch information
cYCL157 committed Apr 23, 2024
1 parent e0a1ef2 commit de06447
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Cortside.Common.BootStrap/ServiceCollectionExtensions.cs
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;
}
}
}

0 comments on commit de06447

Please sign in to comment.