Αγαπητοί φίλοι ,
θέλω να μου πείτε τη γνώμη σας για το interface που θα δείτε παρκάτω καθώς και τα implementations του.
Θέλω να φτιάξω ένα mid-tier που να υποστηρίζει ένα μεγάλο αριθμό datasources. Είμαι στο σωστό δρόμο;
Για αρχή SQL Server (με serviced component implementation) + MSAccess
Imports System.EnterpriseServices
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Namespace SINPRepMid
Public Interface IMid
Inherits IDisposable
Function GetData(ByVal ConString As String) As DataSet
End Interface
<Description("Mid Tier Interface Test component handling the various data selecting functions"), _
Transaction(TransactionOption.NotSupported), _
ObjectPooling(True, 1, 5), _
JustInTimeActivation(True)> _
Public Class SMid
Inherits ServicedComponent
Implements IMid
Public Function GetData(ByVal ConString As String) As DataSet Implements IMid.GetData
Dim MyCon As SqlConnection
Dim MyAdapter As SqlDataAdapter
Dim MyData As DataSet
Try
MyCon = New SqlConnection(ConString)
MyAdapter = New SqlDataAdapter("Select * From tblTest", MyCon)
MyData = New DataSet
MyAdapter.Fill(MyData, "Test")
If MyData.Tables(0).Rows.Count = 0 Then
Throw New SystemException("No SQL SERVER data found")
End If
Return MyData
Finally
If Not MyCon Is Nothing Then
With MyCon
.Close()
.Dispose()
End With
End If
If Not MyAdapter Is Nothing Then
MyAdapter.Dispose()
End If
If Not MyData Is Nothing Then
MyData.Dispose()
End If
End Try
End Function
End Class
Public Class AMid
Implements IMid
Implements IDisposable
Private disposed As Boolean
Public Sub New()
disposed = False
End Sub
Public Function GetData(ByVal ConString As String) As DataSet Implements IMid.GetData
Dim MyCon As OleDbConnection
Dim MyAdapter As OleDbDataAdapter
Dim MyData As DataSet
Try
MyCon = New OleDbConnection(ConString)
MyAdapter = New OleDbDataAdapter("Select * From tblTest", MyCon)
MyData = New DataSet
MyAdapter.Fill(MyData, "Test")
If MyData.Tables(0).Rows.Count = 0 Then
Throw New SystemException("No MSAccess data found")
End If
Return MyData
Finally
If Not MyCon Is Nothing Then
With MyCon
.Close()
.Dispose()
End With
End If
If Not MyAdapter Is Nothing Then
MyAdapter.Dispose()
End If
If Not MyData Is Nothing Then
MyData.Dispose()
End If
End Try
End Function
Public Sub Dispose() Implements IDisposable.Dispose
If Not disposed Then
disposed = True
GC.SuppressFinalize(Me)
End If
End Sub
Protected Overrides Sub Finalize()
Dispose()
End Sub
End Class
End Namespace
Τα καλώ από μία win εφαρμογή στα αντίστοιχα button clicks και γεμίζω ένα grid :
Public Function GetData(ByVal MyOb As SinpMidTier.SINPRepMid.IMid, _
ByVal ConString As String) As DataSet
Dim TempData As New DataSet
Try
With MyOb
TempData = .GetData(ConString)
.Dispose()
End With
Return TempData
Finally
If Not TempData Is Nothing Then
TempData.Dispose()
End If
End Try
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim MyOb As New SinpMidTier.SINPRepMid.AMid
Try
grdTest.DataSource = Nothing
Application.DoEvents()
With grdTest
.DataSource = GetData(MyOb, "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\" & _
"Databases\Test.mdb;" )
End With
Catch ex As Exception
MsgBox(ex.Message.ToString, _
MsgBoxStyle.Critical, _
"Error")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyOb As New SinpMidTier.SINPRepMid.SMid
Try
grdTest.DataSource = Nothing
Application.DoEvents()
With grdTest
.DataSource = GetData(MyOb, "Server=TESTSERVER;Network Library=DBMSSOCN;" & _
"Initial Catalog=TESTDB;Integrated Security=SSPI;" & _
"Min Pool Size=5;Max Pool Size=60;Connect Timeout=10;")
End With
Catch ex As Exception
MsgBox(ex.Message.ToString, _
MsgBoxStyle.Critical, _
"Error")
End Try
End Sub
Πάνος Αβραμίδης