views:

308

answers:

2

I personally like the option to configure StructureMap from C# code. From what I understand, one of the advantages of DI, is that we can easily swap in a new concrete instance. But, if the configuration is defined in code, then the concrete instances are hardcoded in the dll.

So, practically, its as good as having hard coded the dependencies, right? I know, during testing it makes life easier...

My point is, wouldnt it be better to use xml configuration instead? you want to plugin a new concrete instance? simply have your installer overwrite the structuremap.config file with the new one.

So, what is the preferred way to configure StructureMap?

Extra: Am forced to use C# configuration for the time being because I dont know how to pass the connection string to instance. I can write the connectionstring in the config file, but i would like to reuse the connectionstring defined in app.config.

+4  A: 

No matter which particular DI Container you use, you should always defer the resolution of the application's object graph to the last responsible moment. This is called the application's Composition Root.

You can write the bulk of your application without ever referencing the DI Container. This also means that you can defer the decision between configuration in code or config until you need it.

You shouldn't need the container at all for unit testing, but may need it for integration testing. However, in integration tests, you will likely need a different configuration for the container than in the final application.

All in all, configuring the container in code is the preferred approach these days because it's more robust and you can apply convention-based configuration mechanics.

XML configuration tends to be more brittle and too verbose. In most cases, it simply slows you down because you get no refactoring or compiler support.

However, XML configuration is still valid when you need to be able to swap dependencies without recompiling the application. Most DI Containers will let you mix those approaches so that you can have most of your configuration in code, but a few selected dependencies defined in XML for extensibility reasons.

Mark Seemann
hmm... that sounds reasonable. thanks!!
Amith George
While I agree with your answer. It seems the question really asked how do I get my application configuration into my types.
KevM
@KevM, not really. My question was more about which configuration option is better. It arose, because I was having difficulty getting my connection string in to my types using xml. I would have to have written the connection string one more time in the structuremap.config file. I had wanted to avoid having to maintain the connectionstring at two places.
Amith George
You are wise to avoid duplication. This is exactly what the "equal to app setting" capabilities of StructureMap are for in basic configuration mapping scenarios.
KevM
+5  A: 

To answer your question you can have your cake and eat it too in StructureMap. You can configure your container from code and push in that extra bit of config you need from the application configuration. That is what EqualToAppSetting is for.

Create a settings class

    public class DatabaseSettings
{
    public DatabaseSettings(string type, string connectionString)
    {
        Type = type;
        ConnectionString = connectionString;
    }

    public string Type { get; set; }
    public string ConnectionString { get; set; }
}

Next tell StructureMap to configure it using your application settings.

        [Test]
    public void setup_concrete_class_via_application_configuration()
    {
        var container = new Container(config =>
        {
            config.ForConcreteType<DatabaseSettings>().Configure
                .Ctor<string>("type").EqualToAppSetting("dovetail.database.type", "mssql")
                .Ctor<string>("connectionString").EqualToAppSetting("dovetail.database.connectionString");

        });

        var databaseSettings = container.GetInstance<DatabaseSettings>();
        databaseSettings.Type.ShouldEqual("mssql");
        databaseSettings.ConnectionString.ShouldEqual("Data Source=.; Initial Catalog=dovetail;User Id=sa;Password=sa;");
    }

Finally here is what the application settings look like in my application config:

    <appSettings>
    <add key="dovetail.database.type" value="mssql"/>
    <add key="dovetail.database.connectionString" value="Data Source=.;Initial Catalog=dovetail;User Id=sa;Password=sa;"/>    
</appSettings>
KevM
I did think about this, but the downside to me was that I would have to track the connectionstring at two places. one here and once in the connectionstring section of the web/app.config... Thanks for the reply :)
Amith George
am using your suggestion at the moment to solve my connection string issue. At the application start I edit the web.config to include a new app setting called connectionString. It gets its value from the connectionString defined in the proper connectionStrings section. Thus I have to modify the connection string only at one place. and my structuremap config can read it from the appsettings... thanks!
Amith George
Happy to help. We baked a way to conventionally pull config into settings objects to avoid any container configuration. I should really blog about that.
KevM