Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

umar-draz-dotnet/NetCore.AutoRegisterDi

Open more actions menu
 
 

Repository files navigation

NetCore.AutoRegisterDi

This NuGet library contains an extension method to scan an assemby and register all the public classes against their implemented interfaces(s) into the Microsoft.Extensions.DependencyInjection dependency injection provider.

I have written a simple version of AutoFac's RegisterAssemblyTypes method that works directly with Microsoft's DI provider. Here are an example of me using this with ASP.NET Core

Example 1 - scan the calling assembly

public void ConfigureServices(IServiceCollection services)
{
   //... other configure code removed

   service.RegisterAssemblyPublicNonGenericClasses()
     .Where(c => c.Name.EndsWith("Service"))
     .AsPublicImplementedInterfaces();

Example 2 - scaning multiple assemblies

public void ConfigureServices(IServiceCollection services)
{
   //... other configure code removed

   var assembliesToScan = new [] 
   {
        Assembly.GetExecutingAssembly(),
        Assembly.GetAssembly(typeof(MyServiceInAssembly1)),
        Assembly.GetAssembly(typeof(MyServiceInAssembly2))
   };   

   service.RegisterAssemblyPublicNonGenericClasses(assembliesToScan)
     .Where(c => c.Name.EndsWith("Service"))
     .AsPublicImplementedInterfaces(); 

Licence: MIT.

See this article for a bigger coverage of Microsoft DI and the use of this library in real applications.

Why have I written this extension?

There are two reasons:

  1. I really hate having to hand-code each registering of the services - this extension method scans assembles and finds/registers classes with interfaces for you.
  2. I used to use AutoFac's assembly scanning feature, but I then saw a tweet by @davidfowl about Dependency Injection container benchmarks which showed the Microsoft's DI provider was much faster than AutoFac. I therefore implemented a similar (but not exactly the same) feature for the Microsoft.Extensions.DependencyInjection library.

Thanks to Inventory Innovations, Inc. who sponsored the creation of this library.

Detailed information

There are three parts:

  1. RegisterAssemblyPublicNonGenericClasses, which finds all the classes.
  2. An options Where method, which allows you to filter the classes to be considered.
  3. The AsPublicImplementedInterfaces method which finds ant interfaces on a class and registers those interfaces as pointing to the class.

1. The RegisterAssemblyPublicNonGenericClasses method

The RegisterAssemblyPublicNonGenericClasses method will find all the classes in

  1. If no assemblies are provided then it scans the assembly that called this method.
  2. You can provide one or more assemblies to be scanned. The easiest way to reference an assembly is to use something like this Assembly.GetAssembly(typeof(MyService)), which gets the assembly that MyService was defined in.

I only consider classes which match ALL of the critera below:

  • Public access
  • Not nested, e.g. It won't look at classes defined inside other classes
  • Not Generic, e.g. MyClass<T>
  • Not Abstract

2. The Where method

Pretty straightforward - you are provided with the Type of each class and you can filter by any of the Type properties etc. This allows you to do things like only registering certain classes, e.g Where(c => c.Name.EndsWith("Service"))

NOTE: Useful also if you want to register some classes with a different timetime scope - See next section.

3. The AsPublicImplementedInterfaces method

The AsPublicImplementedInterfaces method finds any public, non-nested interfaces (apart from IDisposable) that each class implements and registers each interface, known as service type, against the class, known as the implementation type. This means if you use an interface in a constructor (or other DI-enabled places) then the Microsoft DI resolver will provide an instance of the class that interface was linked to. See Microsoft DI Docs for more on this.*

By default it will register the classes as having a lifetime of ServiceLifetime.Transient, but there is a parameter that allows you to override that.

See this useful article on what lifetime (and other terms) means.

About

Extension method to find/register classes in an assembly into Microsoft.Extensions.DependencyInjection

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.