Skip to content

Simple way to auto-register interfaces their implementations with asp.net core default dependency injection

License

Notifications You must be signed in to change notification settings

SharpTools/SharpDiAutoRegister

Repository files navigation

SharpDiAutoRegister

Simple way to auto-register interfaces to their implementations with asp.net core default dependency injection.

Nuget

Install-Package SharpDiAutoRegister

How to use

In your ConfigureServices method in Startup.cs, just use some regex to match the interfaces you want to register and you are done. SharpDiAutoRegister will scan the Assemblies you provided and register the first class it finds that implements that interface.

Ex:

services.ForInterfacesMatching("^I[a-zA-z]+Repository$")
        .OfAssemblies(Assembly.GetExecutingAssembly())
        .AddSingletons();

services.ForInterfacesMatching("^IRepository")
        .OfAssemblies(Assembly.GetExecutingAssembly())
        .AddTransients();

//and so on...

SharpDiAutoRegister will not override a pre-registered interface, so if you want a custom behavior for some of them, just register them before calling SharpDiAutoRegister methods.

Happy coding!

About

Simple way to auto-register interfaces their implementations with asp.net core default dependency injection

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages