【问题标题】:add new object in IOC container without having to specify every consturctor param在 IOC 容器中添加新对象,而无需指定每个构造函数参数
【发布时间】:2019-04-18 06:04:46
【问题描述】:

我正在尝试在 .net core 2.1 中向我的作用域服务添加一个类,我不希望提供者获得满足构造函数参数所需的所有服务。有没有更简单的方法来做到这一点?

services.AddScoped<BarcodePage>((provider) =>
    new BarcodePage(provider.GetService<IObservationRepository>(),
        provider.GetService<IPageFactory>(),
        provider.GetService<IMediator>(),
        provider.GetService<IUserRepository>()));

【问题讨论】:

    标签: dependency-injection .net-core ioc-container service-provider


    【解决方案1】:

    你可以注册为

    services.AddScoped<BarcodePage>();
    

    容器将为您解析 BarcodePage 类依赖项。

    【讨论】:

      猜你喜欢
      • 2014-03-05
      • 2011-01-10
      • 2010-09-11
      • 2017-11-10
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 2017-04-25
      相关资源
      最近更新 更多