本文介绍了IServiceCollection覆盖一个构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它需要三个构造函数。在我的组合根中,我想定义/覆盖三个构造函数参数中的一个其他两个依赖关系已经映射到我的DI容器中,应该从IServiceProvider创建。



与Ninject我可以这样做:

 绑定< IMyInterface>()。到< MyClass>()
.WithConstructorArgument(constructorArgumentName,x =>constructor argument value );

当Ninject创建MyClass时,它使用此字符串参数,并自动为我注入其他两个依赖项。我在.net核心遇到的问题是,我不能告诉IServiceCollection我只想指定三个参数之一,我必须定义所有这些或没有。例如,在.net核心中,这是我必须做的:

  services.AddTransient< IMyInterface>(x =>新的MyClass(constructor argument value,new Dependency2(),new Dependency3()); 

不喜欢创建Dependency2和Dependency3类的新实例;这两个类可以有自己的构造函数参数,我只是想DI来管理这些依赖关系,所以我的问题是 - 如何覆盖一个构造函数参数使用IServiceCollection类映射依赖于.net core?



如果不能仅覆盖一个引用参数,那么如何解决与IServiceCollection的依赖关系?做这样的事情:

  services.AddTransient< IMyInterface>(x => new MyClass(constructor argument value,serviceCollection .Resolve< IDependency2>(),serviceCollection.Resolve(IDependency3>()); 

但是这不行,我无法弄清楚如何使用IServiceCollection解析依赖关系。

解决方案

尝试这样:

  services.AddTransient< IDependency2,Dependency2Impl>() 

services.AddTransient< IDependency3,Dependency3Impl>();

services.AddTransient< IMyInterface>(provider =>
return new MyClass(constructor argument value,
provider.GetService< IDependency2>(),
provider.GetService< IDependency3>());
);


I have a class that takes three constructor arguments. In my composition root I want to define/override only one of the three constructor arguments; the other two dependencies have already been mapped in my DI container and should get created from the IServiceProvider.

With Ninject I could do something like this:

Bind<IMyInterface>().To<MyClass>()    
    .WithConstructorArgument("constructorArgumentName", x => "constructor argument value");

When Ninject creates MyClass it uses this string parameter and automatically injects the other two dependencies for me. The problem I'm experiencing in .net core is that I cannot tell the IServiceCollection I only want to specify one of the three arguments, I have to define all of them or none. For example, in .net core this is what I have to do:

services.AddTransient<IMyInterface>(x=> new MyClass("constructor argument value", new Dependency2(), new Dependency3());

I don't like having to create new instances of the Dependency2 and Dependency3 classes; these two classes could have their own constructor arguments. I just want the DI to manage those dependencies. So my question is - how do you override a single constructor argument when mapping dependencies in .net core using the IServiceCollection class?

If you can't override just a single contructor argument then how do you resolve a dependency with the IServiceCollection? I tried doing something like this:

services.AddTransient<IMyInterface>(x=> new MyClass("constructor argument value", serviceCollection.Resolve<IDependency2>(), serviceCollection.Resolve(IDependency3>());

But this didn't work, and I couldn't figure out how to resolve dependencies using the IServiceCollection.

解决方案

Try this:

services.AddTransient<IDependency2, Dependency2Impl>();

services.AddTransient<IDependency3, Dependency3Impl>();

services.AddTransient<IMyInterface>(provider=>
    return new MyClass("constructor argument value",
      provider.GetService<IDependency2>(),
      provider.GetService<IDependency3>());
);

这篇关于IServiceCollection覆盖一个构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 22:58