Proxy Para Enterprise Library- Application Block 3.0

Com a nova versão do Application Block ganhamos uma grande facilidade para desenvolver aplicações multibanco , mas voltamos a ter o problema de várias linhas de código para executar um comando.
 
Para retornar um Dataset é necessário:
 
        Dim dsRetorno As DataSet
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = txtProcedure
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        Try
            dsRetorno = db.ExecuteDataSet(dbCommand)
        Catch ex As Exception
            Throw (ex)
        Finally
            db = Nothing
            dbCommand.Dispose()
        End Try
        Return dsRetorno
 
 
Como sou muito preguiçoso acabei desenvolvendo uma classe "proxy" , para voltar ao bom e velho uma linha para executar comandos simples.
 
 
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Text
Imports System.Data.Common
Public Class clsDataBase
    Private Shared Sub criarParametro(ByVal arrParametros As clsParametro(), ByVal db As Database, ByVal dbCommand As DbCommand, ByVal linha As Int32)
       
        For linha = 0 To arrParametros.GetUpperBound(0)
            db.AddInParameter(dbCommand, arrParametros(linha).Nome, arrParametros(linha).TipoParametro, arrParametros(linha).Valor)
        Next
    End Sub
    Shared Function retornarScalar _
                (ByVal txtProcedure As String, ByVal arrParametros() As clsParametro) _
                As String
        Dim txtRetorno As String
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = txtProcedure
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        Dim linha As Int32 = 0
        Try
            criarParametro(arrParametros, db, dbCommand, linha)
            txtRetorno = db.ExecuteScalar(dbCommand)
        Catch ex As Exception
            txtRetorno = ex.Message
        Finally
            db = Nothing
            dbCommand.Dispose()
        End Try
        Return txtRetorno
    End Function
    Shared Function retornarDataReader _
    (ByVal txtProcedure As String, ByVal arrParametros() As clsParametro) _
    As IDataReader
        Dim dtrRetorno As IDataReader
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = txtProcedure
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        Dim linha As Int32 = 0
       
        Try
            criarParametro(arrParametros, db, dbCommand, linha)
            dtrRetorno = db.ExecuteReader(dbCommand)
        Catch ex As Exception
            Throw (ex)
        Finally
            db = Nothing
        End Try
        Return dtrRetorno
    End Function
    Shared Function retornarDataTable _
    (ByVal txtProcedure As String, ByVal arrParametros() As clsParametro) _
    As DataTable
        Dim dtrRetorno As DataTable
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = txtProcedure
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        Dim linha As Int32 = 0
       
        Try
            criarParametro(arrParametros, db, dbCommand, linha)
            dtrRetorno = db.ExecuteDataSet(dbCommand).Tables(0)
        Catch ex As Exception
            Throw (ex)
        Finally
            db = Nothing
            dbCommand.Dispose()
        End Try
        Return dtrRetorno
    End Function
    Shared Function retornarDataSet _
        (ByVal txtProcedure As String, ByVal arrParametros() As clsParametro) _
        As DataSet
        Dim dtrRetorno As DataSet
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = txtProcedure
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        Dim linha As Int32 = 0
        Try
            criarParametro(arrParametros, db, dbCommand, linha)
            dtrRetorno = db.ExecuteDataSet(dbCommand)
        Catch ex As Exception
            Throw (ex)
        Finally
            db = Nothing
            dbCommand.Dispose()
        End Try
        Return dtrRetorno
    End Function
 
    Shared Function retornarScalar _
            (ByVal txtProcedure As String) _
            As String
        Dim txtRetorno As String
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = txtProcedure
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        Dim linha As Int32 = 0
 
        Try
            txtRetorno = db.ExecuteScalar(dbCommand)
        Catch ex As Exception
            txtRetorno = ex.Message
        Finally
            db = Nothing
            dbCommand.Dispose()
        End Try
        Return txtRetorno
    End Function
 
    Shared Function retornarDataReader _
    (ByVal txtProcedure As String) _
    As IDataReader
        Dim dtrRetorno As IDataReader
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = txtProcedure
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        Dim linha As Int32 = 0
 
        Try
            dtrRetorno = db.ExecuteReader(dbCommand)
        Catch ex As Exception
            Throw (ex)
        Finally
            db = Nothing
        End Try
        Return dtrRetorno
    End Function
    Shared Function retornarDataTable _
    (ByVal txtProcedure As String) _
    As DataTable
        Dim dtrRetorno As DataTable
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = txtProcedure
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        Dim linha As Int32 = 0
        Try
            dtrRetorno = db.ExecuteDataSet(dbCommand).Tables(0)
        Catch ex As Exception
            Throw (ex)
        Finally
            db = Nothing
            dbCommand.Dispose()
        End Try
        Return dtrRetorno
    End Function
    Shared Function retornarDataSet _
        (ByVal txtProcedure As String) _
        As DataSet
        Dim dtrRetorno As DataSet
        Dim db As Database = DatabaseFactory.CreateDatabase()
        Dim sqlCommand As String = txtProcedure
        Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
        Try
            dtrRetorno = db.ExecuteDataSet(dbCommand)
        Catch ex As Exception
            Throw (ex)
        Finally
            db = Nothing
            dbCommand.Dispose()
        End Try
        Return dtrRetorno
    End Function
 
