views:

62

answers:

1

Using ninject, I know I can bind to a certain implementation, such that whenver I do:

ISomeCache myCache = Ninject.Get();

It will load my concrete implementation that I binding ninject to.

Say I store which concrete implementation I want to use in the database (web application), and when I change that in the admin panel it should on the fly use the class I want.

Is this possible?

+3  A: 

As long as you can determine the context in which you are executing, yes you can do it.

kernel.Bind<IMyService>().To<MyUserService>().When(request => GetSiteContext() == "user");

kernel.Bind<IMyService>().To<MyAdminService>().When(request => GetSiteContext() == "admin");

where GetSiteContext is a method you would write to determine what context you are executing in. .When(...) takes a Func<IRequest,bool> as a parameter (equal to Predicate<IRequest>).

Ian Davis