我正在尝试学习依赖注入(inject) (DI) 的基础知识。为此,我编写了一个教程,通过构建 C# 控制台应用程序演示如何使用 Ninject 进行 DI。
该应用程序的主要目的是能够使用不同的计算方法计算购物车中商品的值(value)。
应用程序中的类是:
- 一个简单的产品模型
- 两个购物车类,充当产品集合的简单包装器
- 需要一个 ValueProducts 方法的 ValueCalculator 接口(interface),该方法返 repo 物车中的某些商品总数
- ValueCalculator 接口(interface)的两个独立实现(迭代和 LINQ 方法)
我尝试通过 .WhenInjectedTo 扩展方法使用条件注入(inject),以便 ShoppingCart 对象将注入(inject) LinqValueCalculator,而 ShoppingCartTwo 对象将注入(inject) IterativeValueCalulator。但是,在这两种情况下都会注入(inject) 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();
}
}
}
最佳答案
我认为你的问题是:-
ninjectKernel.Get<IValueCalculator>()
正在评估之前它被传递到您的构造函数。
即它在其绑定(bind)上下文之外被调用。
与其自己新建对象,不如使用内核获取对象实例。
var shopCartTwo = ninjectKernel.Get<ShoppingCartTwo>();
请注意,您根本没有传递参数。 Ninject 将查看构造函数签名,确定存在 Unresolved 依赖关系,并使用适当的上下文绑定(bind)。
关于c# - 使用 ninject 和 c#.net 实现条件依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8803772/