Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications and Services

The IDbTransaction Interface

Many of the methods exposed by a database provider are overloaded to allow for an implementation of the IDbTransaction interface. This enables the Data Access Application Block to perform multiple database operations within a single manual transaction. That is, it can ensure that either all database operations are successfully committed or none of the operations are. To provide support for this feature, any .NET managed data provider for use with the Data Access Application Block should implement the IDbTransaction interface. For the .NET managed data provider for XML files, the XmlFileTransaction class implements the IDbTransaction interface. Listing B.7 shows the code for this class.

Listing B.7. XmlFileTransaction Class Implements IDbTransaction

[C#] public class XmlFileTransaction : IDbTransaction { private XmlFileConnection oConnection; private IsolationLevel eIsoLevel; public XmlFileTransaction(XmlFileConnection _Connection) { if (_Connection == null) throw new ArgumentNullException(SR.Connection, SR.ConnectionNullError); oConnection = _Connection; } public XmlFileTransaction(XmlFileConnection oConnection, IsolationLevel _iso) : this(oConnection) { if (_iso.Equals(null)) throw new ArgumentNullException(SR.IsolationLevel, SR.IsolationLevelNullError); eIsoLevel = _iso; } //TODO: modify this so that it clears the data in cache. public void Rollback() { } //TODO: Modify this so that it writes out to the file. public void Commit() { } public IDbConnection Connection { get { return oConnection; } } public IsolationLevel IsolationLevel { get { return !(eIsoLevel.Equals(null))? eIsoLevel : new IsolationLevel(); } } public void Dispose() { } } [Visual Basic] Public Class XmlFileTransaction : Implements IDbTransaction Private oConnection As XmlFileConnection Private eIsoLevel As IsolationLevel Public Sub New(ByVal _Connection As XmlFileConnection) If _Connection Is Nothing Then Throw New ArgumentNullException(SR.Connection, _ SR.ConnectionNullError) End If oConnection = _Connection End Sub Public Sub New(ByVal oConnection As XmlFileConnection, _ ByVal _iso As IsolationLevel) Me.New(oConnection) If _iso.Equals(Nothing) Then Throw New ArgumentNullException(SR.IsolationLevel,_ SR.IsolationLevelNullError) End If eIsoLevel = _iso End Sub 'TODO: modify this so that it clears the data in cache. Public Sub Rollback() Implements IDbTransaction.Rollback End Sub 'TODO: Modify this so that it writes out to the file. Public Sub Commit() Implements IDbTransaction.Commit End Sub Public ReadOnly Property Connection() As IDbConnection _ Implements IDbTransaction.Connection Get Return oConnection End Get End Property Public ReadOnly Property IsolationLevel() As IsolationLevel _ Implements IDbTransaction.IsolationLevel Get Return IIf(Not(eIsoLevel.Equals(Nothing)), eIsoLevel, _ New IsolationLevel()) End Get End Property Public Sub Dispose() End Sub End Class

Категории