The IDbConnection and ICloneable Interfaces


The IDbConnection and ICloneable Interfaces

Providing the ability to set connection and session information for a particular data source is vital to creating a comprehensive data provider. By implementing the IDbConnection interface, a .NET managed data provider can represent a unique session with a data source. Like the IDbCommand interface, some classes that implement the IDbConnection interface may define additional members that add provider-specific functionality. For example, the SqlConnection class defines the PacketSize property in addition to all the other inherited methods.

When designing the class that implements the IDbConnection interface, it is important to keep in mind that the Data Access Application Block needs for this class to also implement the ICloneable interface for dynamic discovery of parameters to work.

In the base DbCommandWrapper's DoDiscoverParameters method, the ICloneable's Clone method is used to make another copy of the IDbConnection to use for querying for the metadata in the data source. Although there's no reason to discover parameters for reading or writing to XML files, I have still implemented the ICloneable interface to be consistent for what you might need to do if you are creating your own .NET managed data provider. Listing B.5 illustrates how the XmlFileConnection class implements both the IDbConnection and ICloneable interfaces.

Listing B.5. XmlFileConnection Implements IDbConnection and ICloneable

[C#] public class XmlFileConnection : IDbConnection, ICloneable {     private string origConnString;     private string fileName;     private StringCollection parameters;     private ConnectionState state = ConnectionState.Closed;     private int connectionTimeout = 0;     public XmlFileConnection()     {     }     public XmlFileConnection(string strConnectionString)     {          ConnectionString = strConnectionString;     }     public IDbTransaction BeginTransaction(IsolationLevel iso)     {          return new XmlFileTransaction(this,iso);     }     public IDbTransaction BeginTransaction()     {          return new XmlFileTransaction(this);     }     public void ChangeDatabase(string newdb)     {          if (! newdb.Equals(fileName)) fileName = newdb;          return;     }     public void Close()     {          state = ConnectionState.Closed;          return;     }     public IDbCommand CreateCommand()     {          IDbCommand idbCommand = (IDbCommand)(new XmlFileCommand());          idbCommand.Connection = this;          return idbCommand;     }     public void Open()     {          state = ConnectionState.Open;     }     public string ConnectionString     {          get { return origConnString; }          set          {               origConnString = value;               fileName = String.Empty;               if (parameters == null)                    parameters = new StringCollection();               parameters.Clear();               string[] arrConnString =                    System.Text.RegularExpressions.Regex.Split                    (value,";,");               foreach (string strParam in arrConnString)               {                    if (fileName.Equals(String.Empty))                         fileName = strParam;                    else                         parameters.Add(strParam);               }          }     }     public int ConnectionTimeout     {          get { return connectionTimeout; }     }     public string Database     {          get { return fileName;     }     }     public ConnectionState State     {          get { return state;     }     }     public void Dispose() {}     public object Clone()     {          return new XmlFileConnection(ConnectionString);     } } [Visual Basic] Public Class XmlFileConnection : Implements IDbConnection, ICloneable     Private origConnString As String     Private fileName As String     Private parameters As StringCollection     Private state_Renamed As ConnectionState = ConnectionState.Closed     Private connectionTimeout_Renamed As Integer = 0     Public Sub New()     End Sub     Public Sub New(ByVal strConnectionString As String)          ConnectionString = strConnectionString     End Sub     Public Function BeginTransaction(ByVal iso As IsolationLevel) _          As IDbTransaction Implements IDbConnection.BeginTransaction               Return New XmlFileTransaction(Me,iso)     End Function     Public Function BeginTransaction() As IDbTransaction _               Implements IDbConnection.BeginTransaction          Return New XmlFileTransaction(Me)     End Function     Public Sub ChangeDatabase(ByVal newdb As String) _               Implements IDbConnection.ChangeDatabase          If (Not newdb.Equals(fileName)) Then               fileName = newdb          End If          Return     End Sub     Public Sub Close() Implements IDbConnection.Close          state_Renamed = ConnectionState.Closed          Return     End Sub     Public Function CreateCommand() As IDbCommand _               Implements IDbConnection.CreateCommand          Dim idbCommand As IDbCommand = _               CType(New XmlFileCommand(), IDbCommand)          idbCommand.Connection = Me          Return idbCommand     End Function     Public Sub Open() Implements IDbConnection.Open          state_Renamed = ConnectionState.Open     End Sub     Public Property ConnectionString() As String _               Implements IDbConnection.ConnectionString          Get               Return origConnString          End Get          Set               origConnString = Value               fileName = String.Empty               If parameters Is Nothing Then                    parameters = New StringCollection()               End If               parameters.Clear()               Dim arrConnString As String() = _                    System.Text.RegularExpressions.Regex.Split _                    (Value,";,")               For Each strParam As String In arrConnString                    If fileName.Equals(String.Empty) Then                         fileName = strParam                    Else                         parameters.Add(strParam)                    End If               Next strParam          End Set     End Property     Public ReadOnly Property ConnectionTimeout() As Integer _                Implements IDbConnection.ConnectionTimeout          Get               Return connectionTimeout_Renamed          End Get     End Property     Public ReadOnly Property Database() As String _               Implements IDbConnection.Database          Get               Return fileName          End Get     End Property     Public ReadOnly Property State() As ConnectionState _               Implements IDbConnection.State          Get               Return state_Renamed          End Get     End Property     Public Sub Dispose()     End Sub     Public Function Clone() As Object Implements ICloneable.Clone          Return New XmlFileConnection(ConnectionString)     End Function End Class




Fenster Effective Use of Microsoft Enterprise Library(c) Building Blocks for Creating Enterprise Applications and Services 2006
Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications and Services
ISBN: 0321334213
EAN: 2147483647
Year: 2004
Pages: 103
Authors: Len Fenster

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net