Implementing a Custom ConnectionA 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 InterfaceThe IDbConnection interface defines the properties and methods given in Table 28.3. The RDPConnection ClassThe 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 Connectionusing 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 } } ![]() |