Ninject: Shared DI/IoC container
Asked Answered
A

1

1

I want to share the container across various layers in my application. I started creating a static class which initialises the container and register types in the container.

public class GeneralDIModule : NinjectModule
{
    public override void Load()
    {
        Bind<IDataBroker>().To<DataBroker>().InSingletonScope();
    }
}

public abstract class IoC
{
    private static IKernel _container;

    public static void Initialize()
    {
        _container = new StandardKernel(new GeneralDIModule(), new ViewModelDIModule());
    }

    public static T Get<T>()
    {
        return _container.Get<T>();
    }
}

I noticed there is a Resolve method as well. What is the difference between Resolve and Get?

In my unit tests I don’t always want every registered type in my container. Is there a way of initializing an empty container and then register types I need. I’ll be mocking types as well in unit test so I’ll have to register them as well.

There is an Inject method, but it says lifecycle of instance is not managed?

Could someone please set me in right way?

How can I register, unregister objects and reset the container.

Aliber answered 19/5, 2010 at 3:41 Comment(4)
Hi Ian Thanks for the reply. Few questions - Does Get method create a new instance of the type with transient lifestyle? - If I create a mocked instance of a type using some mocking framework say Rhino Mocks and Inject it into the kernel then does the subsequent Get will create a new instance of that type or will they return the same instance? - Is ninject.moq somehow different to RhinoMocks? ThanksAliber
If you don't specify a lifestyle, or Specify .InTransientScope(), every call to .Get will return a new instance. If you can, use Moq or RhinoMocks before Ninject.Moq (which uses Moq). Moq and RhinoMocks are both good mocking frameworks.Indelicate
so if i specify SingletonScope when binding in the module, and inject an mocked instance of the type, every call to Get will return the same instance? What does it mean when it says Inject injects the specified existing instance, without managing its lifecycle? i also realised in Silverlight version of Ninject there is no version of Load which take string arugment, which can be used to load the modules dynamically Load("*.dll"). How can I achieve dynamic loading in Silverlight. ThanksAliber
The reason I want to load modules dynamically is to avoid circular dependency issue. I have following layers View --> ViewModel --> DataProvider --> ServiceClient (wcf proxies). Now I want a static IoC container that can be shared across these layers. I want to make my View testable and to do that I’ll have to inject the various dependencies in various layers and mock out those dependencies as well. Now issue I am facing is where to declare and load ninject modules. I guess this is where Castle Windsor stands strong at least you can declare binding externally in xml and load them dynamically.Aliber
I
2

Ninject by default binds components in a transient lifestyle and Ninject does not track transient instances. The Resolve is used internally and shouldn't be used by your code unless you really know what you are doing. If you want to mock your container, use the ninject.moq extension on github. The inject method you are referring to is for instances that you have created yourself. Use the Get and TryGet methods.

Indelicate answered 19/5, 2010 at 4:12 Comment(4)
In Ninject 2 the default lifestyle is singleton, not transient.Torrential
We changed it over a year ago to transient. You can see it in the blame for line 104: github.com/ninject/ninject/blame/master/src/Ninject/Planning/…Indelicate
Hi Ian Thanks for the reply. Few questions - Does Get method create a new instance of the type with transient lifestyle? - If I create a mocked instance of a type using some mocking framework say Rhino Mocks and Inject it into the kernel then does the subsequent Get will create a new instance of that type or will they return the same instance? - Is ninject.moq somehow different to RhinoMocks? ThanksAliber
Oops, sorry, didn't know it was changed back to transient.Torrential

© 2022 - 2024 — McMap. All rights reserved.