StriplingWarrior
StriplingWarrior

Reputation: 156534

Context variables in Ninject 2

I found this article on Context Variables in an earlier version of Ninject. My question is two-fold. First, how can I get this behavior with Ninject 2? Secondly, do context variables carry through down the request chain? For example, let's say I wanted to replace these calls:

var a = new A(new B(new C())));
var specialA = new A(new B(new SpecialC()));

... with this:

var a = kernel.Get<A>();
var specialA = kernel.Get<A>(With.Parameters.ContextVariable("special", "true"));

Is it possible to set up a binding like this, where the context remembers that it is in a "special" context when it comes time to construct a C?

Upvotes: 4

Views: 1707

Answers (1)

Ruben Bartelink
Ruben Bartelink

Reputation: 61795

Here's some stuff that I use against V2, with ~0 effort to clean it up for you - let me know if you can't disentagle it.

As you surmised, there doesn't seem to be a really explicit API that surfaces the "context parameter, even for nested resolutions" stuff in v2 as-is (it's presence is buried as the 3rd parameter on an overload of the Parameter ctor).

public static class ContextParameter
{
    public static Parameter Create<T>( T value )
    {
        return new Parameter( value.GetType().FullName, value, true );
    }
}

public static class ContextParameterFacts
{
    public class ProductId
    {
        public ProductId( string productId2 )
        {
            Value = productId2;

        }
        public string Value { get; set; }
    }

    public class Repository
    {
        public Repository( ProductId productId )
        {
            ProductId = productId;

        }
        public ProductId ProductId { get; set; }
    }

    public class Outer
    {
        public Outer( Repository repository )
        {
            Repository = repository;
        }
        public Repository Repository { get; set; }
    }

    public class Module : NinjectModule
    {
        public override void Load()
        {
            Bind<ProductId>().ToContextParameter();
        }
    }

    //[ Fact ]
    public static void TwoDeepShouldResolve()
    {
        var k = new StandardKernel( new Module() );
        var o = k.Get<Outer>( ContextParameter.Create( new ProductId( "a" ) ) );
        Debug.Assert( "a" == o.Repository.ProductId.Value );
    }
}

And here's some code [that'll confuse the matter] which demonstrates how I apply it in my context:-

public class ServicesNinjectModule : NinjectModule
{
    public override void Load()
    {
        Bind<ProductId>().ToContextParameter();

        Bind<Func<ProductId, ResourceAllocator>>().ToConstant( ( productId ) => Kernel.Get<ResourceAllocator>(
            ContextParameter.Create( productId ) ) );
    }
}

public static class NinjectContextParameterExtensions
{
    public static IBindingWhenInNamedWithOrOnSyntax<T> ToContextParameter<T>( this IBindingToSyntax<T> bindingToSyntax )
    {
        return bindingToSyntax.ToMethod( context => (T)context.Parameters.Single( parameter => parameter.Name == typeof( T ).FullName ).GetValue( context ) );
    }
}

As usual, you should go look a the source and the tests - they'll provide you with a far more detailed and relevant answer than I can.

Upvotes: 3

Related Questions