|
| 1 | +// Copyright (c) 2018 Inventory Innovations, Inc. - build by Jon P Smith (GitHub JonPSmith) |
| 2 | +// Licensed under MIT licence. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using Microsoft.Extensions.DependencyInjection; |
| 8 | + |
| 9 | +namespace NetCore.AutoRegisterDi |
| 10 | +{ |
| 11 | + public static class AutoRegisterHelpers |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// This finds all the public, non-generic, non-nested classes in an assembly |
| 15 | + /// in the provided assemblies |
| 16 | + /// </summary> |
| 17 | + /// <param name="services">the NET Core dependency injection service</param> |
| 18 | + /// <param name="assemblies">Each assembly you want scanned </param> |
| 19 | + /// <returns></returns> |
| 20 | + public static AutoRegisterData RegisterAssemblyPublicNonGenericClasses(this IServiceCollection services, params Assembly[] assemblies) |
| 21 | + { |
| 22 | + var allPublicTypes = assemblies.SelectMany(x => x.GetExportedTypes() |
| 23 | + .Where(y => y.IsClass && !y.IsAbstract && !y.IsGenericType && !y.IsNested)); |
| 24 | + return new AutoRegisterData(services, allPublicTypes); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// This allows you to filter the classes in some way. |
| 29 | + /// For instance <code>Where(c =\> c.Name.EndsWith("Service")</code> would only register classes who's name ended in "Service" |
| 30 | + /// </summary> |
| 31 | + /// <param name="autoRegData"></param> |
| 32 | + /// <param name="predicate">A function that will take a type and return true if that type should be included</param> |
| 33 | + /// <returns></returns> |
| 34 | + public static AutoRegisterData Where(this AutoRegisterData autoRegData, Func<Type, bool> predicate) |
| 35 | + { |
| 36 | + if (autoRegData == null) throw new ArgumentNullException(nameof(autoRegData)); |
| 37 | + autoRegData.TypeFilter = predicate; |
| 38 | + return new AutoRegisterData(autoRegData.Services, autoRegData.TypesToConsider.Where(predicate)); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// This registers the classes against any public interfaces (other than IDisposable) implemented by the class |
| 43 | + /// </summary> |
| 44 | + /// <param name="autoRegData">AutoRegister data produced by <see cref="RegisterAssemblyPublicNonGenericClasses"/></param> method |
| 45 | + /// <param name="lifetime">Allows you to define the timetime of the service - defaults to ServiceLifetime.Transient</param> |
| 46 | + /// <returns></returns> |
| 47 | + public static IServiceCollection AsPublicImplementedInterfaces(this AutoRegisterData autoRegData, |
| 48 | + ServiceLifetime lifetime = ServiceLifetime.Transient) |
| 49 | + { |
| 50 | + if (autoRegData == null) throw new ArgumentNullException(nameof(autoRegData)); |
| 51 | + foreach (var classType in (autoRegData.TypeFilter == null |
| 52 | + ? autoRegData.TypesToConsider |
| 53 | + : autoRegData.TypesToConsider.Where(autoRegData.TypeFilter))) |
| 54 | + { |
| 55 | + var interfaces = classType.GetTypeInfo().ImplementedInterfaces |
| 56 | + .Where(i => i != typeof(IDisposable) && (i.IsPublic)); |
| 57 | + foreach (var infc in interfaces) |
| 58 | + { |
| 59 | + autoRegData.Services.Add(new ServiceDescriptor(infc, classType, lifetime)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return autoRegData.Services; |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments