简体   繁体   English

合并到多个构造函数中注入的相同服务的最佳方法是什么?

[英]What is the best way to merge into one same services injected in several constructors?

I'm working on a ASP.net core app and of course using dependency injection.我正在开发 ASP.net 核心应用程序,当然还使用依赖注入。 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.例如,有ILogger<T> ,一个IAppInfo保存一些应用程序范围的信息,还有一个IFileServices用于许多其他服务。 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?所以我最终在构造函数中进行了大量注入,超过 7 个。问题是,这是摆脱几乎每个构造函数中使用的所有参数的最佳方法吗?

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?注入IServiceProvider并用它解决服务是否更好? Thank you.谢谢你。

It is better not to use IServiceProvider it hides the dependency of your service Assume that in some case you want to test this service oops your are forced to Mock IserviceProvider.最好不要使用 IServiceProvider 它隐藏了你的服务的依赖假设在某些情况下你想测试这个服务 oops 你被迫模拟 IserviceProvider。 The task that you wants to do has no differ from using ServiceLocator This link can help you to see why it is not good to use ServiceLocator你要做的任务和用ServiceLocator没什么区别 这个链接可以帮你看看为什么用ServiceLocator不好

ServiceLocator is an antipattern ServiceLocator 是一种反模式

But if you are really wants to do that in any circumstances, Constructor injection is not the only way that you can use to inject dependency.但是,如果您真的想在任何情况下都这样做,构造函数注入并不是您可以用来注入依赖项的唯一方法。 There is another way that called Property injection This is a good discussion about property injection还有一种方式叫做属性注入 这是一个很好的关于属性注入的讨论

Property Injection in ASP.NET Core ASP.NET Core 中的属性注入

Do not forget you can configure asp.net core to use some other IOC containers like castle Windsor or Unity that support property injection不要忘记你可以配置 asp.net core 来使用其他一些支持属性注入的 IOC 容器,比如 Castle Windsor 或 Unity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM