Change map structure configuration based on settings in app.config
I am using structmap in my project. To implement a different repository implementation, I want to have a switch in app.config that changes the whole real repository implementation to mock repos.
Let's say IRepository has two implementations RealRepository and MockRepository
ForRequestedType () .TheDefaultIsConcreteType ();
I want to have a switch in app.config / web.config say (Mock = 1) that changes the whole implementation of the real repositories to
ForRequestedType () .TheDefaultIsConcreteType ();
I don't want to write a complete plugin definition in app.config, I just want one switch, how to implement this?
a source to share
While this may sound strange, remember that your StructureMap registration code is still only C # and you have full accessibility to you. This means that you can use the if statement when needed. Since your condition is based on app.config, you don't need to consider changing the value at runtime - you can safely use that value during startup configuration.
if (appConfigIndicatesMockMode()){
ForRequestedType<IRepository>().TheDefaultIsConcreteType<MockRepository>()
} else {
ForRequestedType<IRepository>().TheDefaultIsConcreteType<RealRepository>()
}
where appConfigIndicatesMockMode()
is the method that reads your app.config parameter in the usual way and returns true or false.
a source to share