c# - 嵌套 Ninject 绑定(bind) - 依赖注入(inject)

标签 c# dependency-injection ninject ninject.web.mvc

我有两个类(具有类似命名的接口(interface)):

OuterService(IInnerService innerService)

InnerService(string configurationKey)

在我的 NinjectMvc3 类中,我有一个 IInnerService 接口(interface)的绑定(bind):

kernel.Bind<IInnerService>.ToMethod(c => new InnerService("myConfigurationKey")))

现在,我只是在 IOuterService 绑定(bind)中复制 InnerService 的构造函数实例化:

kernel.Bind<IOuterService>.ToMethod(c => new OuterService(new InnerService("myConfigurationKey")))

有什么方法可以通过使用 IInnerService 接口(interface)进行嵌套/级联注入(inject)来避免第二个 InnerService 构造函数吗?

// I realize this isn't valid, but it may better clarify my intent:
kernel.Bind<IOuterService>.ToMethod(c => new OuterService(IInnerService))

最佳答案

我觉得普通kernel.Bind<IOuterService>().To<OuterService>()应该很适合你。

我准备了一个小片段来确定这是否真的是您想要的。这是正确的吗?

using System;
using Ninject;

namespace NinjectTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IKernel kernel = new StandardKernel();

            kernel.Bind<IInnerService>().ToMethod(c=>new InnerService("this is a test config key")); //bind InnerService implementation to be used with provided string
            kernel.Bind<IOuterService>().To<OuterService>(); //bind OuterService implementation to be used, all parameters will be injected to it using previously defined configs

            var outerService = kernel.Get<IOuterService>();

            var result = outerService.CallInner();

            Console.WriteLine(result);
            Console.ReadLine();
        }

        public interface IInnerService
        {
            string GetConfigKey();
        }

        public class InnerService : IInnerService
        {
            private readonly string _configurationKey;

            public InnerService(string configurationKey)
            {
                _configurationKey = configurationKey;
            }

            public string GetConfigKey()
            {
                return _configurationKey;
            }
        }

        public class OuterService : IOuterService
        {
            private readonly IInnerService _innerService;

            public OuterService(IInnerService innerService)
            {
                _innerService = innerService;
            }

            public string CallInner() //purely for testing
            {
                return _innerService.GetConfigKey();
            }
        }   

        public interface IOuterService
        {
            string CallInner();
        }
    }
}

关于c# - 嵌套 Ninject 绑定(bind) - 依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34360954/

相关文章:

c# - 编译表达式比解释版本运行得慢得多

c# synchronization 锁的行为方式

c# - 如何在 sql server 中存储 SecureString?

java - Dagger2 字段注入(inject)不起作用

asp.net-mvc-3 - 如何选择DI容器?

c# - 测量 C# 代码的可测试性

c# - 如何结合 BindBase() 和 BindAllInterfaces()?

asp.net-mvc-3 - 在 MVC3 应用程序中将 Ninject 与自定义角色提供程序结合使用

dependency-injection - 如何创建一个返回相同对象的 Ninject 自定义范围,直到该对象被释放?

c# - 什么是NullReferenceException,如何解决?