More On Generics and Factories in the Data Access Layer: IoC and Multiple Database Support

In a recent post, Generics and Factories in the Data Access Layer – Code Generation, Eugene had a couple of questions about IoC and Multiple Database Support in the comments that I thought were worth sharing in a new post.


Inversion of Control

You don't need to use IoC with the DAO object created in the post. The DAO class would have a default constructor that calls out and gets the default IDatabase from a factory, such as DatabaseFactory, shown below:


public class DAO<T> where T : Entity
    IDatabase _database;

    public DAO() : this(DatabaseFactory.CreateDatabase())

    public DAO(IDatabase database)
        if (database == null)
            throw new ArgumentNullException("database");

        _database = database;


Therefore you don't need to provide an IDatabase in your client code. It is there for testing and IoC if you use it. You can instantiate the DAO as such:


OrderDAO dao = new OrderDAO();
Order order = dao.GetOrderByOrderId(1);


 The DAO Class will get the proper IDatabase class for you via the DatabaseFactory.


Multiple Database Support

The DatabaseFactory Class instantiates a concrete class that implements IDatabase. Therefore if you are using SQL Server, it may be a SqlDatabase Class, or if you are using Oracle it could be a OracleDatabase Class like the following:


public static class DatabaseFactory
    public static IDatabase CreateDatabase()
        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string providerName = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;

        IDatabase database;

        switch (providerName)
            case "System.Data.SqlClient": database = new SqlDatabase(connectionString); break;
            case "System.Data.OracleClient": database = new OracleDatabase(connectionString); break;
            default: throw new DataAccessException(string.Format("Uknown Provider: {0}", providerName));

        return database;


Here we grab the connection string and invariant name of the database provider in the web.config class and instantiate the proper concrete class based on that information. You could add more or different overloads to DatabaseFactory depending on your needs.

In this case it will use SqlDatabase since we are using "System.Data.SqlClient" as specified in our config file:


    <add name="ConnectionString" connectionString="..." providerName="System.Data.SqlClient"/>



Hopefully this clears up any concerns on IoC and Multiple Database Support. This is of course 1 solution to a challenge that has many solutions.

I was thinking about showing this using the Provider Model next time since I haven't talked about it in awhile.


FYI… Useful Examples on Provider Factories in ADO.NET 2.0


by David Hayden


This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

One Response to More On Generics and Factories in the Data Access Layer: IoC and Multiple Database Support

  1. Eugene Ye says:

    Hi David, in your multiple database support, are you talking about the extensibility of the system to connect to different database platform? I actually wants to know how to handle multiple database connections at the same time and draws data from both of them eg. database 1 contains West data , whereas database 2 contains East data. My system would draw West and East data, and work on them together. If I pass a database connection into the parameter(which the example default constructor do as well), where should I fit my second database. This is to consider that to connect to two database simultaneously is a later possibility (not a confirmed requirement), which is not to be implementend in the first phase.


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>