添加一个参数DI注册



我有一个具有多个构造函数依赖性的类。可能值得重构,但以后。

我知道AutoFac可以通过以下代码进行:

builder.RegisterType<ConfigReader>()
       .As<IConfigReader>()
       .WithParameter("configSectionName", "sectionName");

它的意思是"解决参数configSectionName并使用默认流程时使用sectionName"

问题是,在di in in in di i din中构建的.NET核心中,除了工厂方法迫使我在其他所有参数上手动调用serviceProvider.GetService<IFoo>(),这很烦人。

可以直接表达出来,还是我必须选择AutoFac/Workarounds?我目前的讨厌代码是:

serviceCollection.AddTransient<IPollService, PollService>(provider => new PollService(
    provider.GetService<IEthereumSettings>(),
    provider.GetService<EthereumClientProvider>(),
    provider.GetService<ABIDeserialiser>(),
    provider.GetService<ConstructorCallEncoder>(),
    provider.GetService<IJsonSerializer>(),
    provider.GetService<ILogger>(),
    rootContractAddress));

我可以添加某种类型的RootContractConfig并进行注册,但是我必须修改API以使DI快乐似乎很难。

按设计,ASP.NET Core中的内置DI仅支持入门所需的一小部分功能。对于更高级的方案,可以用不同的DI容器代替内置的DI容器。

因此,如果要使用AUTOFAC,则可以替换ASP.NET Core Docs或AutoFac在ASP.NET Core Integration的文档中所述的内置DI。

最新更新