Xml configuration or Configuration through code?

2019-01-18 23:16发布

问题:

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.

回答1:

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.



回答2:

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>