Thursday, August 4, 2011

Data Access Layer using DbProviderFactory

 Hi,

This article is basically intended for the beginners or intermediate level of C# developers who loves 3 tier Architecture in their app. This is a wrapper class called DbHelper which can interact with any database , supported by ADO.Net framework.

Name spaces that are used by the various classes in this DbHelper class.
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
 
 This is mine custom NameSpace. You can replace it with your own. The name space has a class DbHelper which is implementing IDiposible interface , as I prefer using{ } block in my code. Why using, because the open parentheses and closing parentheses includes try - catch and calls dispose automatically. Some private members like DbConnection and DbProviderFactory are declared. Here we are using DbProviderFactory as Data providers with registered factory classes in the .NET Framework include System.Data.Odbc, System.Data.OleDb, System.Data.SqlClient, System.Data.SqlServerCe, and System.Data.OracleClient. For more details on DbProviderFactory have MSDN . Below is the entire code. I know one thing i am missing here is exception handling. Yes its true, as i generally catches exception in business layer or presentation and there I use to log it. Instead of placing try catch in each level, I use a single try-catch at presentation layer , so that I can have entire stack trace.
namespace ARSoftSys.DataAccessWrapper
{
    public sealed class DbHelper : IDisposable
    {
        #region PRIVATE MEMBER VARIABLES


        private static DbConnection _connection;
        private static bool _dispose;
        private static DbProviderFactory _provider;

        #endregion

        ///

        /// contructor, call method to open connection
        ///

        public DbHelper()
        {
            _connection = CreateConnection();
        }

        ///
        /// retrieves connection string from config
        ///

        private static string ConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; }
        }

        ///
        /// retrieves provider from connection string
        ///

        private static string ProviderName
        {
            get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName; }
        }

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion

        ///
        /// Destructor of the class. Calls method close connection that will automatically
        /// closes connection and calls class , dispose method
        ///

        ~DbHelper()
        {
            CloseConnection();
            Dispose();
        }


        // Given a provider name and connection string,
        // create the DbProviderFactory and DbConnection. Returns a DbConnection on success; null on failure.
        private static DbConnection CreateConnection()
        {
            // Assume failure.
            DbConnection connection = null;

            // Create the DbProviderFactory and DbConnection.
            if (ConnectionString != null)
            {
                try
                {
                    _provider = DbProviderFactories.GetFactory(ProviderName);

                    connection = _provider.CreateConnection();
                    connection.ConnectionString = ConnectionString;
                    connection.Open();
                }
                catch (Exception ex)
                {
                    // Set the connection to null if it was created.
                    if (connection != null)
                    {
                        connection = null;
                    }
                    Common.LogError(ex);
                }
            }
            else
            {
                throw new NullReferenceException(
                    "Invalid or missing connection string . Check if it exists in configuration file.");
            }

            // Return the connection.
            return connection;
        }


        private static void CloseConnection()
        {
            if (_connection.State == ConnectionState.Open)
            {
                _connection.Close();
                _connection.Dispose();
                _provider = null;
            }
        }


        ///
        ///
        ///

        ///
        ///
        public DbDataReader ExecuteSelect(string commandText)
        {
            return ExecuteSelect(commandText, CommandType.Text, null);
        }

        ///
        ///
        ///

        ///
        ///
        ///
        public DbDataReader ExecuteSelect(string commandText, CommandType cmdType)
        {
            return ExecuteSelect(commandText, cmdType, null);
        }


        ///
        /// Accepts command text , command type and database parameters
        /// as parameters and retrieves the table
        ///

        /// Select statement
        /// Type of command
        /// Database parameters
        ///
        public DbDataReader ExecuteSelect(string commandText, CommandType commandType, object parameters)
        {
            DbDataReader reader = null;

            // Check for valid DbConnection.
            if (_connection != null)
            {
                using (_connection)
                {
                    try
                    {
                        // Create the command.
                        var command = _connection.CreateCommand();
                        command.CommandText = commandText;
                        command.CommandType = commandType;
                        if (parameters != null)
                            command.Parameters.Add(parameters);
                        // Open the connection.
                        _connection.Open();

                        // Retrieve the data.
                        reader = command.ExecuteReader();
                        command.Parameters.Clear();
                    }

                    catch (DbException ex)
                    {
                        Common.LogError(ex);
                        throw;
                    }
                }
            }

            return reader;
        }

        ///
        ///
        ///

        ///
        ///
        public DataSet ExecuteSelectDataSet(string commandText)
        {
            return ExecuteSelectDataSet(commandText, CommandType.Text, null);
        }

        ///
        ///
        ///

        ///
        ///
        ///
        public DataSet ExecuteSelectDataSet(string commandText, CommandType cmdType)
        {
            return ExecuteSelectDataSet(commandText, cmdType, null);
        }


        ///
        /// Accepts command text , command type and database parameters
        /// as parameters and retrieves the table
        ///

        /// Select statement
        /// Type of command
        /// Database parameters
        ///
        public DataSet ExecuteSelectDataSet(string commandText, CommandType commandType, object parameters)
        {
            DataSet ds = null;

            // Check for valid DbConnection.
            if (_connection != null)
            {
                using (_connection)
                {
                    try
                    {
                        // Create the command.
                        var command = _connection.CreateCommand();
                        command.CommandText = commandText;
                        command.CommandType = commandType;
                        if (parameters != null)
                            command.Parameters.Add(parameters);
                        // Open the connection.
                        _connection.Open();
                        using (DbDataAdapter adapter = _provider.CreateDataAdapter())
                        {
                            adapter.SelectCommand = command;
                            adapter.Fill(ds);
                        }
                        command.Parameters.Clear();
                    }

                    catch (DbException ex)
                    {
                        Common.LogError(ex);
                        throw;
                    }
                }
            }

            return ds;
        }

        ///
        ///
        ///

        ///
        ///
        public object ExecuteScalar(string commandText)
        {
            return ExecuteScalar(commandText, CommandType.Text, null);
        }

        ///
        ///
        ///

        ///
        ///
        ///
        public object ExecuteScalar(string commandText, CommandType cmdType)
        {
            return ExecuteScalar(commandText, cmdType, null);
        }


        ///
        /// Accepts command text , command type and database parameters
        /// as parameters and retrieves the table
        ///

        /// Select statement
        /// Type of command
        /// Database parameters
        ///
        public object ExecuteScalar(string commandText, CommandType commandType, object parameters)
        {
            object obj = null;

            // Check for valid DbConnection.
            if (_connection != null)
            {
                using (_connection)
                {
                    try
                    {
                        // Create the command.
                        DbCommand command = _connection.CreateCommand();
                        command.CommandText = commandText;
                        command.CommandType = commandType;
                        if (parameters != null)
                            command.Parameters.Add(parameters);
                        // Open the connection.
                        _connection.Open();
                        obj = command.ExecuteScalar();
                        command.Parameters.Clear();
                    }

                    catch (DbException ex)
                    {
                        Common.LogError(ex);
                        throw;
                    }
                }
            }

            return obj;
        }

        ///
        ///
        ///

        ///
        ///
        public object ExecuteNonQuery(string commandText)
        {
            return ExecuteNonQuery(commandText, CommandType.Text, null);
        }

        ///
        ///
        ///

        ///
        ///
        ///
        public object ExecuteNonQuery(string commandText, CommandType cmdType)
        {
            return ExecuteNonQuery(commandText, cmdType, null);
        }


        ///
        /// Accepts command text , command type and database parameters
        /// as parameters and retrieves the table
        ///

        /// Select statement
        /// Type of command
        /// Database parameters
        ///
        public int ExecuteNonQuery(string commandText, CommandType commandType, object[] parameters)
        {
            int rowsAffected = 0;

            try
            {
                // Create the command.
                DbCommand command = _connection.CreateCommand();
                command.CommandText = commandText;
                command.CommandType = commandType;
                if (parameters != null)
                    command.Parameters.AddRange(parameters);
                rowsAffected = command.ExecuteNonQuery();
                command.Parameters.Clear();
            }

            catch (DbException ex)
            {
                Common.LogError(ex);
                throw;
            }

            return rowsAffected;
        }

        private static void Dispose(bool disposing)
        {
            if (_dispose) return;
            if (disposing)
            {
                if (_connection != null)
                {
                    _provider = null;
                    _connection.Dispose();
                }
            }

            _dispose = true;
        }
    }
}


