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

时间:2012-01-10 13:09:12

标签: c# .net dependency-injection conditional 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();
    }
}
}

1 个答案:

答案 0 :(得分:5)

我认为你的问题是: -

ninjectKernel.Get<IValueCalculator>()

正在评估之前将传递给你的构造函数。

即。它被绑定在其绑定环境之外。

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

var shopCartTwo = ninjectKernel.Get<ShoppingCartTwo>();

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