Dependency Injection Parameters and Properties Like Castle Windsor

Dependency Injection Parameters and Properties Like Castle Windsor

by David Hayden ( Florida .NET Developer )

 

Castle Windsor has concepts of parameters and properties to help inject dependencies like strings and integers into your classes. For example, you may have a database service that requires a connection string to the database:

 

public class SqlDatabase : IDatabase
{
    public SqlDatabase(string connectionString)
    {
        ...
    }
}

 

The connection string isn't something that you would find in the container, so you need to add a parameter into the configuration to specify the value:

 

<component service="IDatabase" type="SqlDatabase">
  <parameters>
    <connectionString>MyConnectionString</connectionString>
  </parameters>
</component>

 

If you find that many components may use the connection string as a parameter, you can add a property to the configuration and use the property in the parameter:

 

<properties>
    <connection>MyConnectionString</connection>
</properties>


<component service="IDatabase" type="SqlDatabase">
  <parameters>
    <connectionString>#{connection}</connectionString>
  </parameters>
</component>

 

where #{connection} refers to the value of the connection property above. This way if the connection string changes you ownly need to change the property and not all the parameters.

This is all very, very cool and a necessary feature of any decent dependency injection tool.

I added this functionality today to my dependency injection tool.

In addition to the functionality above, I also added the concept of ExpressionEvaluators which I based on ExpressionBuilders in ASP.NET 2.0. I can specify a parameter value pulled from ConnectionStrings, like:

 

<add interfaceType="IDatabase" concreteType="SqlDatabase">
  <parameters>
      <add
        key="connectionString"
        value="${ConnectionString:MyConnectionString}" />
  </parameters>
</add>

 

The ConnectionStringExpressionEvaluator will look for the named connection string MyConnectionString in the app.config or web.config and use it as the parameter value for connectionString.

If you instead want to get a value from AppSettings, the AppSettingExpressionEvaluator will take care of that one:

 

<add interfaceType="IDatabase" concreteType="SqlDatabase">
    <parameters>
        <add
            key="connectionString"
            value="${AppSetting:MyConnectionString}" />
    </parameters>
</add>

 

There is also the concept of globals which are the same as properties in Windsor. You can use expressions in the globals as well:

 

<globals>
  <add
    key="MyConnectionString"
    value="${ConnectionString:MainConnectionString}" />
</globals>



<add interfaceType="IDatabase" concreteType="SqlDatabase">
    <parameters>
        <add
            key="connectionString"
            value="#{MyConnectionString}" />
    </parameters>
</add>

 

This last addition makes my dependency injection tool feature complete for 1.0.

 

by David Hayden ( Florida .NET Developer )

 

posted on Thursday, October 04, 2007 8:26 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices