Creating database connections with Unity

I was adding dependency injection to an existing project and opted to use Unity configured via the application configuration file. As I was running through the configuration, I hit a type that required a  database connection which it took in as an IDbConnection reference in the constructor so I added the following config under the container element:

 <types>
    <type type="MyProject.IRepository, MyProject, Version=1.0.0.0, Culture=neutral"
          mapTo="MyProject.DefaultRepository, MyProject, Version=1.0.0.0, Culture=neutral">
        <lifetime type="transient" />
        <typeConfig>
            <constructor>
                <param name="connection" parameterType="IDbConnection">
                    <dependency />
                </param>
            </constructor>
        </typeConfig>
    </type>
</types>

(Before going any further, I should point out that I’ve added a type alias to the config for IDbConnection to save entering “System.Data.IDbConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” each time.)

If you’re not familiar with the configuration file options for Unity, each type element tells Unity what to do when that type is requested. In the case of IRepository the mapTo attribute instructs Unity to create an instance of DefaultRepository. The typeConfig section is used to specify how you want Unity to create the type and in this case we are instructing it to use the constructor that takes a single parameter of type IDbConnection. The dependency sub-element tells Unity to use itself to resolve the value for the IDbConnection parameter, so we need to add that to the config:

 <types>
    <type type="MyProject.IRepository, MyProject, Version=1.0.0.0, Culture=neutral"
          mapTo="MyProject.DefaultRepository, MyProject, Version=1.0.0.0, Culture=neutral">
        <lifetime type="transient" />
        <typeConfig>
            <constructor>
                <param name="connection" parameterType="IDbConnection">
                    <dependency />
                </param>
            </constructor>
        </typeConfig>
    </type>
    <type type="IDbConnection" 
          mapTo="SqlConnection" >
        <typeConfig>
            <constructor>
                <param name="connectionString" parameterType="System.String">
                    <value value="Data Source=(local);Database=AdventureWorks;Integrated Security=SSPI;" />
                </param>
            </constructor>
        </typeConfig>
    </type>
</types>

We’ve now given Unity enough information to resolve the IRepository type and wire up the IDbConnection parameter in the constructor. One problem with this config is that whenever we try to resolve an IDbConnection we’re going to get a connection to AdventureWorks and my application needs a couple of different database connections. Fortunately Unity provides for this situation and we can add a name to the type mapping. So the IDbConnection specification becomes:

 <type type="IDbConnection" 
          mapTo="SqlConnection" 
          name="AdventureWorksConnection"  >

and we can now refer to this type by name in the parameter dependency:

 <dependency name="AdventureWorksConnection"   />

This allows us to define multiple connection entries that we simply refer to by name when we need to. Using this approach, the same connection details can be referenced in multiple places in the config so connecting to a different server or database is simply a case of changing the connection string in a single place.  Also, assuming our repository class is able to deal with multiple database providers we can simply switch out the provider by changing the type Unity is creating for us (currently SqlConnection).

There’s lots more to Unity and for more information the Introduction to Unity page on MSDN is a good place to start. Next time we’ll take advantage of some of the flexibility of Unity to fix an something that I don’t really like about the above solution.