Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Scrutor/IImplementationTypeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,11 @@ public interface IImplementationTypeFilter : IFluentInterface
/// <param name="predicate">The predicate to match types.</param>
/// <exception cref="ArgumentNullException">If the <paramref name="predicate" /> argument is <c>null</c>.</exception>
IImplementationTypeFilter Where(Func<Type, bool> predicate);
}

/// <summary>
/// Will transform the types using the specified <paramref name="selector"/>.
/// </summary>
/// <param name="selector">A transform function to apply to each element.</param>
/// <exception cref="ArgumentNullException">If the <paramref name="selector" /> argument is <c>null</c>.</exception>
IImplementationTypeFilter Select(Func<Type, Type> selector);
}
10 changes: 9 additions & 1 deletion src/Scrutor/ImplementationTypeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,12 @@ public IImplementationTypeFilter Where(Func<Type, bool> predicate)
Types = Types.Where(predicate);
return this;
}
}

public IImplementationTypeFilter Select(Func<Type, Type> selector)
{
Preconditions.NotNull(selector, nameof(selector));

Types = Types.Select(selector);
return this;
}
}
25 changes: 25 additions & 0 deletions test/Scrutor.Tests/ScanningTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,20 @@ public void ShouldRegisterOpenGenericTypes()
Assert.Null(provider.GetService<IPartiallyClosedGeneric<string, int>>());
}

[Fact]
public void ShouldRegisterPartiallyOpenGenericTypesUsingSelect()
{
Collection.Scan(scan => scan.FromAssemblyOf<IDto>()
.AddClasses(classes =>
classes.AssignableTo<IDto>().Select(x => typeof(ProcessDtoCommandHandler<>).MakeGenericType(x)))
.AsImplementedInterfaces());

var provider = Collection.BuildServiceProvider();

Assert.NotNull(provider.GetService<ICommandHandler<ProcessDto<FooDto>>>());
Assert.NotNull(provider.GetService<ICommandHandler<ProcessDto<BarDto>>>());
}

[Fact]
public void ShouldNotIncludeCompilerGeneratedTypes()
{
Expand Down Expand Up @@ -556,6 +570,17 @@ public class QueryHandler : IQueryHandler<string, int> { }
public interface IOpenGeneric<T> : IOtherInheritance { }

public class OpenGeneric<T> : IOpenGeneric<T> { }

public interface ICommandHandler<T> { }

public class ProcessDto<T> where T : IDto { }
public class ProcessDtoCommandHandler<T> : ICommandHandler<ProcessDto<T>> where T : IDto { }

public interface IDto {}

public record FooDto : IDto {}

public record BarDto : IDto {}

public interface IPartiallyClosedGeneric<T1, T2> { }

Expand Down