I'm working on a ASP.net core app and of course using dependency injection. Mainly injecting required services in constructors of pages or other services.
Some of these injected services are the same in almost every other constructor. For example there is the ILogger<T>
, an IAppInfo
holding some application wide information and there is an IFileServices
used in many other services also.
So I end up with lots of injections in constructors, in some more than 7.
Question is, which is the best way to get rid of all there parameters used in almost every constructor?
For now I have implemented another service with methods to get the required services:
public interface IBasicsService<out T>
{
ILogger<T> GetLogger();
ApplicationInfo GetApplicationInfo();
}
implemented in:
public class BasicsService<T> : IBasicsService<T>
{
private readonly ILogger<T> _logger;
private readonly ApplicationInfo _info;
// ReSharper disable once ContextualLoggerProblem
public BasicsService(ApplicationInfo info, ILogger<T> logger) {
_info = info;
_logger = logger;
}
public ILogger<T> GetLogger() {
return _logger;
}
public ApplicationInfo GetApplicationInfo() {
return _info;
}
}
Is that correct? Is it better to inject IServiceProvider
and resolve services with this?
Thank you.