Ninject:绑定构造参数到其他对象的属性 [英] Ninject: Bind Constructor Argument to Property of Other Object

查看:229
本文介绍了Ninject:绑定构造参数到其他对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含在我的应用程序中使用设置的 IConfig 对象。此刻,我注入整个对象到需要它的每个对象的构造函数,如下所示:

I have an IConfig object that contains settings used throughout my application. At the moment, I inject the entire object into the constructor of each object that needs it, as follows:

public interface IConfig 
{
    string Username { get; }
    string Password { get; }
    //... other settings
}

public class Foo : IFoo
{
    private readonly string username;
    private readonly string password;

    public Foo(IConfig config)
    {
        this.username = config.Username;
        this.password = config.Password;
    }
}



缺点是, IConfig 包含因为它是从整体的配置文件deserialised,所以注射整个对象是不必要的大量的设置。我想要做的是改变构造函数美孚(用户名字符串,字符串密码),使其只接收其所需的设置。这也使得创建对象测试更容易(不必建立 IConfig 只是为了创造)。我想构造函数的参数直接绑定我的 NinjectModule ,类似如下:

The downside is that IConfig contains a large number of settings because it's deserialised from an overall config file, so injecting the entire object is unnecessary. What I'd like to do is change the constructor to Foo(string username, string password) so that it only receives the settings it needs. This also makes creating Foo objects for testing much easier (not having to set up IConfig just to create Foo). I'd like to bind the constructor arguments directly in my NinjectModule, something like the following:

public class MyModule : NinjectModule
{
    public override void Load()
    {
        Bind<IConfig>().To<JsonConfig>()
            .InSingletonScope();

        Bind<IFoo>().To<Foo>()
            .WithConstructorArgument("username", IConfig.Username)
            .WithConstructorArgument("password", IConfig.Password);
    }
}



显然,这代码不工作,但如何将我去这样做我想要的东西?

Obviously this code doesn't work, but how would I go about doing what I wanted?

我最初的想法是使用 NinjectModule.Kernel 来获得的iKernel 然后让我的 IConfig 对象的实例,并根据需要注入的属性,但返回的对象 NinjectModule.Kernel 没有获取LT; T>()

My original idea was to use the NinjectModule.Kernel to get the IKernel then get an instance of my IConfig object and inject the properties as required, but the object returned by NinjectModule.Kernel has no Get<T>() method.

推荐答案

您在正确的轨道上:

Kernel.Get< T>()方法是在 Ninject namepsace上的 ResolutionExtensions 定义的扩展方法,以便与添加使用Ninject; 这是你的模块以及可用

The Kernel.Get<T>() method is an extension method defined on the ResolutionExtensions in the Ninject namepsace so with adding the using Ninject; it is available in your module as well.

但不是 Module.Kernel 你应该使用 IContext WithConstructorArgument 的第二个重载提供获得内核

But instead of the Module.Kernel you should use the IContext provided in the second overload of WithConstructorArgument to get the Kernel:

Bind<IFoo>().To<Foo>()
    .WithConstructorArgument("username", 
                             context => context.Kernel.Get<IConfig>().Username)
    .WithConstructorArgument("password", 
                             context => context.Kernel.Get<IConfig>().Password);

这篇关于Ninject:绑定构造参数到其他对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