-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test helper that registers a type with all its interfaces
- Loading branch information
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Merq; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddAll<TImplementation>(this IServiceCollection services) | ||
where TImplementation : class, new() | ||
{ | ||
AddAll(services, _ => new TImplementation()); | ||
return services; | ||
} | ||
|
||
public static IServiceCollection AddAll<TImplementation>( | ||
this IServiceCollection services, | ||
Func<IServiceProvider, TImplementation> implementationFactory) | ||
where TImplementation : class | ||
{ | ||
// Simulates what the DependencyInjection.Attribute does, by registering | ||
// the implementation with all the interfaces it implements. | ||
|
||
services.AddSingleton(implementationFactory); | ||
|
||
foreach (var interfaceType in typeof(TImplementation).GetInterfaces()) | ||
services.AddSingleton(interfaceType, s => s.GetRequiredService(typeof(TImplementation))); | ||
|
||
return services; | ||
} | ||
} |