Ninject WhenInjectedInto keyword equivalent in simple Injector

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


Wanted to translte this code into Simple Injector.
Could you please help here?
Nov 29, 2014 at 2: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.RegisterWithContext<IRegionService>(context => {
    if (context.ImplementationType == typeof(CachingRegionDataWrapper)) {
        return container.GetInstance<RegionDataService>();
    } else {
       // return something else here.
Nov 29, 2014 at 5:11 PM
Many thanks for quick reply. First I added RegisterWithContext extension method and put the above code. This saved my time. Thanks again.