Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Ninject注入类库,其中构造函数接受多个参数_C#_Inversion Of Control_Ninject - Fatal编程技术网

C# 使用Ninject注入类库,其中构造函数接受多个参数

C# 使用Ninject注入类库,其中构造函数接受多个参数,c#,inversion-of-control,ninject,C#,Inversion Of Control,Ninject,我正处于一个极度的困境之中。此外,如何将C#解决方案中的多个区域绑定到同一个容器。总之,我知道通过Ninject模块加载是最好的方法,但我无法直接访问内核(据我所知,这是一种反模式)来调用\u kernel.Get() 所以我相信构造函数注入是最好的方法。现在假设我有一个顶级类程序,它加载模块:ninject模块类 class Program { IKernel _kernel; public static main() { _kernel = new S

我正处于一个极度的困境之中。此外,如何将C#解决方案中的多个区域绑定到同一个容器。总之,我知道通过Ninject模块加载是最好的方法,但我无法直接访问内核(据我所知,这是一种反模式)来调用
\u kernel.Get()

所以我相信构造函数注入是最好的方法。现在假设我有一个顶级类
程序
,它加载
模块:ninject模块

class Program
{
    IKernel _kernel;
    public static main()
    {
        _kernel = new StandardKernel();
        _kernel.Load(ClassA.Module);
        _kernel.Load(ClassB.Module);

    }
}
为了使代码保持最少,假设ClassA模块将
ISomething
的所有实现绑定到
ConcreteSomething
,并且
ClassB
(其中
ClassA
依赖于)实现以下构造函数方法

public ClassB(ISomething thing, int paramA, int paramB)
{
    //Do stuff with paramA and paramB using thing
}
在单个解决方案中,可以直接访问_内核,
\u kernel.Get.WithConstructorArgument(“paramA”,123)。WithCon…

但是,我不确定在提供类无法访问其调用者容器的情况下这将如何工作

我脑海中闪过的一个想法是使用工厂方法,但我不知道这将如何工作


任何重量都将不胜感激

对依赖项使用构造函数注入是正确的

将参数(paramA,paramB)移动到一个方法,比如Initialize或Execute,怎么样

如果这是不合理的(比如当它发出命令时),那么您将需要使用工厂。 看一看

对于工厂扩展不支持的用例,您始终可以拥有一个注入IResolutionRoot的工厂。您可以在该界面上执行Get操作。工厂可以位于任何组件中

public class FactoryB : IFactoryB {
    private readonly IResolutionRoot resolutionRoot;
    public FactoryB(IResolutionRoot resolutionRoot) {
        this.resolutionRoot = resolutionRoot;
    }

    public IClassB Create(int paramA, int paramB) {
        return this.resolutionRoot.Get<IClassB>(new ConstructorArgument("paramA", paramA), new ConstructorArgument("paramB", paramB));
    }
}
公共类工厂B:IFactoryB{
私有只读IResolutionRoot resolutionRoot;
公共工厂B(IResolutionRoot resolutionRoot){
this.resolutionRoot=resolutionRoot;
}
公共IClassB创建(int-paramA,int-paramB){
返回this.resolutionRoot.Get(new-ConstructorArgument(“paramA”,paramA),new-ConstructorArgument(“paramB”,paramB));
}
}

我想知道你是否真的需要为paramA,paramB使用工厂。这取决于值的来源/确定方式。它们是依赖于classA还是仅仅是“全局配置”值?

paramA、paramB(ClassB的ctor)的值来自哪里?在我的例子中,这些参数将从组合根(即:
Program
aka GUI client)级联而下,但是,我可能需要使用中间库中的一些元素(
ClassA
在本例中)。在我的示例中,我有一个RESTful PCL,它需要为特定平台传递特定的哈希/加密算法。平台声明将在组合根(
客户端。{platform}
)中进行然后传递给所有平台共享的核心客户端库,然后调用PCL,注入特定的<代码> IHASH 加密散列。在这种情况下,在组合根(<代码>客户机> {平台} /代码>)中很容易绑定<代码> iHash <代码>,并在中间库中定义其余部分(<代码> ClassA < /代码>)。。您可能需要考虑基于约定的模块加载,因此您确实需要在
客户端中尽可能少地进行配置。{platform}
composition root。我当时不得不放弃IoC,因为我们需要发布一个最低可行的产品。有很多紧密耦合的类需要在核心客户端库中进行大量的工作(以及大量的PCL问题),但我会在接下来的几周内重写时注意这一建议。