How to inject services without registering them?
I mean in the past some DI frameworks automatically registered Service
for IService
.
I'm in a situation where I have a dozen of services and basically registering every single one is a pain in the ass. so is this supported in asp.net core default DI framework?
The interface-based dependency injection can be achieved by creating the common interface and other classes are implements this interface to inject the dependency. In this type of DI, we can use either constructor injection or setter injection. There is a built-in support of dependency injection in ASP.net Core.
Dependency injection (DI) is a technique widely used in programming and well suited to Android development. By following the principles of DI, you lay the groundwork for good app architecture. Implementing dependency injection provides you with the following advantages: Reusability of code.
NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.
I guess you like the way it works with Autofac:
var assembly = typeof(MyModule).Assembly;
builder.RegisterAssemblyTypes(assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
But you don't want to switch to Autofac for some reasons (for example you want use extensions from external libraries to register their dependencies). So my suggestion is to create some extensions that use reflection like these ones:
public static IServiceCollection AddSingletonsByConvention(this IServiceCollection services, Assembly assembly, Func<Type, bool> interfacePredicate, Func<Type, bool> implementationPredicate)
{
var interfaces = assembly.ExportedTypes
.Where(x => x.IsInterface && interfacePredicate(x))
.ToList();
var implementations = assembly.ExportedTypes
.Where(x => !x.IsInterface && !x.IsAbstract && implementationPredicate(x))
.ToList();
foreach (var @interface in interfaces)
{
var implementation = implementations.FirstOrDefault(x => @interface.IsAssignableFrom(x));
if (implementation == null) continue;
services.AddSingleton(@interface, implementation);
}
return services;
}
public static IServiceCollection AddSingletonsByConvention(this IServiceCollection services, Assembly assembly, Func<Type, bool> predicate)
=> services.AddSingletonsByConvention(assembly, predicate, predicate);
Now you can register all your services by simple code like this:
var assembly = typeof(MyType).Assembly;
services.AddSingletonsByConvention(assembly, x => x.Name.EndsWith("Service"));
Feel free to customize these extensions to match your needs. For example you can fire an exception if you don't find implementations for some services if that will make you feel a bit safer.
The out-of-the-box DI doesn't support it and do not intend to do so. The built-in IoC Container is kept simple by design, to allow basic dependency injection which works for most cases.
If you want advanced features like registering by convention, assembly scanning or decorator support, you have to use 3rd party IoC container like Autofac, SimpleInjector, Castle Windsor.
I would like to mention the Scrutor nuget which let's you use the ASP.NET Core Dependency Injection but adds a fluent interface on top of it to allow for convention-based configuration.
Read this article for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With