Home / ASP.NET Wiki / Architecture / Design Patterns / Factory / Provider Factory

Provider Factory

 Rate It (1)

Providers factory provides us indepandent database access where we can connect to any database sources(SQLServer,DB2,Oracle,MS-Access).
Provider Factory allows programmers to write their own implementation for accessing database.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

namespace DatabaseProvider
{
    /// <summary>
    ///
    /// Design Pattern: Factory.
    /// </summary>
    public abstract class Database
    {
        private string connectionString;       
        private DbProviderFactory dbProviderFactory;
        private string schema;
        private string dataProvider;
        private bool status;

        protected Database() { }    

        public DbProviderFactory DbProviderFactory
        {
            get { return dbProviderFactory; }
        }      
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }       
        public string Schema
        {
            get { return schema; }
        }

        public string DataProvider
        {
            get { return dataProvider; }
        }
           
        public DbConnection GetConnection()
        {
            DbConnection newConnection = null;
            try
            {
                try
                {
                       ProviderFactory = DbProviderFactories.GetFactory(this.ProviderType);
                       dbConnection = ProviderFactory.CreateConnection();
                       dbConnection.ConnectionString = this.ConnectionString;                    
                }
                catch
                {
                    throw;
                }
            }
            catch
            {
                if (newConnection != null)
                    newConnection.Close();

                throw;
            }

            return newConnection;
        }

        public DbCommand CreateCommand()
        {
            return dbProviderFactory.CreateCommand();
        }

        public DbCommand CreateCommand(string sQueryString, DbConnection connection)
        {
            try
            {
                // Create the DbCommand.
                DbCommand command = this.CreateCommand();
                command.CommandText = sQueryString;
                command.Connection = connection;

                return command;
            }
            catch
            {
                throw;
            }
        }      

        public DbDataAdapter CreateDataAdapter()
        {
            return dbProviderFactory.CreateDataAdapter();
        }
        public DbDataAdapter CreateDataAdapter(string sQueryString, DbConnection connection)
        {
            try
            {
                // Create the DbCommand.
                DbCommand command = this.CreateCommand();
                command.CommandText = sQueryString;
                command.Connection = connection;

                // Create the DbDataAdapter.
                DbDataAdapter adapter = this.CreateDataAdapter();
                adapter.SelectCommand = command;
               
                return adapter;
            }
            catch
            {
                throw;
            }
        }

        private DbCommandBuilder CreateCommandBuilder()
        {
            return dbProviderFactory.CreateCommandBuilder();
        }

        public DbCommandBuilder CreateCommandBuilder(DbDataAdapter dbDA)
        {
            DbCommandBuilder dbCB = this.CreateCommandBuilder();
            dbCB.DataAdapter = dbDA;

            return dbCB;
        }       
    }
}

web.config
<add key="Connectionstring" value="database=local;user id=sa;pwd=sa;initial catalog=northwind"/>
<add key="SQLProvider" value="System.Data.SqlClient"/>
<add key="OledbProvider" value="System.Data.OleDb"/>
<add key="Db2Provider" value="IBM.Data.DB2"/>
<add key="OracleProvider" value="System.Data.OracleClient"/>

aspx.cs
private Database DB;
private DbDataAdapter SQLDA;
private DbCommandBuilder SQLDB;
private DbConnection Conn;
private DBCommand cmd;

//pass the connection string and provider type to create database connection
DB.Connectionstring=Configuration.ConfigurationManager.AppSettings["Connectionstring"].ToString();
DB.DataProvider=Configuration.ConfigurationManager.AppSettings["SQLProvider"].ToString();

//create database connection
Conn = DB.GetConnection();
Conn.Open();

//creating Data Adapter
SQLDA=DB.CreateDataAdapter("select * from emp",Conn);

//Create Command Builder
SQLDB=DB.CreateCommandBuilder(SQLDA);

//Create Command
cmd=DB.CreateCommand("select * from emp",Conn);

Revision number 1, Friday, February 12, 2010 8:07:27 AM by beharavenkata@gmail.com
This is not the most up to date version of this article. The most recent version can be found here.

Comments

Related Articles

Abstract Factory

Abstract FactoryThe Gang of Four defintion for this design pattern is: "Provide an interface for creating families of related or dependant objects without specifying their concrete classes.".A VB example of the Abstract Factory Design Pattern'

Design Patterns

Design Patterns Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups

Architecture

MSDN has an excellent section on Architecting ASP.NET Applications. patterns & practices' Web Client Software Factory on CodePlex!- This factory will provide comprehensive architecture guidance to help customers build web solutions using the Microsoft

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. francissvk (1)
  2. deepeshsp (1)