|
-
Aug 2nd, 2010, 11:11 AM
#1
Thread Starter
New Member
3 tiers problem
Im builtina an windows application based on 3 tiers model. I have BLL, DAL, and UI Class. IN DAL I have SQL code, In BLL I have proprietie and methods .
Im trying to validate the input fields on BLL class and no problem,and im just do it after SET instruction. I also PUT a message to view error. But From BLL class I want to put the focus in textfield that return error. I cant acess form proprieties (the forms are on UI class). IT is that possible , how...thanks
-
Aug 2nd, 2010, 12:04 PM
#2
Re: 3 tiers problem
How does your UI consume your business layer? Are you binding your UI controls to the BLL classes, or are you manually linking up the data from your classes into controls on the form?
One way to bring information like validation errors from the business layer to the UI is to raise events in the business layer that the UI will consume and be able to respond to.
-
Aug 2nd, 2010, 06:56 PM
#3
Thread Starter
New Member
Re: 3 tiers problem
SO, I will copy my code
1- DAL,
2-BLL
3-UI
Im using vs2008.
1 - MY DAL:
Namespace DAL.modelo
Public Class DAL_modelo
#Region "Private variables"
Private _conn_string As String = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=particular)(PORT=1521)))(CONNECT_DATA =(SERVER=DEDICATED)(SERVICE_NAME=xe)));User Id=gspt;Password=dihmlfyi"
Private _adap As New OracleDataAdapter
Private _conn As New OracleConnection
Private _comm As New OracleCommand
Private dt_retorno As Data.DataSet = Nothing ' As New Data.DataSet
Private map_retorno As String = Nothing
Private _estado As Boolean = Nothing
Private _retorno As Integer = Nothing
Private _transaction As OracleTransaction
#End Region
#Region "Public methods"
Public Function NovoModelo(ByVal modelo As String, ByVal EmUso As String) As Boolean
'Inicializar o estado da conexao
_estado = True
_retorno = -1
'configurar a conexao
_conn.ConnectionString = _conn_string
Try
'Abre a conexão
_conn.Open()
'Verifica estado da conexão
If (_conn.State = ConnectionState.Open) Then
'iniciar uma transacao local a bd
_transaction = _conn.BeginTransaction(IsolationLevel.ReadCommitted)
With _comm
.Connection = _conn
'.CommandType = CommandType.Text
.Transaction = _transaction
.CommandText = "insert into modelo " & _
"(IDMODELO,MODELO,EM_USO) VALUES " & _
"(modelo_seq.nextval,:modelo,'S')"
.Parameters.Add(":modelo", OracleType.VarChar).Value = modelo
Try
_retorno = .ExecuteNonQuery()
_transaction.Commit()
.Parameters.Clear()
Catch ex As Exception
_transaction.Rollback()
_estado = False
End Try
End With
Else
_estado = False
End If
Catch ex As Exception
'mensagem_de_exception(ex.Message)
_retorno = -1
End Try
'fecha a conexao
_conn.Close()
Return _estado
End Function
#End Region
End Class
2 - MY BLL:
Namespace BLL.modelo
Public Class bll_modelo
#Region "Variaveis"
Private _obj As DAL_modelo = Nothing
Private _modeloid As Integer
Private _modelo As String
Private _emuso As String
#End Region
#Region "Propriedades"
Public Property Modelo() As String
Get
Return _modelo
End Get
Set(ByVal value As String)
_modelo = value
'testa os campos
If _modelo = "" Then
Throw New Exception("Field cant be null")
HERE I WANT TO PUT THE FOCUS ON FIELD txtmodelo (txtmodelo.focus)
HOW DO it?
ElseIf Modelo = 1 Then
Throw New Exception("Invalid number...")
End If
End Set
End Property
Public Property EmUso() As String
Get
Return _emuso
End Get
Set(ByVal value As String)
_emuso = value
End Set
End Property
#End Region
#Region "Métodos e funções publicas"
Public Function NovoModelo() As Boolean
Dim _estado As Boolean
_estado = False
Try
_obj = New DAL_modelo
_estado = _obj.NovoModelo(Modelo, EmUso)
Catch ex As Exception
Throw ex
Finally
_obj = Nothing
End Try
Return _estado
End Function
#End Region
3 - UI (Form)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
_estado = False
'cria uma instância da classe BLLModelo
_objm = New bll_modelo
With _objm
'atribui os valores as propriedades do objeto
.Modelo = TxtModelo.Text
.EmUso = "S"
_estado = .NovoModelo()
End With
If (_estado) = True Then
MessageBox.Show("Model saved.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("error when save model", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Gspt", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
_objm = Nothing
End Try
Me.Cursor = Cursors.Default
End Sub
#End Region
-
Aug 2nd, 2010, 07:10 PM
#4
Thread Starter
New Member
Re: 3 tiers problem
 Originally Posted by kleinma
How does your UI consume your business layer? Are you binding your UI controls to the BLL classes, or are you manually linking up the data from your classes into controls on the form?
One way to bring information like validation errors from the business layer to the UI is to raise events in the business layer that the UI will consume and be able to respond to.
Here is the code in the form:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
_estado = False
'cria uma instância da classe BLLModelo
_objm = New bll_modelo
With _objm
'atribui os valores as propriedades do objeto
.Modelo = TxtModelo.Text
.EmUso = "S"
_estado = .NovoModelo()
End With
If (_estado) = True Then
MessageBox.Show("Saved with sucess.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("error saving", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Gspt", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
_objm = Nothing
End Try
Me.Cursor = Cursors.Default
End Sub
#End Region
2 - Here is code on BLL
Namespace BLL.modelo
Public Class bll_modelo
#Region "Variaveis"
Private _obj As DAL_modelo = Nothing
Private _modeloid As Integer
Private _modelo As String
Private _emuso As String
#End Region
#Region "Propriedades"
Public Property Modelo() As String
Get
Return _modelo
End Get
Set(ByVal value As String)
_modelo = value
'testa os campos
If _modelo = "" Then
Throw New Exception("field cant de null")
***HERE i want to focus fiels of UI (txtmodelo) , HOW acess form proprieties???
*** txtmodelo.focus ?????? Here???? HOW?
ElseIf Modelo = 1 Then
Throw New Exception("Introduza um valor válido")
End If
End Set
End Property
Public Property EmUso() As String
Get
Return _emuso
End Get
Set(ByVal value As String)
_emuso = value
End Set
End Property
#End Region
#Region "Métodos e funções publicas"
Public Function NovoModelo() As Boolean
Dim _estado As Boolean
_estado = False
Try
_obj = New DAL_modelo
_estado = _obj.NovoModelo(Modelo, EmUso)
Catch ex As Exception
Throw ex
Finally
_obj = Nothing
End Try
Return _estado
End Function
#End Region
-
Aug 2nd, 2010, 08:41 PM
#5
Re: 3 tiers problem
You should be handling validation in essentially the same way as you would in an application without tiers. You handle the Validating event of each control that needs validating and execute your validation logic in the event handler. If the data fails validation you set e.Cancel to True and the control won't lose focus. When it comes time to save you call ValidateChildren on the Form and it will raise the Validating event on every control, catching even those that haven't received focus.
In your case, the validation logic that you include in the event handler will consist of calling the appropriate method in the BLL, which will return True or False to indicate pass or fail. If there is some logic that doesn't fit into this pattern for some reason, put it into a special method that you can call immediately after ValidateChildren. Let it return one or more instances of a type that contains information about the field that failed and why. You can then use that data appropriately in your PL.
-
Aug 2nd, 2010, 11:20 PM
#6
Thread Starter
New Member
Re: 3 tiers problem
can you update my example to shoe me?
-
Aug 2nd, 2010, 11:40 PM
#7
Re: 3 tiers problem
Maybe you could try for yourself first, because there's no point my doing the whole thing for you if you can do most of it for yourself. I mentioned the Validating event and the ValidateChildren method. Have you done any research to find out what you can about them? Have you read the relevant documentation? Have you tested using them in a simple test application first? These are the sorts of things you should be doing for yourself first, then asking for help if you get stuck. Asking someone else to do it for you without making any sort of attempt is not the best way to learn.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|