| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | |
| | | 3 | | using System.Reflection; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.Injection.Scrutor; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Registers types from assemblies using Scrutor. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// Scrutor does not traverse up the inheritance hierarchy of interfaces |
| | | 12 | | /// to look for the <see cref="DoNotAutoRegisterAttribute"/>. As a result, |
| | | 13 | | /// types that implement an interface with this attribute will still be |
| | | 14 | | /// registered unless the attribute is applied directly to the type itself. |
| | | 15 | | /// </remarks> |
| | | 16 | | public sealed class ScrutorTypeRegistrar : ITypeRegistrar |
| | | 17 | | { |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public void RegisterTypesFromAssemblies( |
| | | 20 | | IServiceCollection services, |
| | | 21 | | ITypeFilterer typeFilterer, |
| | | 22 | | IReadOnlyList<Assembly> assemblies) |
| | | 23 | | { |
| | 30 | 24 | | services |
| | 60 | 25 | | .Scan(x => x |
| | 60 | 26 | | .FromAssemblies(assemblies) |
| | 60 | 27 | | .AddClasses(classes => |
| | 30 | 28 | | classes |
| | 30 | 29 | | .WithoutAttribute<DoNotAutoRegisterAttribute>() |
| | 551 | 30 | | .Where(type => !typeFilterer.IsTypeExcluded(type)) |
| | 581 | 31 | | .Where(type => typeFilterer.IsInjectableScopedType(type)), |
| | 60 | 32 | | publicOnly: false) |
| | 60 | 33 | | .AsSelfWithInterfaces() |
| | 60 | 34 | | .WithScopedLifetime()); |
| | | 35 | | |
| | 30 | 36 | | services |
| | 60 | 37 | | .Scan(x => x |
| | 60 | 38 | | .FromAssemblies(assemblies) |
| | 60 | 39 | | .AddClasses(classes => |
| | 30 | 40 | | classes |
| | 30 | 41 | | .WithoutAttribute<DoNotAutoRegisterAttribute>() |
| | 551 | 42 | | .Where(type => !typeFilterer.IsTypeExcluded(type)) |
| | 581 | 43 | | .Where(type => typeFilterer.IsInjectableTransientType(type)), |
| | 60 | 44 | | publicOnly: false) |
| | 60 | 45 | | .AsSelfWithInterfaces() |
| | 60 | 46 | | .WithTransientLifetime()); |
| | | 47 | | |
| | 30 | 48 | | services |
| | 60 | 49 | | .Scan(x => x |
| | 60 | 50 | | .FromAssemblies(assemblies) |
| | 60 | 51 | | .AddClasses(classes => |
| | 30 | 52 | | classes |
| | 30 | 53 | | .WithoutAttribute<DoNotAutoRegisterAttribute>() |
| | 551 | 54 | | .Where(type => !typeFilterer.IsTypeExcluded(type)) |
| | 581 | 55 | | .Where(type => typeFilterer.IsInjectableSingletonType(type)), |
| | 60 | 56 | | publicOnly: false) |
| | 60 | 57 | | .AsSelfWithInterfaces() |
| | 60 | 58 | | .WithSingletonLifetime()); |
| | 30 | 59 | | } |
| | | 60 | | } |