This project is read-only.

Ninject WhenInjectedInto keyword equivalent in simple Injector

Nov 29, 2014 at 3:50 PM
I am new to Simple Injector. I have the following code using NINJECT.

kernel.Rebind<IRegionService>().To<CachingRegionDataWrapper>();
kernel.Bind<IRegionService>().To<RegionDataService>().WhenInjectedInto<CachingRegionDataWrapper>();

Wanted to translte this code into Simple Injector.
Could you please help here?
Nov 29, 2014 at 3:56 PM
There's no built-in equivalent to Ninject's WhenInjectedInto method, but the documentation describes how to do context based injection here. The documentation describes the RegisterWithContext extension method (click here for the source code). Using the RegisterWithContext, the Simple Injector equivalent to your Ninject registration will be as follows:
container.Register<IRegionService, CachingRegionDataWrapper>();
container.Register<RegionDataService>();
container.RegisterWithContext<IRegionService>(context => {
    if (context.ImplementationType == typeof(CachingRegionDataWrapper)) {
        return container.GetInstance<RegionDataService>();
    } else {
       // return something else here.
    }
});
Nov 29, 2014 at 6:11 PM
Many thanks for quick reply. First I added RegisterWithContext extension method and put the above code. This saved my time. Thanks again.