Implementing a Custom Connection

A connection is an abstraction of a physical link between the data provider and the source of the data itself. In most cases, that means a network connection between the client application code and the database server code (such as a TCP/IP connection on port 1560 for SQL Server, and the like).

In the case of this sample remote data provider, the connection is an implied or logical connection rather than a physical connection. As you know, HTTP is a stateless protocol. When you request information from a web server, the connection is opened at the time of the request, not ahead of time as with database connections.

For this reason, the connection being implemented is just a storage place for the connection string, and a state machine that indicates whether the connection is open or closed (although it has no real impact).

The IDbConnection Interface

The IDbConnection interface defines the properties and methods given in Table 28.3.

Table 28.3. Properties and Methods of the IDbConnection Interface

Property or Method



The connection string used to connect to the database (or web service, in the case of this example).


The timeout period for establishing a connection.


The name of the database to which the connection is attached. This property is irrelevant for the sample provider in this chapter, so it will always be the empty string.


The state of the connection (for example, Open, Closed, Closing, and so on).


Begins a new transaction on the connection.


Forces the connection to point to a different database within the same context. This method won't be supported by the RDP.


Closes the connection to the database.


Creates a new command object that is pre-associated with this connection.


Opens the connection.

The RDPConnection Class

The code in Listing 28.3 illustrates the RDPConnection class. Note that no physical or networking connections are made. All that's done is store a connection string that will be used by the RDPCommand object to issue web service method invocations.

Listing 28.3. The Remote Data Provider Connection
 using System; using System.Data; namespace SAMS.CSharpUnleashed.RemoteDataProvider {   public class RDPConnection : IDbConnection   {     private ConnectionState state;     private string connectionString;     public RDPConnection()     {       state = ConnectionState.Closed;       connectionString = "";     }     public RDPConnection( string connString )     {       state = ConnectionState.Closed;       connectionString = connString;     }     #region IDbConnection Members     public void ChangeDatabase(string databaseName)     {       throw new NotSupportedException("RDP does not dynamic change of Database.");     }     public IDbTransaction BeginTransaction(System.Data.IsolationLevel il)     {       throw new NotSupportedException("Transactions not supported in RDP.");     }     IDbTransaction System.Data.IDbConnection.BeginTransaction()     {       throw new NotSupportedException("Transactions not supported in RDP.");     }     public System.Data.ConnectionState State     {       get       {         return state;       }     }     public string ConnectionString     {       get       {          return connectionString;       }       set       {          connectionString = value;       }     }     IDbCommand IDbConnection.CreateCommand()     {       RDPCommand cmd = new RDPCommand();       cmd.Connection = this;       return (IDbCommand)cmd;     }     public RDPCommand CreateCommand()     {       RDPCommand cmd = new RDPCommand();       cmd.Connection = this;       return cmd;     }     public void Open()     {       state = ConnectionState.Open;     }     public void Close()     {       state = ConnectionState.Closed;     }     public string Database     {       get       {          return "";       }     }     public int ConnectionTimeout     {       get       {         return 0;       }     }     #endregion     #region IDisposable Members     public void Dispose()     {       this.Dispose(true);       System.GC.SuppressFinalize(this);     }     private void Dispose( bool disposing )     {        if (state == ConnectionState.Open)          Close();     }     #endregion   } } 

    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316 © 2008-2017.
    If you may any questions please contact us: