• Stars
    star
    3,214
  • Rank 13,971 (Top 0.3 %)
  • Language
    C#
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Assembly scanning and decoration extensions for Microsoft.Extensions.DependencyInjection

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>();

More Repositories

1

Middleware

Various ASP.NET Core middleware
C#
804
star
2

scriptcs-editor

An ultra-lightweight editor with syntax highlighting and intellisense for scriptcs
C#
97
star
3

MimeTypes

A simple lookup from file name/extension to MIME/media type, generated from mime-db, which in turn is compiled from IANA, Apache and nginx's MIME types.
Puppet
70
star
4

EF.Interception

A library for intercepting EntityFramework actions, like insert, update and delete.
C#
41
star
5

EFCore.Sqlite.NodaTime

Adds support for NodaTime types when using SQLite with Entity Framework Core.
C#
32
star
6

Hellang.MessageBus

A MessageBus for .NET 3.5+, Silverlight 4+, Windows Phone 7+, Xamarin.iOS, Xamarin.Android and .NET for Windows Store apps (WinRT)
C#
17
star
7

LangVersionFixer

A tiny utility to set the LangVersion property of all *.csprojs in a folder
C#
16
star
8

github-contributions

A simple console program that lists out the merged PR count, grouped by repository, done by the specified author.
C#
7
star
9

KestrelPureOwin

A demo on running pure OWIN directly on top of Kestrel
C#
7
star
10

Redskap

A collection of blazing fast ๐ŸŽ๏ธ๐Ÿ’จ Norwegian ๐Ÿ‡ณ๐Ÿ‡ด utilities with a โœจ modern API โœจ
C#
6
star
11

NntpLib.Net

An RFC 3977 and RFC 4643 compliant NNTP client library for .NET, written in C#
C#
6
star
12

yEnc

A tiny library for yEncoding and decoding with .NET
C#
5
star
13

Nzb

A tiny library for parsing NZB documents with .NET
C#
5
star
14

ResourceHelperGenerator

An MSBuild task that generates a strongly typed helper class for resource files with support for string formatting.
C#
5
star
15

Serilog.ReSharper

ReSharper support for Serilog
C#
5
star
16

Conference

C#
4
star
17

ExpressionActivator

A fast object activator based on compiled expression trees.
C#
4
star
18

PlayingCards

A tiny library with primitive types for building card games, like Suit, Rank, Card and Deck.
C#
3
star
19

nancy-bootstrapper-prototype

C#
3
star
20

Caliburn.Micro.ExposedProperties

A small Caliburn.Micro extension to allow exposing Model properties through a ViewModel with a simple attribute.
C#
3
star
21

fag-miles

2
star
22

Nancy.Autofac.Dnx

An example of running Nancy on DNX451 with and Autofac bootstrapper.
C#
2
star
23

CommandHandlerExample

A simple example for decorated command handlers
C#
2
star
24

Glimpse.Autofac

A Glimpse plugin to show Autofac registrations and resolutions
C#
2
star
25

NzbStation

C#
1
star
26

NETMF-NNUG

Code from presentation at NNUG Stavanger, 24/8-11
C#
1
star
27

NancyAutofacAspNetCore

A minimal Nancy + Autofac + ASP.NET Core 3.0 sample application
C#
1
star
28

CommandLine

C#
1
star
29

CamelSnakeKebab

C#
1
star
30

JetBrains.Annotations.Dnx

C#
1
star
31

bluecone

Automatically exported from code.google.com/p/bluecone
C#
1
star
32

Boilerplate

My preferred ASP.NET Core setup, packaged up into a few method calls ๐Ÿ”ฅ
1
star
33

blazor.jwttest

Quick test using JWT authentication for a blazor hosted (Client/Serverside) app with API and Authentication.
C#
1
star