Skip to content

Commit

Permalink
Add AsImplementedInterfaces overload with predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Mar 2, 2022
1 parent 5afe1a7 commit 937b19e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/Scrutor/IServiceTypeSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public interface IServiceTypeSelector : IImplementationTypeSelector
/// </summary>
ILifetimeSelector AsImplementedInterfaces();

/// <summary>
/// Registers each matching concrete type as all of its implemented interfaces.
/// </summary>
/// <param name="predicate">A predicate to filter which interfaces to register.</param>
ILifetimeSelector AsImplementedInterfaces(Func<Type, bool> predicate);

/// <summary>
/// Registers each matching concrete type as all of its implemented interfaces, by returning an instance of the main type
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Scrutor/LifetimeSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public ILifetimeSelector AsImplementedInterfaces()
return Inner.AsImplementedInterfaces();
}

public ILifetimeSelector AsImplementedInterfaces(Func<Type, bool> predicate)
{
return Inner.AsImplementedInterfaces(predicate);
}

public ILifetimeSelector AsSelfWithInterfaces()
{
return Inner.AsSelfWithInterfaces();
Expand Down
10 changes: 9 additions & 1 deletion src/Scrutor/ServiceTypeSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,17 @@ public ILifetimeSelector As(IEnumerable<Type> types)

public ILifetimeSelector AsImplementedInterfaces()
{
return AsImplementedInterfaces(_ => true);
}

public ILifetimeSelector AsImplementedInterfaces(Func<Type, bool> predicate)
{
Preconditions.NotNull(predicate, nameof(predicate));

return As(t => t.GetInterfaces()
.Where(x => x.HasMatchingGenericArity(t))
.Select(x => x.GetRegistrationType(t)));
.Select(x => x.GetRegistrationType(t))
.Where(predicate));
}

public ILifetimeSelector AsSelfWithInterfaces()
Expand Down
8 changes: 4 additions & 4 deletions test/Scrutor.Tests/ScanningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void Scan_TheseTypes()
{
Collection.Scan(scan => scan
.AddTypes<TransientService1, TransientService2>()
.AsImplementedInterfaces()
.AsImplementedInterfaces(x => x != typeof(IOtherInheritance))
.WithSingletonLifetime());

Assert.Equal(2, Collection.Count);
Expand Down Expand Up @@ -116,7 +116,7 @@ public void UsingRegistrationStrategy_ReplaceImplementationTypes()

var services = Collection.GetDescriptors<ITransientService>();

Assert.Equal(4, services.Count(x => x.ServiceType == typeof(ITransientService)));
Assert.Equal(3, services.Count(x => x.ServiceType == typeof(ITransientService)));
}

[Fact]
Expand All @@ -140,7 +140,7 @@ public void CanFilterTypesToScan()
Collection.Scan(scan => scan
.FromAssemblyOf<ITransientService>()
.AddClasses(classes => classes.AssignableTo<ITransientService>())
.AsImplementedInterfaces()
.AsImplementedInterfaces(x => x != typeof(IOtherInheritance))
.WithTransientLifetime());

var services = Collection.GetDescriptors<ITransientService>();
Expand Down Expand Up @@ -510,7 +510,7 @@ public interface ITransientService { }
[ServiceDescriptor(typeof(ITransientService))]
public class TransientService1 : ITransientService { }

public class TransientService2 : ITransientService { }
public class TransientService2 : ITransientService, IOtherInheritance { }

public class TransientService : ITransientService { }

Expand Down

0 comments on commit 937b19e

Please sign in to comment.