- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试学习依赖注入(inject) (DI) 的基础知识。为此,我编写了一个教程,通过构建 C# 控制台应用程序演示如何使用 Ninject 进行 DI。
该应用程序的主要目的是能够使用不同的计算方法计算购物车中商品的值(value)。
应用程序中的类是:
我尝试通过 .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/
我在 gobject 上阅读了一个维基百科页面,上面写着, Depending only on GLib and libc, GObject is a cornerstone of GNOME and
如何注册一个依赖属性,其值是使用另一个依赖属性的值计算的? 由于 .NET 属性包装器在运行时被 WPF 绕过,因此不应在 getter 和 setter 中包含逻辑。解决方案通常是使用 Proper
我一直在尝试将 ActionbarSherlock maven 依赖项添加到我的项目中 com.actionbarsherlock library 4.2.0 在我的 po
http://tutorials.jenkov.com/ood/understanding-dependencies.html#whatis说(强调我的): Whenever a class A us
我对所有这些魔法有点不清楚。 据我了解,依赖属性是从 DependencyObject 继承的,因此存储值: 如果分配了值(在本地字典中),则在实例本身中 或者如果未指定值,则从指向父元素的链接中获取
我刚刚更新了在 ASP.NET Framework 4.5.2 版上运行的 MVC Web 应用程序。我正在使用 Twilio 发送 SMS 消息: var twilio = new TwilioRe
我刚刚发现了一件令人生畏的事情。 spring 依赖坐标有两个版本。 项目依赖于 spring mvc 和 spring flow。有两组并行的依赖项。 Spring MVC 具有以下方案的依赖项
我正在尝试包含 的 maven 依赖项 org.jacorb jacorb 2.3.1 依赖已解决,但它导致另一个依赖 picocontainer 出现问题: [ERROR
我正在尝试在 Haskell 项目中包含特定版本的库。该库是住宿加早餐型的(用于 martix 操作),但我需要特定的 0.4.3 版本,该版本修复了乘法实现的错误。 所以,我的 stack.yaml
有谁知道如何制作依赖的 UIPickerView.例如,当我选择组件一的第 2 行时,组件二的标题会发生变化吗? 我在互联网上查找过,没有真正的答案,我尝试过使用 if 和 switch 语句,但它们
我正在编写一个用于验收测试的项目,由于各种原因,这依赖于另一个打包为 WAR 的项目。我已成功使用 maven-dependency-plugin 解压 WAR,但无法让我的项目包含解压的 WEB-I
或多或少我在 session 上大量构建我的网站(特别是重定向用户等),我很好奇这是否是一种危险的做法。禁用浏览器 cookie 保存的用户的大致比例是多少?我愿意接受任何建议:) 谢谢 最佳答案 s
开始玩 Scala futures,我被依赖的 futures 困住了。 让我们举个例子。我搜索地点并获得 Future[Seq[Place]]。对于这些地点中的每一个,我搜索最近的地铁站(该服务返回
或多或少我在 session 上大量构建我的网站(特别是重定向用户等),我很好奇这是否是一种危险的做法。禁用浏览器 cookie 保存的用户的大致比例是多少?我愿意接受任何建议:) 谢谢 最佳答案 s
我有一个二进制文件,需要一些 *.so 文件才能执行。现在,当我尝试在一些旧机器上执行它时,它会显示 /lib/libc.so.6: version `GLIBC_2.4' not found 如何将
我尝试使用 Dygraph 来表示图表,我在 https://github.com/danvk/dygraphs 中找到了代码,但是它有太多的依赖文件,我觉得很烦人。是否有一个文件可以容纳所有必需的
我正在处理一个 javascript 文件,该文件 a) 声明一个具有函数的对象,并且 b) 使用它期望在外部声明的散列调用该对象的 init 函数。我的 Jasmine 规范提示它找不到哈希,因为它
最近我一直在学习 Angular 并且进展顺利,但是关于依赖注入(inject)的一些事情我仍然不清楚。 是否有任何理由在我的 app.js 文件中声明我的应用程序的其他部分(服务、 Controll
考虑一个名为 foo 的表,它有 id (PRIMARY & AUTO_INCREMENT) 列。我正在向该表中插入一行,挑战从此时开始。 $db->query("INSERT INTO `foo`
我正在使用级联下拉 jquery 插件。 (https://github.com/dnasir/jquery-cascading-dropdown) 我有两个下拉菜单。 “客户端”和“站点”。 根据您
我是一名优秀的程序员,十分优秀!