Ninject kernel binding transfers

I'm just wondering what works best for reinstalling bindings in the kernel.

I have a class with a kernel and a private class module with standard product bindings.

In tests, I want to override these bindings so that I can swap my Duples / Mocks objects.

does

MyClass.Kernel.Load(new InlineModule(m=> m.Bind<IDepend>().To<TestDoubleDepend>()))

override any existing bindings for IDepend?

+5
source share
6 answers

​​DI , ( , , Attribute). , , DI, .

:

public interface IDependencyResolver : IDisposable
{
    T GetImplementationOf<T>();
}

public static class DependencyResolver
{
    private static IDependencyResolver s_resolver;

    public static T GetImplementationOf<T>()
    {
        return s_resolver.GetImplementationOf<T>();
    }

    public static void RegisterResolver( IDependencyResolver resolver )
    {
        s_resolver = resolver;
    }

    public static void DisposeResolver()
    {
        s_resolver.Dispose();
    }
}

, IDependencyResolver , RegisterResolver , , , . IoC, .

, IDependencyResolver , , . , , Ninject, IDependencyResolver.

, , , - , , IoC, , , , ( ) . , , ( " , , " ).

+5

, -

        var kernel = new StandardKernel(new ProductionModule(config));
        kernel.Rebind<ITimer>().To<TimerImpl>().InSingletonScope();

ProductionModule - , Rebind . , .

: - , , , . Guice java... , .

+3

, , - - , , , . ​​ . CI , script, .

, / , , .

+1

Peter Mayer shuoul Unit Test, IMHO, Mock /?

, (, ), .

- kronhrbaugh Hamish Smith, - " ", .

+1

MyClass, .
, .
, .

0

, , (, , , ..). .

dev, stage production , , . .

KernelFactory, IKernel .

This allows me to switch the environment token, which in turn will automatically change all my bindings.

But if it is for unit testing, I agree with the comments above that a simple constructor that allows manual binding is the way to go since it does not allow Ninject from your tests.

0
source

All Articles