Skip to content

BorowskiKamil/NetCoreIoCAutoRegister

Repository files navigation

.NET Core IoC Auto Register

This simple extension provides a way to scan and auto-register components in assemblies according to user-specified rules in .NET Core default dependency injection container.

Available packages

Package name Current version (master branch)
NetCoreIoCAutoRegister NuGet

How to get started

Scanning for Types

services.RegisterTypes(Assembly.GetExecutingAssembly())
		.Where(x => x.Name.EndsWith("Repository"))
		.AsScoped();

Remember: Each RegisterTypes() call will apply one set of rules only - you can't do multiple invocations in one approach.

To submit given rules call AsSingleton(), AsTransient(), AsScoped() according which service lifetime you want to choose.

Filtering Types

RegisterTypes() accepts as a parameter assembly. You can provide more assemblies by calling OfAssemblies() where the parameter is list of assemblies.

services.RegisterTypes()
	.OfAssemblies(new List<Assembly> { Assembly.GetExecutingAssembly() } )
	.AsScoped();

If you only want your public classes registered, use PublicOnly():

services.RegisterTypes(Assembly.GetExecutingAssembly())
		.PublicOnly()
		.AsScoped();

To apply custom filtering use Where() method:

services.RegisterTypes(Assembly.GetExecutingAssembly())
		.Where(x => x.Name.EndsWith("Service"))
		.AsScoped();

To exclude types from scanning use Except<>() method:

services.RegisterTypes(Assembly.GetExecutingAssembly())
		.Where(x => x.Name.EndsWith("Repository"))
		.Except<IFirstRepository>()
		.AsScoped();

About

Scan and auto-register components in assemblies according to user-specified rules in .NET Core.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages