【问题标题】:Nested Ninject Bindings - Dependency Injection嵌套的 Ninject 绑定 - 依赖注入
【发布时间】:2015-12-19 05:31:18
【问题描述】:

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

OuterService(IInnerService innerService)

InnerService(string configurationKey)

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

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

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

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

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

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

【问题讨论】:

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


    【解决方案1】:

    我认为普通的kernel.Bind&lt;IOuterService&gt;().To&lt;OuterService&gt;() 应该很适合你。

    我准备了一个小 sn-p 来确定,如果这真的是你想要的。这是正确的吗?

    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();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      相关资源
      最近更新 更多