martes, 16 de diciembre de 2008

Dal.Core.ObjetoBase.vb

Namespace Dal
  Public MustInherit Class ObjetoBase : Implements IDisposable
    Public ConnectionProvider As Dal.ConnectionProviderDelegate = Nothing
    Public TransactionProvider As Dal.TransactionProviderDelegate = Nothing

#Region " Constructores "
    Public Sub New()
    End Sub
    Public Sub New(ByVal conexion_ As IDbConnection)
      _ConexionActual = conexion_
    End Sub

    Public Sub New(ByVal connectionProvider As ConnectionProviderDelegate, ByVal transactionProvider As TransactionProviderDelegate)
      Me.ConnectionProvider = connectionProvider
      Me.TransactionProvider = transactionProvider
    End Sub
#End Region


    ' Destructor
    Protected Overrides Sub Finalize()
      Dispose(False)
    End Sub

    Public Sub Dispose() Implements System.IDisposable.Dispose
      Dispose(True)
      GC.SuppressFinalize(True)
    End Sub

    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
      If ConnectionProvider IsNot Nothing Then Return
      If disposing = False Then _ConexionActual = Nothing : Return
      If _ConexionActual Is Nothing Then Return
      If ConnectionProvider Is Nothing Then
        _ConexionActual.Close()
        _ConexionActual.Dispose()
        _ConexionActual = Nothing
      End If
    End Sub


    Private _ConexionActual As IDbConnection = Nothing
    Protected ReadOnly Property ConexionEnUso() As IDbConnection
      Get
        ' Primero se intenta usar la conexion asignada al objeto
        If _ConexionActual IsNot Nothing Then Return _ConexionActual
        ' Luego se intenta usar el proveedor de conexion
        If Me.ConnectionProvider IsNot Nothing Then
          _ConexionActual = ConnectionProvider.Invoke()
          Return _ConexionActual
        End If
        ' Si la conexion compartida no existe se crea una nueva (normalmente para uso con WebForm)
        If DataServer.Connection Is Nothing Then
          _ConexionActual = DataServer.GetNewConection()
          Return _ConexionActual
        End If
        Return DataServer.Connection() ' Se devuelve la connexion compartida (Esta ser� la usada normalmente por WindowsForm)
      End Get
    End Property

    Private _Transaction As IDbTransaction = Nothing
    Protected ReadOnly Property TransaccionEnUso() As IDbTransaction
      Get
        ' Primero se intenta usar la _Transaction asignada al objeto
        If _Transaction IsNot Nothing Then Return _Transaction
        ' si hay un delegado lo llamamos
        If TransactionProvider IsNot Nothing Then
          _Transaction = TransactionProvider.Invoke()
          Return _Transaction
        End If
        ' Se devuelve la transaccion compartida
        Return DataServer.Transaction
      End Get
    End Property

    Protected Function CreateCommand() As System.Data.IDbCommand
      Dim cmd As IDbCommand = ConexionEnUso.CreateCommand()
      cmd.Connection = ConexionEnUso
      cmd.Transaction = TransaccionEnUso
      Return cmd
    End Function

  End Class

End Namespace

No hay comentarios: