使用ninject和c#.net实现条件依赖注入

bra*_*bow 4 .net c# conditional dependency-injection ninject

我正在尝试学习依赖注入(DI)的基础知识.为此,我已经开始了一个教程,演示如何通过构建C#控制台应用程序来使用Ninject进行DI.

该应用程序本质上旨在能够使用不同的计算方法计算购物车中的项目的价值.

应用程序中的类是:

  • 一个简单的产品模型
  • 两个购物车类,作为产品集合的简单包装
  • ValueCalculator接口,它需要一个方法ValueProducts,它返回购物车中的一些项目
  • ValueCalculator接口的两个独立实现(迭代和LINQ方法)

我试图通过.WhenInjectedTo扩展方法使用条件注入,这样ShoppingCart对象就会注入一个LinqValueCalculator,而ShoppingCartTwo对象会注入一个IterativeValueCalulator.但是,在这两种情况下都会注入LinqValueCalculator.

见下面的代码

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Ninject;

    namespace NinjectDemo
{

// a simple product model
public class Product
{
    public int ProductID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Category { set; get; }
}

// calculator interface
public interface IValueCalculator
{
    decimal ValueProducts(params Product[] products);
}

// a specific implementation of the IValueCalculator using LINQ
public class LinqValueCalculator : IValueCalculator
{

    public LinqValueCalculator() {}

    public decimal ValueProducts(params Product[] products)
    {
        return (products.Sum(p => p.Price));
    }
}

// another implementation of IValueCalculator using iteration
// (*2 is to so that it returns a different result to LinqValueCalculator)
public class IterativeValueCalculator : IValueCalculator
{
    public IterativeValueCalculator() {}

    public decimal ValueProducts(params Product[] products)
    {
        decimal totalValue = 0;
        foreach (Product p in products)
        {
            totalValue += (p.Price) * 2;
        }
        return totalValue;
    }
}

// a shopping cart modelling a collection of products
public class ShoppingCart
{
    protected IValueCalculator calculator;
    protected Product[] products;

    public ShoppingCart(IValueCalculator calcParam)
    {
        calculator = calcParam;
        // define the set of products to sum
        products = new []
            {
                new Product() { Name = "Kayak", Price = 275M},
                new Product() { Name = "Lifejacket", Price = 48.95M},
                new Product() { Name = "Soccer ball", Price = 19.50M},
                new Product() { Name = "Stadium", Price = 79500M}
            };
    }

    public virtual decimal CalculateStockValue()
    {
        // calculate the total value of the products
        decimal totalValue = calculator.ValueProducts(products);
        // return the result
        return totalValue;
    }
}

// another, different, shopping cart
public class ShoppingCartTwo
{
    protected IValueCalculator calculator;
    protected Product[] products;

    public ShoppingCartTwo(IValueCalculator calcParam)
    {
        calculator = calcParam;
        // define the set of products to sum
        products = new[]
            {
                new Product() { Name = "Kayak", Price = 275M},
                new Product() { Name = "Lifejacket", Price = 48.95M},
                new Product() { Name = "Soccer ball", Price = 19.50M},
                new Product() { Name = "Stadium", Price = 79500M}
            };
    }

    public virtual decimal CalculateStockValue()
    {
        // calculate the total value of the products
        decimal totalValue = calculator.ValueProducts(products);
        // return the result
        return totalValue;
    }
}

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

        // define the bindings
        ninjectKernel.Bind<IValueCalculator>().To<IterativeValueCalculator> ().WhenInjectedInto<ShoppingCartTwo>();
        ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();

        // create the carts and inject the dependency
        ShoppingCart cart = new ShoppingCart(ninjectKernel.Get<IValueCalculator>());
        ShoppingCartTwo cartTwo = new ShoppingCartTwo(ninjectKernel.Get<IValueCalculator>());

        // perform the calculation and write out the result
        Console.WriteLine("Total: {0:c}", cart.CalculateStockValue());
        Console.WriteLine("Total: {0:c}", cartTwo.CalculateStockValue());

        Console.Read();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*lor 5

我认为你的问题是: -

ninjectKernel.Get<IValueCalculator>()
Run Code Online (Sandbox Code Playgroud)

它被传递到你的构造函数之前进行评估.

即它在其绑定上下文之外被调用.

不要自己新建对象,而是使用内核来获取对象实例.

var shopCartTwo = ninjectKernel.Get<ShoppingCartTwo>();
Run Code Online (Sandbox Code Playgroud)

请注意,您根本没有传递参数.Ninject将查看构造函数签名,确定存在未解析的依赖关系,并使用适当的上下文绑定.