Scrutor 5.0.2

Scrutor Build status NuGet Package

Scrutor - I search or examine thoroughly; I probe, investigate or scrutinize
From scrūta, as the original sense of the verb was to search through trash. - https://en.wiktionary.org/wiki/scrutor

Assembly scanning and decoration extensions for Microsoft.Extensions.DependencyInjection

Installation

Install the Scrutor NuGet Package.

Package Manager Console

Install-Package Scrutor

.NET Core CLI

dotnet add package Scrutor

Usage

The library adds two extension methods to IServiceCollection:

  • Scan - This is the entry point to set up your assembly scanning.
  • Decorate - This method is used to decorate already registered services.

See Examples below for usage examples.

Examples

Scanning

var collection = new ServiceCollection();

collection.Scan(scan => scan
     // We start out with all types in the assembly of ITransientService
    .FromAssemblyOf<ITransientService>()
        // AddClasses starts out with all public, non-abstract types in this assembly.
        // These types are then filtered by the delegate passed to the method.
        // In this case, we filter out only the classes that are assignable to ITransientService.
        .AddClasses(classes => classes.AssignableTo<ITransientService>())
            // We then specify what type we want to register these classes as.
            // In this case, we want to register the types as all of its implemented interfaces.
            // So if a type implements 3 interfaces; A, B, C, we'd end up with three separate registrations.
            .AsImplementedInterfaces()
            // And lastly, we specify the lifetime of these registrations.
            .WithTransientLifetime()
        // Here we start again, with a new full set of classes from the assembly above.
        // This time, filtering out only the classes assignable to IScopedService.
        .AddClasses(classes => classes.AssignableTo<IScopedService>())
            // Now, we just want to register these types as a single interface, IScopedService.
            .As<IScopedService>()
            // And again, just specify the lifetime.
            .WithScopedLifetime()
        // Generic interfaces are also supported too, e.g. public interface IOpenGeneric<T> 
        .AddClasses(classes => classes.AssignableTo(typeof(IOpenGeneric<>)))
            .AsImplementedInterfaces()
        // And you scan generics with multiple type parameters too
        // e.g. public interface IQueryHandler<TQuery, TResult>
        .AddClasses(classes => classes.AssignableTo(typeof(IQueryHandler<,>)))
            .AsImplementedInterfaces());

Decoration

var collection = new ServiceCollection();

// First, add our service to the collection.
collection.AddSingleton<IDecoratedService, Decorated>();

// Then, decorate Decorated with the Decorator type.
collection.Decorate<IDecoratedService, Decorator>();

// Finally, decorate Decorator with the OtherDecorator type.
// As you can see, OtherDecorator requires a separate service, IService. We can get that from the provider argument.
collection.Decorate<IDecoratedService>((inner, provider) => new OtherDecorator(inner, provider.GetRequiredService<IService>()));

var serviceProvider = collection.BuildServiceProvider();

// When we resolve the IDecoratedService service, we'll get the following structure:
// OtherDecorator -> Decorator -> Decorated
var instance = serviceProvider.GetRequiredService<IDecoratedService>();

Showing the top 20 packages that depend on Scrutor.

Packages Downloads
Elsa.Core
Elsa is a set of workflow libraries and tools that enable lean and mean workflowing capabilities in any .NET Core application. This package contains the core of Elsa. Tip: reference the `Elsa` package instead of this one.
6
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore
6
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore
10
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
6
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
7
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
8
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
9
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
10
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
11
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
12
Swashbuckle.AspNetCore.Filters
Some additional useful filters for Swashbuckle.AspNetCore. This package replaces Swashbuckle.AspNetCore.Examples.
14

Version Downloads Last updated
5.0.2 1 11/12/2024
5.0.1 0 09/26/2024
5.0.0 0 09/25/2024
4.2.2 7 05/06/2024
4.2.1 5 05/09/2024
4.2.0 8 06/01/2024
4.0.0 5 06/01/2024
3.2.2 3 06/01/2024
3.2.1 6 06/06/2022
3.2.0 5 06/01/2024
3.1.0 3 06/01/2024
3.0.3 7 06/01/2024
2.2.2 5 06/06/2022
2.2.1 3 06/01/2024
2.2.0 4 06/01/2024
2.1.2 10 06/06/2022
2.1.1 6 06/06/2022
2.1.0 4 06/01/2024
2.0.0 3 05/08/2024
2.0.0-rc2 5 06/06/2022
2.0.0-rc 7 06/06/2022
2.0.0-beta 6 05/22/2024
1.12.0 4 06/01/2024
1.11.0 4 06/01/2024
1.10.1 8 06/01/2024
1.10.0 2 06/01/2024
1.9.1 4 05/25/2024
1.9.0 4 06/01/2024
1.8.0 6 06/06/2022
1.6.0 8 06/01/2024
1.5.0 6 05/31/2024
1.4.0 6 06/06/2022
1.3.2 3 06/01/2024
1.3.1 9 06/06/2022
1.3.0 5 05/12/2024
1.2.0 6 06/01/2024
1.1.1 4 06/01/2024
1.1.0 5 05/09/2024
1.0.0 3 06/01/2024