3

I have been studying design pattern of "Gang of four design pattern".

I need to design data layer to handle multiple database as well as extend the function if required. The database could one is SQL, one XMl or some external system database.

What would be good design pattern to follow?

Edit: Based on suggestion, i tried to implement DAO and Factory pattern in Data layer. To an extent i could get to the point. But As per GOF desgin pattern, The data provider name decided which database object i can call. Like below. Where as i am trying to make the layer dynamic so that multiple database can handled. If i follow below pattern then i can only access one database in one application.

If somebody has looked into Gang of Four Design pattern then Please guide me.

public class DaoFactories
{
    public static IDaoFactory GetFactory(string dataProvider)
    {
        // Return the requested DaoFactory
        switch (dataProvider)
        {
            case "ADO.NET.Access": return new AdoNet.Access.AccessDaoFactory();
            case "ADO.NET.Oracle": return new AdoNet.Oracle.OracleDaoFactory();

            case "ADO.NET.SqlExpress":
            case "ADO.NET.SqlServer": return new AdoNet.SqlServer.SqlServerDaoFactory();

            case "LinqToSql.SqlExpress": 
            case "LinqToSql.SqlServer": return new LinqToSql.Implementation.LinqDaoFactory();

            case "EntityFramework.SqlExpress": 
            case "EntityFramework.SqlServer": return new EntityFramework.Implementation.EntityDaoFactory();

            // Default: SqlExpress
            default: return new AdoNet.SqlServer.SqlServerDaoFactory(); 
        }
    }
}
2
  • Sounds like a facade. Commented May 29, 2014 at 8:07
  • I tried to make my question more short after applying few pattern. If somebody could guide me then it would be nice. Thanks. Commented May 30, 2014 at 9:52

2 Answers 2

4

You should look into the Repository Pattern and Data Access Object Pattern

Repository Pattern: In many applications, the business logic accesses data from data stores such as databases, SharePoint lists, or Web services. Directly accessing the data can result in the following: Duplicated code A higher potential for programming errors Weak typing of the business data Difficulty in centralizing data-related policies such as caching An inability to easily test the business logic in isolation from external dependencies

Data Access Object: In computer software, a data access object (DAO) is an object that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer, DAO provide some specific data operations without exposing details of the database.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, Those two pattern is combined with singleton and implemented in Gang of four design pattern but i am facing one issue. Actually based on data provider GOF decide to DataAccessOject Group where as in my case Data access object is decided not like. ICustomerDao is from excel where as IOrderDao is based on Sql. So i am confused how to handle that.
Actually in pattern example they have divided the data provider group and code is repeated.
2

Repository is the way to go. I have used it multiple times when I had to work with Oracle and SQL databases. This might be useful for you: http://www.primaryobjects.com/CMS/Article108.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.