Ryan Peters
Ryan Peters

Reputation: 7708

Environment-specific StructureMap configuration?

Back when I used StructureMap 2.6, I was able to have it pull configuration data from a config by setting PullConfigurationFromAppConfig to TRUE. This was helpful for allowing me to override instances depending on the build configuration.

In the newest version, this appears to be no more. How can I go about specifying custom instances via a configuration file, specifically the web.config (where I am currently doing transforms based on build config). Is this possible? Is there another IoC framework that DOES support this? Thanks.

Upvotes: 3

Views: 332

Answers (1)

NightOwl888
NightOwl888

Reputation: 56849

XML support has been dropped from most DI containers because it is considered an antiquated way to configure the container. XML is very brittle and there is no compile-time type checking, so it is often more trouble than it is worth to configure an application using it.

Even if you find a DI container that supports XML now, it is not likely to support it in the future.

The way around this is of course to put in however many conditional configuration settings you need for your use case. For example, you could use something along the lines of

<appSettings>
    <add key="DIConfig" value="Test" />
    <!--<add key="DIConfig" value="Production" />-->
</appSettings>

and then in your composition root use something like

if (ConfigurationManager.AppSettings["DIConfig"] == "Test")
{
    // load test configuration
}
else if (ConfigurationManager.AppSettings["DIConfig"] == "Production")
{
    // load production configuration
}

If you need to, you can make more configuration file settings (or even your own config section) to make the configuration more granular than this.

Upvotes: 2

Related Questions