Skip to content

Commit 434da4b

Browse files
committed
Added feature to list what it registered with the DI provider
1 parent a9e535c commit 434da4b

File tree

6 files changed

+138
-9
lines changed

6 files changed

+138
-9
lines changed

NetCore.AutoRegisterDi/AutoRegisterData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class AutoRegisterData
1818
/// </summary>
1919
/// <param name="services"></param>
2020
/// <param name="typesToConsider"></param>
21-
public AutoRegisterData(IServiceCollection services, IEnumerable<Type> typesToConsider)
21+
internal AutoRegisterData(IServiceCollection services, IEnumerable<Type> typesToConsider)
2222
{
2323
Services = services ?? throw new ArgumentNullException(nameof(services));
2424
TypesToConsider = typesToConsider ?? throw new ArgumentNullException(nameof(typesToConsider));

NetCore.AutoRegisterDi/AutoRegisterHelpers.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Code added/updated by Fedor Zhekov, GitHub: @ZFi88
44

55
using System;
6+
using System.Collections;
7+
using System.Collections.Generic;
68
using System.Linq;
79
using System.Net.Http.Headers;
810
using System.Reflection;
@@ -37,6 +39,7 @@ public static AutoRegisterData RegisterAssemblyPublicNonGenericClasses(this ISer
3739
/// <summary>
3840
/// This allows you to filter the classes in some way.
3941
/// For instance <code>Where(c =\> c.Name.EndsWith("Service")</code> would only register classes who's name ended in "Service"
42+
/// NOTE: You can have multiple calls to this method to apply a series off filters
4043
/// </summary>
4144
/// <param name="autoRegData"></param>
4245
/// <param name="predicate">A function that will take a type and return true if that type should be included</param>
@@ -69,10 +72,16 @@ public static AutoRegisterData IgnoreThisInterface<TInterface>(this AutoRegister
6972
/// <param name="autoRegData">AutoRegister data produced by <see cref="RegisterAssemblyPublicNonGenericClasses"/></param> method
7073
/// <param name="lifetime">Allows you to define the lifetime of the service - defaults to ServiceLifetime.Transient</param>
7174
/// <returns></returns>
72-
public static IServiceCollection AsPublicImplementedInterfaces(this AutoRegisterData autoRegData,
75+
public static IList<AutoRegisteredResult> AsPublicImplementedInterfaces(this AutoRegisterData autoRegData,
7376
ServiceLifetime lifetime = ServiceLifetime.Transient)
7477
{
7578
if (autoRegData == null) throw new ArgumentNullException(nameof(autoRegData));
79+
80+
//This lists all the ignored interfaces
81+
var result = autoRegData.InterfacesToIgnore.Select(x =>
82+
new AutoRegisteredResult(null, x, ServiceLifetime.Singleton))
83+
.ToList();
84+
7685
foreach (var classType in autoRegData.TypesToConsider)
7786
{
7887
if (classType.IsMultipleLifetime())
@@ -83,11 +92,13 @@ public static IServiceCollection AsPublicImplementedInterfaces(this AutoRegister
8392
!autoRegData.InterfacesToIgnore.Contains(i) //This will not register the class with this interface
8493
&& i.IsPublic && !i.IsNested))
8594
{
86-
autoRegData.Services.Add(new ServiceDescriptor(infc, classType, classType.GetLifetimeForClass(lifetime)));
95+
var lifetimeForClass = classType.GetLifetimeForClass(lifetime);
96+
autoRegData.Services.Add(new ServiceDescriptor(infc, classType, lifetimeForClass));
97+
result.Add(new AutoRegisteredResult(classType, infc, lifetimeForClass));
8798
}
8899
}
89100

90-
return autoRegData.Services;
101+
return result;
91102
}
92103
}
93104
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace NetCore.AutoRegisterDi
5+
{
6+
/// <summary>
7+
/// This contains the registered class with its interface and lifetime that were register wit DI
8+
/// If the <see cref="Class"/> is null, then it is listing the ignored interfaces
9+
/// </summary>
10+
public class AutoRegisteredResult
11+
{
12+
internal AutoRegisteredResult(Type classType, Type interfaceType, ServiceLifetime lifetime)
13+
{
14+
Class = classType;
15+
Interface = interfaceType;
16+
Lifetime = lifetime;
17+
}
18+
19+
/// <summary>
20+
/// If not null, then it contains the class that is registered with DI
21+
/// If null, then the class is showing an interface in the ignored list
22+
/// </summary>
23+
public Type Class { get; }
24+
25+
/// <summary>
26+
/// If Class is not null, then this is the interface that the class was registered against
27+
/// If Class is null, then the interface is ignored
28+
/// </summary>
29+
public Type Interface { get; }
30+
31+
/// <summary>
32+
/// If Class is not null this contains the ServiceLifetime used when the class was registered in DI
33+
/// </summary>
34+
public ServiceLifetime Lifetime { get; }
35+
36+
/// <summary>
37+
/// Gives a easy to read summary of a result.
38+
/// </summary>
39+
/// <returns></returns>
40+
public override string ToString()
41+
{
42+
return Class == null
43+
? $"The interface {Interface.Name} is ignored"
44+
: $"{Class.Name} : {Interface.Name} ({Lifetime})";
45+
}
46+
}
47+
}

NetCore.AutoRegisterDi/NetCore.AutoRegisterDi.csproj

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<PackageVersion>2.0.0</PackageVersion>
7-
<Version>2.0.0</Version>
8-
<AssemblyVersion>2.0.0.0</AssemblyVersion>
9-
<FileVersion>2.0.0.0</FileVersion>
6+
<PackageVersion>2.1.0</PackageVersion>
7+
<Version>2.1.0</Version>
8+
<AssemblyVersion>2.1.0.0</AssemblyVersion>
9+
<FileVersion>2.1.0.0</FileVersion>
1010
<PackageId>NetCore.AutoRegisterDi</PackageId>
1111
<PackageProjectUrl>https://github.com/JonPSmith/NetCore.AutoRegisterDi</PackageProjectUrl>
1212
<PackageLicenseUrl>https://raw.githubusercontent.com/JonPSmith/NetCore.AutoRegisterDi/master/LICENCE.txt</PackageLicenseUrl>
@@ -17,7 +17,12 @@
1717
<Copyright>Copyright (c) 2018 Selective Analytics Ltd.</Copyright>
1818
<Company>Selective Analytics</Company>
1919
<Description>Extension method to find/register classes in an assembly into Microsoft.Extensions.DependencyInjection</Description>
20-
<PackageReleaseNotes>New Feature: You can now set what ServiceLifetime your class has via Attributes - see README file.</PackageReleaseNotes>
20+
<PackageReleaseNotes>
21+
- New feature: IgnoreThisInterface method allows you to state that the given interface will not be registered against a class
22+
- Improvement: Adds ISerializable interface base list interfaces to ignore (IDisposable is already in base ignore list)
23+
- Improvement: You can use muliple have Where methods to filter
24+
- Improvement: Returns a list of the classes that were registered with the DI provider (and what interfaces were ignored)
25+
</PackageReleaseNotes>
2126
<PackageIconUrl>https://raw.githubusercontent.com/JonPSmith/NetCore.AutoRegisterDi/master/AutoRegisterDiIcon128.png</PackageIconUrl>
2227
</PropertyGroup>
2328

Test/Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="JetBrains.DotMemoryUnit" Version="3.1.20200127.214830" />
1011
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
1112
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
1213
<PackageReference Include="xunit" Version="2.3.1" />

Test/TestAutoRegisterResult.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
2+
// Licensed under MIT license. See License.txt in the project root for license information.
3+
4+
using System.Linq;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using NetCore.AutoRegisterDi;
7+
using Test.DifferentServices;
8+
using Xunit;
9+
using Xunit.Extensions.AssertExtensions;
10+
11+
namespace Test
12+
{
13+
public class TestAutoRegisterResult
14+
{
15+
16+
[Fact]
17+
public void TestAsPublicImplementedInterfacesMyService()
18+
{
19+
//SETUP
20+
var service = new ServiceCollection();
21+
22+
//ATTEMPT
23+
var results = service.RegisterAssemblyPublicNonGenericClasses()
24+
.AsPublicImplementedInterfaces();
25+
26+
//VERIFY
27+
results.Count.ShouldEqual(7);
28+
var resultsAsString = results.Select(x => x.ToString()).ToList();
29+
resultsAsString[0].ShouldEqual("The interface IDisposable is ignored");
30+
resultsAsString[1].ShouldEqual("The interface ISerializable is ignored");
31+
32+
resultsAsString.ShouldContain("LocalService : ILocalService (Transient)");
33+
resultsAsString.ShouldContain("LocalService : IAnotherInterface (Transient)");
34+
resultsAsString.ShouldContain("LocalSingletonService : ILocalService (Singleton)");
35+
resultsAsString.ShouldContain("LocalTransientService : ILocalService (Transient)");
36+
resultsAsString.ShouldContain("LocalScopeService : ILocalService (Scoped)");
37+
}
38+
39+
[Fact]
40+
public void TestAsPublicImplementedInterfacesWithIgnore()
41+
{
42+
//SETUP
43+
var service = new ServiceCollection();
44+
45+
//ATTEMPT
46+
var results = service.RegisterAssemblyPublicNonGenericClasses()
47+
.IgnoreThisInterface<IAnotherInterface>()
48+
.AsPublicImplementedInterfaces();
49+
50+
//VERIFY
51+
results.Count.ShouldEqual(7);
52+
var resultsAsString = results.Select(x => x.ToString()).ToList();
53+
resultsAsString[0].ShouldEqual("The interface IDisposable is ignored");
54+
resultsAsString[1].ShouldEqual("The interface ISerializable is ignored");
55+
resultsAsString[2].ShouldEqual("The interface IAnotherInterface is ignored");
56+
57+
resultsAsString.ShouldContain("LocalService : ILocalService (Transient)");
58+
resultsAsString.ShouldContain("LocalSingletonService : ILocalService (Singleton)");
59+
resultsAsString.ShouldContain("LocalTransientService : ILocalService (Transient)");
60+
resultsAsString.ShouldContain("LocalScopeService : ILocalService (Scoped)");
61+
}
62+
63+
64+
}
65+
}

0 commit comments

Comments
 (0)