请大家先看以下代码,我们先创建最基本的产品类,然后添加一个价格计算接口和一个实现此接口的类: @H_502_0@ //产品类
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; }
}
//价格计算接口
public interface IValueCalculator
{
decimal ValueProducts(params Product[] products);
}
//实现价格计算接口
public class LinqValueCalculator : IValueCalculator
{
public decimal ValueProducts(params Product[] products)
{
return products.Sum(p => p.Price);
}
} @H_502_0@接下来再创建购物车类,然后将以上接口添加进去: @H_502_0@ public class ShoppingCart
{
private IValueCalculator calculator; @H_502_0@ public ShoppingCart(IValueCalculator calcParam)
{
calculator = calcParam;
} @H_502_0@ public decimal CalculateStockValue()
{
// 定义和设置产品信息
Product[] products =
{
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}
};
// 计算总价格
decimal totalValue = calculator.ValueProducts(products); @H_502_0@ // 返回计算结果
return totalValue;
}
}
从以上代码可看出,ShoppingCart和LinqValueCalculator都依赖于IValueCalculator接口,但ShoppingCart和LinqValueCalculator之间并没有直接关系,甚至ShoppingCart并不知道LinqValueCalculator的存在,这样的好处也显而易见,就是LinqValueCalculator可以随时更换而不会影响到ShoppingCart里的代码,因此也达到了松耦合的效果了。@H_502_0@以下是上面几个类的关系图:
{
static void Main(string[] args)
{
IKernel ninjectKernel = new StandardKernel();
//绑定接口到实现类里
ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator<();
}
} @H_502_0@以上代码将接口与实现类绑定起来了,然后再添加以下代码以实现ShoppingCart类: @H_502_0@ // 获取实现的接口
IValueCalculator calcImpl = ninjectKernel.Get<IValueCalculator>();
// 创建实例注入到 ShoppingCart
ShoppingCart cart = new ShoppingCart(calcImpl);
// 执行计算功能以输出结果
Console.WriteLine("Total: {0:c}",cart.CalculateStockValue()); @H_502_0@可能大家会感觉这样使用还不如直接调用来得方便啊,如果不使用 Ninject ,我们只需直接使用: @H_502_0@ShoppingCart cart = new ShoppingCart(new LinqValueCalculator()); @H_502_0@这样就可以了,不是方便很多吗?为什么还要用Ninject写这么多代码呢?
嗯,以上其实只是一个最简单的为了演示的代码,如果随着复杂的多层接口的注入增加的话,就可以马上体现出Ninject的效率和好处了! @H_502_0@请继续看以下代码,我们再添加一个新的折扣接口: @H_502_0@//添加折扣接口
public interface IDiscountHelper
{
decimal ApplyDiscount(decimal totalParam);
} @H_502_0@ //实现不同的折扣
public class DefaultDiscountHelper : IDiscountHelper
{ @H_502_0@ public decimal ApplyDiscount(decimal totalParam)
{
return (totalParam - (10m / 100m * totalParam));
}
} @H_502_0@ //为之前的价格功能里添加折扣功能,实现折扣接口
public class LinqValueCalculator : IValueCalculator
{
private IDiscountHelper discounter; @H_502_0@ public LinqValueCalculator(IDiscountHelper discountParam)
{
discounter = discountParam;
} @H_502_0@ public decimal ValueProducts(params Product[] products)
{
return discounter.ApplyDiscount(products.Sum(p => p.Price));
}
} @H_502_0@现在我们又多了一层注入的接口了,但使用Ninject的话就非常方便,只需添加多一行代码就可以了: @H_502_0@ ...
IKernel ninjectKernel = new StandardKernel();
ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
//这是为新接口添加的,其他代码均不需改动
ninjectKernel.Bind<IDiscountHelper>().To<DefaultDiscountHelper>();
// 获取实现的接口
IValueCalculator calcImpl = ninjectKernel.Get<IValueCalculator>();
ShoppingCart cart = new ShoppingCart(calcImpl);
Console.WriteLine("Total: {0:c}",cart.CalculateStockValue());
... @H_502_0@随着代码接口的复杂性增多,就可以体现出Ninject的好处了 @H_502_0@ASP.NET MVC中应用: @H_502_0@namespace NinjectDemo.Infrastructure
{
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext,
Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
// put additional bindings here
ninjectKernel.Bind<IProductRepository>().To<FakeProductRepository>();
}
}
} @H_502_0@protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
} @H_502_0@引用:http://www.coderblog.in/2011/09/using-ninject-for-di.html 原文链接:https://www.f2er.com/javaschema/287032.html