locked
Why to use Interface IDbConnection,IDbDataAdapter .Its real time use????

    Question

  • Hi,
    I could not understand real use of this syntax

    IDbConnection myConn = new SqlConnection();

     IDbConnection myConn = new OleDbConnection();

      IDbConnection myConn = new OdbcConnection();
     
    We can also use for SqlConnection
    System.Data.SqlClient's SqlConnection class .
    Then why to use Interfaces....
    Please give architectural benefit....any example will be more help full.
    regards
    ashok 

    Monday, May 02, 2005 6:26 PM

Answers

All replies

  • Let's say you had an application you wanted to deploy to several shops. You wouldn't want to assume they all had SQL Server, Oracle, MySQL, etc.

    You could de-couple the application from the database details by using the System.Data interfaces. For example...

    public abstract class DatabaseLayerBase
    {
      public abstract IDbConnection CreateConnection(String connectionString);
    }

    public class SqlClientDatabaseLayer : DatabaseLayerBase
    {
      public override IDbConnection CreateConnection(String connectionString)
      {
        return new SqlConnection(connectionString);
      }
    }


    public class OracleDatabaseLayer : DatabaseLayerBase
    {
      public override IDbConnection CreateConnection(String connectionString)
      {
        return new OracleConnection(connectionString);
      }
    }


    public class OdbcDatabaseLayer : DatabaseLayerBase
    {
      public override IDbConnection CreateConnection(String connectionString)
      {
        return new OdbcConnection(connectionString);
      }
    }

    public class ApplicationLayer
    {
      public void ConnectToDatabase()
      {
        IDbConnection connection = databaseLayerBase.CreateConneciton();
        connection.Open();
      }

      public void CreateDatabaseLayerInstance()
      {
        // Instanciate a concrete class by reading from a config file...
      }

      private DatabaseLayerBase databaseLayer;
    }

    It's a crude example, but hopefully you get the idea. Your application layer knows nothing about the kind of database it's interacting with. This means you can later talk to any other kind of database you want without changing your application layer.
    Monday, May 02, 2005 6:39 PM
  • If u declare your variable from a Interface u can assign anything which implements that interface...

    IDbConnection myConn = new SqlConnection();

    on here.. myConn can be OracleConnection, MySqlConnection... Why ?
    becouse they are implemented from IDbConnection. not just them.. maybe u can create your custom Connection class... if u implement from IDbConnection.. u can use your custom connection class like this..

    In OOP programming.. U really need interfaces.. and you use like this...
    If you look at design patterns of Gang of Four.. they use like this.

    so it is flexible using..

    Monday, May 02, 2005 6:54 PM
  • Humm...
    Its nice idea....
    encapsulationg the power fo oops and data tire and presentation tire sepration.
    I am fresher in .net so these basic questions..
    Thank u Ganr & Unqualex.
    Very soon I will implement this logic to my project as my project is suppose to face this situation.I will keep the code -seg for architectural verification..
    regards
    ashok
    Monday, May 02, 2005 7:43 PM
  • Hi Gary D
    While  I was going through your  code example I was confused by this segment

        IDbConnection connection = databaseLayerBase.CreateConneciton();

         private DatabaseLayerBase databaseLayer;

    here databaseLayerBase is an abstract class .So how we can create instance of it??
    will it would be
     either of any three..

    IDbConnection connection = sqlClientDatabaseLayer.CreateConneciton(connectionString);
    private  SqlClientDatabaseLayer sqlClientDatabseLayer...
    Please check it out...
    As I will have to implement the block...
    regards
    ashok

    Monday, May 02, 2005 8:09 PM
  • Hi Ashok,

    The implementation is called the Factory Design Pattern.

    Some articles which cover the same topic:
    Writing a Generic Data Access Component
    http://www.c-sharpcorner.com/Code/2002/July/GenericDataProvider.asp

    A Generic Data Access Component using Factory Pattern
    http://www.c-sharpcorner.com/Code/2002/July/GenericDataAccessCompActivator.asp

    Regards,
    Vikram
    Tuesday, May 03, 2005 4:22 PM
  • In the context of this dialogue, what is the advantage of using the IDbConnection interface for implementing, rather than the System.Data.Common.DbConnection base class for inheriting?

    Friday, December 09, 2011 10:32 PM