C# 使用Ninject将父构造函数参数传递给嵌套类构造函数

C# 使用Ninject将父构造函数参数传递给嵌套类构造函数,c#,ninject,C#,Ninject,我试图让ninject使用父类的构造函数参数,并在实例化时将其作为参数传递给子类。我将如何进行绑定以正确实现这一点?我一直在浏览这些示例,但没有找到解决方案 public class MyModule : NinjectModule { public override void Load() { Bind<ParentClass>().ToSelf(); Bind<ChildClass>().ToSelf(); }

我试图让ninject使用父类的构造函数参数,并在实例化时将其作为参数传递给子类。我将如何进行绑定以正确实现这一点?我一直在浏览这些示例,但没有找到解决方案

public class MyModule : NinjectModule
{
    public override void Load()
    {
        Bind<ParentClass>().ToSelf();
        Bind<ChildClass>().ToSelf();
    }
}

public class ParentClass
{
    private string _index;
    private ChildClass _childClass;

    public ParentClass(string index, ChildClass childClass)
    {
        _index = index;
        _childClass = childClass;
    }
}

public class ChildClass
{
    private string _index;

    public ChildClass(string index)
    {
        _index = index;
    }

    public string Index { get; set; }
}

var kernel = new StandardKernel(new MyModule());
kernel.Get<ParentClass>(new ConstructorArgument("index", "MyIndex"));
公共类MyModule:Ninject模块
{
公共覆盖无效负载()
{
Bind().ToSelf();
Bind().ToSelf();
}
}
公共类父类
{
私有字符串_索引;
私人儿童班(ChildClass);;
公共父类(字符串索引、子类和子类)
{
_指数=指数;
_childClass=childClass;
}
}
公营儿童班
{
私有字符串_索引;
公共子类(字符串索引)
{
_指数=指数;
}
公共字符串索引{get;set;}
}
var kernel=新的标准内核(new MyModule());
Get(新的构造函数参数(“index”,“MyIndex”));

因此,当我创建ParentClass实例时,我希望其中的ChildClass具有相同的索引值。

只需更改要继承到子请求的参数,如下所示:

kernel.Get<ParentClass>(new ConstructorArgument("index", "MyIndex", true));
kernel.Get(新的构造函数参数(“index”,“MyIndex”,true));
这样,
ConstructorArgument
将应用于由
Get
调用实例化的所有对象。当然,
ConstructorArgument
仅在存在具有匹配名称的构造函数参数时应用。在您的例子中,参数被命名为
index
,用于
ParentClass
ChildClass
,因此它可以工作

另请参见
ConstructorArgument
constructor的文档和
iPParameter的文档

使现代化
在较新的Ninject版本中,现在有了匹配类型而不是参数名的。但是,如果它确实是一个像
string
那样无所不在的类型,那么将配置值“封装”在一个新类型中会更有意义,如所示。

只需更改要继承到子请求的参数,如下所示:

kernel.Get<ParentClass>(new ConstructorArgument("index", "MyIndex", true));
kernel.Get(新的构造函数参数(“index”,“MyIndex”,true));
这样,
ConstructorArgument
将应用于由
Get
调用实例化的所有对象。当然,
ConstructorArgument
仅在存在具有匹配名称的构造函数参数时应用。在您的例子中,参数被命名为
index
,用于
ParentClass
ChildClass
,因此它可以工作

另请参见
ConstructorArgument
constructor的文档和
iPParameter的文档

使现代化
在较新的Ninject版本中,现在有了匹配类型而不是参数名的。但是,如果它真的是像
string
那样无所不在的类型,那么将配置值“封装”在一个新类型中会更有意义,如所示。

我遇到了类似的问题,最初使用继承的构造函数参数解决方案,直到我的一位同事指出了缺点——如果您(或其他人)如果更改构造函数参数名称(索引),应用程序将中断

使用带继承的命名构造函数参数的替代方法是创建一个配置类型,为此请求上下文创建一个新内核,并将配置类型(该内核)绑定到一个常量。你会得到这样的结果:

public class MyModule : NinjectModule
{
    private string _index;

    public MyModule(string index)
    {
        _index = index;
    }

    public override void Load()
    {
        Bind<ConfigurationType>().ToConstant(new ConfigurationType(_index));
        Bind<ParentClass>().ToSelf();
        Bind<ChildClass>().ToSelf();
    }  
}

public class ParentClass
{
    private string _index;
    private ChildClass _childClass;

    public ParentClass(ConfigurationType configuration, ChildClass childClass)
    {
        _index = configuration.Index;
        _childClass = childClass;
    }
}

public class ChildClass
{
    private string _index;

    public ChildClass(ConfigurationType configuration configuration)
    {
        _index = configuration.Index;
    }

    public string Index { get; set; }
}
公共类MyModule:Ninject模块
{
私有字符串_索引;
公共MyModule(字符串索引)
{
_指数=指数;
}
公共覆盖无效负载()
{
Bind().ToConstant(新配置类型(_索引));
Bind().ToSelf();
Bind().ToSelf();
}  
}
公共类父类
{
私有字符串_索引;
私人儿童班(ChildClass);;
公共父类(配置类型配置,子类子类)
{
_索引=配置。索引;
_childClass=childClass;
}
}
公营儿童班
{
私有字符串_索引;
公共子类(配置类型配置)
{
_索引=配置。索引;
}
公共字符串索引{get;set;}
}

我曾经遇到过类似的问题,最初使用继承的构造函数参数解决方案,直到我的一位同事指出了缺点——如果您(或其他人)更改构造函数参数名称(索引),您的应用程序将崩溃

使用带继承的命名构造函数参数的替代方法是创建一个配置类型,为此请求上下文创建一个新内核,并将配置类型(该内核)绑定到一个常量。你会得到这样的结果:

public class MyModule : NinjectModule
{
    private string _index;

    public MyModule(string index)
    {
        _index = index;
    }

    public override void Load()
    {
        Bind<ConfigurationType>().ToConstant(new ConfigurationType(_index));
        Bind<ParentClass>().ToSelf();
        Bind<ChildClass>().ToSelf();
    }  
}

public class ParentClass
{
    private string _index;
    private ChildClass _childClass;

    public ParentClass(ConfigurationType configuration, ChildClass childClass)
    {
        _index = configuration.Index;
        _childClass = childClass;
    }
}

public class ChildClass
{
    private string _index;

    public ChildClass(ConfigurationType configuration configuration)
    {
        _index = configuration.Index;
    }

    public string Index { get; set; }
}
公共类MyModule:Ninject模块
{
私有字符串_索引;
公共MyModule(字符串索引)
{
_指数=指数;
}
公共覆盖无效负载()
{
Bind().ToConstant(新配置类型(_索引));
Bind().ToSelf();
Bind().ToSelf();
}  
}
公共类父类
{
私有字符串_索引;
私人儿童班(ChildClass);;
公共父类(配置类型配置,子类子类)
{
_索引=配置。索引;
_childClass=childClass;
}
}
公营儿童班
{
私有字符串_索引;
公共子类(配置类型配置)
{
_索引=配置。索引;
}
公共字符串索引{get;set;}
}

看看这个:@Dennisch参数现在可以定义了,它继承了
所以你链接到的答案现在是。。。让我们称之为被取代。看看这个:@Dennisch参数现在可以定义为继承的
,因此您链接到的答案是现在。。。让我们称之为被取代。