Finally, please provide your valuable suggestions to make it more useful and more flexible.


Configuring Sify Broadband with your Wireless Router

Recently , I switched from my Reliance 3G data card to Sify broadband internet connection. Because of reliance poor customer service and the most important pathetic 3G speed. It was merely  between 30-60Kbps., where they were claiming it as 21Mbps. Sify cable connection is quite good as compared to it.


Anyways coming to the topic, the Sify technicians configured it via my LAN card and they left. They I thought it to connect via Wireless Modem but I failed to do so, because Sify verifies your MAC Address then allows you to connect. If it varies then you cant . That its a good part and bad as well. Good why??? because , even if some body have your user id and password he wont be able to connect. Bad because if you need to use computer other than registered , you cant????


So here are the steps to set up WiFi using any Wireless Router. 

Hardware Requirements
1. Wireless Router (Mine is Belkin G Wireless Router)
2. Laptop
3. Sify Connection

Sify's connection type is basically Cabel Modem type. As there are some local server that authenticates you. So you need a router that has Cable Modem support (generally all routers have this).
1. Switch on the router.
2. Plug the Sify LAN cable to your Router.
3. Connect your laptop using WiFi to router using 192.168.1.1 or 192.168.2.1 (type in browser address bar and press enter) , or as provided in your router manual.
4. In the Connection Type opt Static Connection.
5. Fill all the parameters , IP address, Gateway and DNS. Do not forget to add DNS other wise you will not be able to connect. Generally they provide 10.x.x.x
6. Restart your router and then check whether you are connected to internet or not. Routers generally have fields that display whether they are connected or not.
7. Definitely you will not be connected as the Sify technician has configured it on LAN and sify has got your LAN mac address. So either you need to clone the MAC address of LAN if your router has functionality. Otherwise there is a tool called Technitium MAC Address Changer . This will help in changing you MAC address. You need to make the LAN MAC as WiFi MAC and LAN MAC to some other. otherwise
8. The Last resort , call sify customer care . They will ask some question for verification purpose and release your current MAC from their address table.
9. Do the step 8 before start following this procedure. Otherwise you need to call the sify again. Also not you can register at max of 2 MAC with Sify at a time.

In case any help required please leave a comment.

Happy Surfing