Skip to content

Latest commit

 

History

History
34 lines (26 loc) · 1.07 KB

use-with-ioc.md

File metadata and controls

34 lines (26 loc) · 1.07 KB

Use with IoC

Example how to use BBT.StrategyPattern with IoC.

public void WorksWithIocNinject()
{
    var calc1 = new CalculationInput() { Number1 = 5, Number2 = 3 };
    var op1 = new Operator() { Operation = OperatorEnum.Addition };
    var op2 = new Operator() { Operation = OperatorEnum.Subtraktion };

    IKernel kernel = new StandardKernel();

    kernel.Bind(typeof(IStrategyLocator<>)).To(typeof(NinjectStrategyLocator<>));

    kernel
        .Bind<IOperatorStrategy>()
        .To<AdditionStrategy>();
    kernel
        .Bind<IOperatorStrategy>()
        .To<SubtraktionStrategy>();
    kernel
        .Bind<IGenericStrategyProvider<IOperatorStrategy, Operator>>()
        .To<GenericStrategyProvider<IOperatorStrategy, Operator>>();

    IOperatorStrategy strategy;

    strategy = kernel.Get<IGenericStrategyProvider<IOperatorStrategy, Operator>>().GetStrategy(op1);
    strategy.DoCalculate(calc1).Should().Be(8);

    strategy = kernel.Get<IGenericStrategyProvider<IOperatorStrategy, Operator>>().GetStrategy(op2);
    strategy.DoCalculate(calc1).Should().Be(2);
}