End Class

Criei outra classe para os parametros

 

Imports System.Data.Common
Imports System

Public Class clsParametro

    Private _TipoParametro As DbType
    Public Property TipoParametro() As DbType
        Get
            Return _TipoParametro
        End Get
        Set(ByVal value As DbType)
            _TipoParametro = value
        End Set
    End Property

    Private _Valor As Object
    Public Property Valor() As Object
        Get
            Return _Valor
        End Get
        Set(ByVal value As Object)
            _Valor = value
        End Set
    End Property

    Private _NomeProcedure As String
    Public Property Nome() As String
        Get
            Return _NomeProcedure
        End Get
        Set(ByVal value As String)
            _NomeProcedure = value
        End Set
    End Property

    Sub New(ByVal txtNome As String, ByVal Tipo As DbType, ByVal objValor As Object)

        Nome = txtNome
        TipoParametro = Tipo
        Valor = objValor

    End Sub

End Class

Utilizando essas classes voltamos a facilidade do Application Block 2.0, veja como fica mais simples .

        DataTable dtbRetorno;
        BD.clsParametro[] arrParametro = new BD.clsParametro[11];

        arrParametro[0] = new BD.clsParametro("@IDSENHA", DbType.Int32, IDSENHA);
        arrParametro[1] = new BD.clsParametro("@IDEMPRESA", DbType.Int32, IDEMPRESA);
        arrParametro[2] = new BD.clsParametro("@IDVENDEDOR_DESTINO", DbType.Int32, IDVENDEDOR_DESTINO);
        arrParametro[3] = new BD.clsParametro("@IDOS", DbType.Int32, IDOS);
        arrParametro[4] = new BD.clsParametro("@NR_ITEM", DbType.Int16, NR_ITEM);
        arrParametro[5] = new BD.clsParametro("@NR_SERIE", DbType.String, NR_SERIE);
        arrParametro[6] = new BD.clsParametro("@OBS_MOVTO", DbType.String, OBS_MOVTO);
        arrParametro[7] = new BD.clsParametro("@IDSTATUSOS_NOVO", DbType.Int32, 0); 
        arrParametro[8] = new BD.clsParametro("@VENDEDOR_ORIGINAL_OS", DbType.String, "F"); 
        arrParametro[9] = new BD.clsParametro("@FINALIZAR_OS", DbType.String, FINALIZAR_OS); 
        arrParametro[10] = new BD.clsParametro("@CLIENTE_VISUALIZA_OBS", DbType.String, "T");

       dtbRetorno = BD.clsDataBase.retornarDataTable("MANUT_OS_ENCAMINHAR", arrParametro);

No webconfig eu configuro a conexão com o banco,

 <connectionStrings>
  <add name="FireBird" providerName="FirebirdSql.Data.FirebirdClient" connectionString="Server=localhost;User=XXXX;Password=XXXXX;Charser=WIN1252;Database=c:\XXXXX.GDB"/>
 </connectionStrings>
 <dataConfiguration defaultDatabase="FireBird"/>

Assim eu continuo com o poder do novo Application Block sem perder a facilidade do antigo…

 

 

 

Anúncios

2 comentários sobre “Proxy Para Enterprise Library- Application Block 3.0

  1. Jhonathas disse:

    Será que consigo rodar  o Proxy Para Enterprise Library- Application Block 3.0 no Visual Studio Codename Orcas Beta 2 ?

  2. Marcelo disse:

    Provavelmente você teria que pegar os fontes do Aplication Block e compilar no Orcas, mas talvez no orcas não precisaremos usar nem o application block …  

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. Sair / Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair / Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair / Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair / Alterar )

Conectando a %s