Search code examples
sitecoreinversion-of-controlcastle-windsorcastle

Windsor IOC registration for Sitecore


Is there in Castle Windsor IoC registration an equivalent for the When clause in Ninject? I want to use Castle Windsor in Sitecore. When I am in the PageEditor (boolean indicator) I want to use a static list instead of making a call to a backend application.

My solution should be something like:

        Component.For<ICartProvider>().ImplementedBy<OscarCartProvider>().DependsOn(Dependency.OnValue(false, StateHelper.InMockState())).LifestyleTransient(),
        Component.For<ICartProvider>().ImplementedBy<MockCartProvider>().DependsOn(Dependency.OnValue(true, StateHelper.InMockState())).LifestyleTransient(),

StateHelper.InMockState returns a boolean value which should result in laoding the MockCart provider instead of the OscarCartProvider.


Solution

  • The nearest equivalent to Ninject's When would probably be the IHandlerSelector interface, which allows you to select a given handler based on some predicate, which in the case of the IHandlerSelector is the return value of the HasOpinionAbout method.

    Ayende has an example for how HandlerSelectors can be used on his blog.

    Alternatively, and perhaps simpler since it looks like a static list implementation is always desired to be used with PageEditor, you could use DependsOn() when registering PageEditor with the container to register your static list implementation to be used over your other implementation.