I'm using the Simple Injector IoC framework, and I would like to be able to change the dependency registration at run time. For example, I have two implementations, A
and B
, of interface I
. Implementation A
is registered at app start, but depending on some flag which can change during runtime, I would like to switch the implementation. We are currently doing this the OnActionExecuting
event of our BaseController
, which all of our controllers inherit from. Here is the sample code of what I am trying to do.
protected override void OnActionExecuting(
ActionExecutingContext filterContext)
{
if (IsRuntimeFlag)
{
// check current implementation type and
// change implementation to A
}
else
{
// check current implementation type and
// change implementation to B
}
base.OnActionExecuting(filterContext);
}
Thanks in advance for your help.
In case
IsRuntimeFlag
is a configuration value (thus cannot change during the lifetime of the application), you can make the registration as follows:or equally:
In case the value can change during the lifetime of the application a proxy or composite that deals with dispatching to the right instance is the right solution:
Because the composite directly depends on
A
andB
, you can simply register it as follows:When
A
orB
have a different lifetime than transient (a new instance for each request), you should also register them. For instance:You can also let your composite depend on the
I
abstraction itself instead of the concreteA
andB
implementations:Depending on the
I
abstraction makes this class more flexible and more testable. It does mean however, that you need to register it a little bit different: