Skip to content

Commit a9e535c

Browse files
committed
Added way to ignore interfaces
1 parent 2dc5955 commit a9e535c

File tree

7 files changed

+129
-23
lines changed

7 files changed

+129
-23
lines changed

NetCore.AutoRegisterDi/AutoRegisterData.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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.
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.
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Runtime.Serialization;
67
using Microsoft.Extensions.DependencyInjection;
78

89
namespace NetCore.AutoRegisterDi
910
{
1011
/// <summary>
11-
/// This holds the data passed between the various stages of the AutoRegisterDi extension methods
12+
/// This holds the data passed between the the various Stages
1213
/// </summary>
1314
public class AutoRegisterData
1415
{
@@ -34,9 +35,13 @@ public AutoRegisterData(IServiceCollection services, IEnumerable<Type> typesToCo
3435
public IEnumerable<Type> TypesToConsider { get; }
3536

3637
/// <summary>
37-
/// This holds an options test method which will be applied using a Where clause to filter the classes
38-
/// If the TypeFiler is null, then no filtering is done
38+
///
3939
/// </summary>
40-
public Func<Type, bool> TypeFilter { get; set; }
40+
internal List<Type> InterfacesToIgnore { get; set; }
41+
= new List<Type>
42+
{
43+
typeof(IDisposable),
44+
typeof(ISerializable)
45+
};
4146
}
4247
}

NetCore.AutoRegisterDi/AutoRegisterHelpers.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
// Copyright (c) 2018 Inventory Innovations, Inc. - build by Jon P Smith (GitHub JonPSmith)
1+
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
22
// Licensed under MIT license. See License.txt in the project root for license information.
33
// Code added/updated by Fedor Zhekov, GitHub: @ZFi88
44

55
using System;
66
using System.Linq;
7+
using System.Net.Http.Headers;
78
using System.Reflection;
9+
using System.Runtime.Serialization;
810
using Microsoft.Extensions.DependencyInjection;
911

1012
namespace NetCore.AutoRegisterDi
@@ -42,12 +44,27 @@ public static AutoRegisterData RegisterAssemblyPublicNonGenericClasses(this ISer
4244
public static AutoRegisterData Where(this AutoRegisterData autoRegData, Func<Type, bool> predicate)
4345
{
4446
if (autoRegData == null) throw new ArgumentNullException(nameof(autoRegData));
45-
autoRegData.TypeFilter = predicate;
4647
return new AutoRegisterData(autoRegData.Services, autoRegData.TypesToConsider.Where(predicate));
4748
}
4849

4950
/// <summary>
50-
/// This registers the classes against any public interfaces (other than IDisposable) implemented by the class
51+
/// This allows you to state that the given interface will not be registered against a class.
52+
/// Useful if a class has a interface that you don't want registered against a class.
53+
/// NOTE: the <see cref="IDisposable"/> and <see cref="ISerializable"/> interfaces are automatically ignored
54+
/// </summary>
55+
/// <typeparam name="TInterface">interface to be ignored</typeparam>
56+
/// <param name="autoRegData"></param>
57+
/// <returns></returns>
58+
public static AutoRegisterData IgnoreThisInterface<TInterface>(this AutoRegisterData autoRegData)
59+
{
60+
if (!typeof(TInterface).IsInterface)
61+
throw new InvalidOperationException($"The provided {typeof(TInterface).Name} mus be an interface");
62+
autoRegData.InterfacesToIgnore.Add(typeof(TInterface));
63+
return autoRegData;
64+
}
65+
66+
/// <summary>
67+
/// This registers the classes against any public interfaces (other than InterfacesToIgnore) implemented by the class
5168
/// </summary>
5269
/// <param name="autoRegData">AutoRegister data produced by <see cref="RegisterAssemblyPublicNonGenericClasses"/></param> method
5370
/// <param name="lifetime">Allows you to define the lifetime of the service - defaults to ServiceLifetime.Transient</param>
@@ -56,15 +73,15 @@ public static IServiceCollection AsPublicImplementedInterfaces(this AutoRegister
5673
ServiceLifetime lifetime = ServiceLifetime.Transient)
5774
{
5875
if (autoRegData == null) throw new ArgumentNullException(nameof(autoRegData));
59-
foreach (var classType in (autoRegData.TypeFilter == null
60-
? autoRegData.TypesToConsider
61-
: autoRegData.TypesToConsider.Where(autoRegData.TypeFilter)))
76+
foreach (var classType in autoRegData.TypesToConsider)
6277
{
6378
if (classType.IsMultipleLifetime())
6479
throw new ArgumentException($"Class {classType.FullName} has multiple life time attributes");
6580

6681
var interfaces = classType.GetTypeInfo().ImplementedInterfaces;
67-
foreach (var infc in interfaces.Where(i => i != typeof(IDisposable) && i.IsPublic && !i.IsNested))
82+
foreach (var infc in interfaces.Where(i =>
83+
!autoRegData.InterfacesToIgnore.Contains(i) //This will not register the class with this interface
84+
&& i.IsPublic && !i.IsNested))
6885
{
6986
autoRegData.Services.Add(new ServiceDescriptor(infc, classType, classType.GetLifetimeForClass(lifetime)));
7087
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Test.DifferentServices
4+
{
5+
public class ClassWithJustIDisposable : IDisposable
6+
{
7+
public void Dispose()
8+
{
9+
}
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Runtime.Serialization;
2+
3+
namespace Test.DifferentServices
4+
{
5+
public class ClassWithJustISerializable : ISerializable
6+
{
7+
public void GetObjectData(SerializationInfo info, StreamingContext context)
8+
{
9+
throw new System.NotImplementedException();
10+
}
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2019 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+
namespace Test.DifferentServices
5+
{
6+
public interface IAnotherInterface
7+
{
8+
bool IsPositive(int i);
9+
}
10+
}

Test/DifferentServices/LocalService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// Copyright (c) 2019 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
22
// Licensed under MIT license. See License.txt in the project root for license information.
33

4-
using NetCore.AutoRegisterDi;
5-
64
namespace Test.DifferentServices
75
{
8-
public class LocalService : ILocalService
6+
public class LocalService : ILocalService, IAnotherInterface
97
{
108
public bool IsPositive(int i)
119
{

Test/TestAutoRegisterDiCallingAssembly.cs

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
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.
33

44
using System;
55
using System.Collections.Generic;
@@ -32,6 +32,8 @@ public void TestRegisterAssemblyPublicNonGenericClasses()
3232
typeof(TestAutoRegisterDiCallingAssembly),
3333
typeof(TestAutoRegisterDiDifferentAssembly),
3434
typeof(TestTypeExtensions),
35+
typeof(ClassWithJustIDisposable),
36+
typeof(ClassWithJustISerializable),
3537
typeof(LocalScopeService), typeof(LocalService),
3638
typeof(LocalSingletonService), typeof(LocalTransientService),
3739
});
@@ -48,9 +50,11 @@ public void TestAsPublicImplementedInterfacesMyService()
4850
.AsPublicImplementedInterfaces();
4951

5052
//VERIFY
51-
service.Count.ShouldEqual(4);
53+
service.Count.ShouldEqual(5);
5254
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalService), ServiceLifetime.Transient),
5355
new CheckDescriptor()).ShouldBeTrue();
56+
service.Contains(new ServiceDescriptor(typeof(IAnotherInterface), typeof(LocalService), ServiceLifetime.Transient),
57+
new CheckDescriptor()).ShouldBeTrue();
5458
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalSingletonService), ServiceLifetime.Singleton),
5559
new CheckDescriptor()).ShouldBeTrue();
5660
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalTransientService), ServiceLifetime.Transient),
@@ -70,9 +74,11 @@ public void TestAsPublicImplementedInterfacesMyServiceSetLifetime()
7074
.AsPublicImplementedInterfaces(ServiceLifetime.Singleton);
7175

7276
//VERIFY
73-
service.Count.ShouldEqual(4);
77+
service.Count.ShouldEqual(5);
7478
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalService), ServiceLifetime.Singleton),
7579
new CheckDescriptor()).ShouldBeTrue();
80+
service.Contains(new ServiceDescriptor(typeof(IAnotherInterface), typeof(LocalService), ServiceLifetime.Singleton),
81+
new CheckDescriptor()).ShouldBeTrue();
7682
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalSingletonService), ServiceLifetime.Singleton),
7783
new CheckDescriptor()).ShouldBeTrue();
7884
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalTransientService), ServiceLifetime.Transient),
@@ -82,18 +88,65 @@ public void TestAsPublicImplementedInterfacesMyServiceSetLifetime()
8288
}
8389

8490
[Fact]
85-
public void TestWhere()
91+
public void TestSingleWhere()
92+
{
93+
//SETUP
94+
var service = new ServiceCollection();
95+
96+
//ATTEMPT
97+
service.RegisterAssemblyPublicNonGenericClasses()
98+
.Where(x => x.Name == nameof(LocalService))
99+
.AsPublicImplementedInterfaces();
100+
101+
//VERIFY
102+
service.Count.ShouldEqual(2);
103+
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalService), ServiceLifetime.Transient),
104+
new CheckDescriptor()).ShouldBeTrue();
105+
service.Contains(new ServiceDescriptor(typeof(IAnotherInterface), typeof(LocalService), ServiceLifetime.Transient),
106+
new CheckDescriptor()).ShouldBeTrue();
107+
}
108+
109+
[Fact]
110+
public void TestMultipleWhere()
111+
{
112+
//SETUP
113+
var service = new ServiceCollection();
114+
115+
//ATTEMPT
116+
service.RegisterAssemblyPublicNonGenericClasses()
117+
.Where(x => x.Name != nameof(LocalService))
118+
.Where(x => x.Name != nameof(LocalScopeService))
119+
.AsPublicImplementedInterfaces();
120+
121+
//VERIFY
122+
service.Count.ShouldEqual(2);
123+
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalSingletonService), ServiceLifetime.Singleton),
124+
new CheckDescriptor()).ShouldBeTrue();
125+
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalTransientService), ServiceLifetime.Transient),
126+
new CheckDescriptor()).ShouldBeTrue();
127+
}
128+
129+
[Fact]
130+
public void TestSingleIgnoreThisInterface()
86131
{
87132
//SETUP
88133
var service = new ServiceCollection();
89134

90135
//ATTEMPT
91136
service.RegisterAssemblyPublicNonGenericClasses()
92-
.Where(x => x.Name == nameof(MyService))
137+
.IgnoreThisInterface<IAnotherInterface>()
93138
.AsPublicImplementedInterfaces();
94139

95140
//VERIFY
96-
service.Count.ShouldEqual(0);
141+
service.Count.ShouldEqual(4);
142+
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalService), ServiceLifetime.Transient),
143+
new CheckDescriptor()).ShouldBeTrue();
144+
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalSingletonService), ServiceLifetime.Singleton),
145+
new CheckDescriptor()).ShouldBeTrue();
146+
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalTransientService), ServiceLifetime.Transient),
147+
new CheckDescriptor()).ShouldBeTrue();
148+
service.Contains(new ServiceDescriptor(typeof(ILocalService), typeof(LocalScopeService), ServiceLifetime.Scoped),
149+
new CheckDescriptor()).ShouldBeTrue();
97150
}
98151

99152
//-------------------------------------------------------------------------

0 commit comments

Comments
 (0)