Question : Interfaces and Data Provider

I have created an interface for the data provider and now im working on a base class for my data objects to inherit

I wanted to create a property that could be easily accessed in the base class.
Then I could just do Provider.ExecuteNonQuery() without having to  explicitly create an instance because it would be lazy loaded.

Make sense?

Public MustInherit Class DataLayerBase

    Private _provider As IDataProvider

    Public ReadOnly Property Provider() As IDataProvider
        Get
            If _provider Is Nothing Then
            ' dont think i can do this....
            End If
        End Get
    End Property

End Class


    Public Interface IDataProvider

        Function ExecuteNonQuery(ByVal sql As String) As Integer

        Function ExecuteNonQuery(ByVal sql As String, ByVal params As SortedList) As Integer

        Function ExecuteReader(ByVal sql As String) As IDataReader

        Function ExecuteReader(ByVal sql As String, ByVal params As SortedList) As IDataReader

        Function ExecuteScalar(ByVal sql As String) As Object

        Function ExecuteScalar(ByVal sql As String, ByVal params As SortedList) As Object

        Function ExecuteDataTable(ByVal sql As String) As DataTable

        Function ExecuteDataTable(ByVal sql As String, ByVal params As SortedList) As DataTable

        Function ExecuteDataSet(ByVal sql As String) As DataSet

        Function ExecuteDataSet(ByVal sql As String, ByVal params As SortedList) As DataSet

    End Interface

Answer : Interfaces and Data Provider

Sounds like what you're really after are Shared methods.

Generally, this approach is combined with a variation on Singleton pattern, which is what you're trying to do with the property that you've got.

I've also added a factory class, so that you can encapsulate the logic required to determine what provider you want to use. In general, you'll want to read from a config file, or something similar to make that determination. For now, I've just stubbed in a dummy class to demonstrate the idea.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
Public MustInherit Class DataLayerBase
	Private Shared _provider As IDataProvider

	Public Shared ReadOnly Property Provider() As IDataProvider
		Get
			If _provider Is Nothing Then _provider = ProviderFactory.GetProvider
			Return _provider
		End Get
	End Property
End Class

Public Class ProviderFactory
	Public Shared Function GetProvider() As IDataProvider
		Return New MyDataProvider
	End Function
End Class

Public Class MyDataProvider
	Implements IDataProvider

	Public Function ExecuteDataSet(ByVal sql As String) As System.Data.DataSet Implements IDataProvider.ExecuteDataSet
	End Function

	Public Function ExecuteDataSet(ByVal sql As String, ByVal params As System.Collections.SortedList) As System.Data.DataSet Implements IDataProvider.ExecuteDataSet
	End Function

	Public Function ExecuteDataTable(ByVal sql As String) As System.Data.DataTable Implements IDataProvider.ExecuteDataTable
	End Function

	Public Function ExecuteDataTable(ByVal sql As String, ByVal params As System.Collections.SortedList) As System.Data.DataTable Implements IDataProvider.ExecuteDataTable
	End Function

	Public Function ExecuteNonQuery(ByVal sql As String) As Integer Implements IDataProvider.ExecuteNonQuery
	End Function

	Public Function ExecuteNonQuery(ByVal sql As String, ByVal params As System.Collections.SortedList) As Integer Implements IDataProvider.ExecuteNonQuery
	End Function

	Public Function ExecuteReader(ByVal sql As String) As System.Data.IDataReader Implements IDataProvider.ExecuteReader
	End Function

	Public Function ExecuteReader(ByVal sql As String, ByVal params As System.Collections.SortedList) As System.Data.IDataReader Implements IDataProvider.ExecuteReader
	End Function

	Public Function ExecuteScalar(ByVal sql As String) As Object Implements IDataProvider.ExecuteScalar
	End Function

	Public Function ExecuteScalar(ByVal sql As String, ByVal params As System.Collections.SortedList) As Object Implements IDataProvider.ExecuteScalar
	End Function
End Class

Public Interface IDataProvider
	Function ExecuteNonQuery(ByVal sql As String) As Integer
	Function ExecuteNonQuery(ByVal sql As String, ByVal params As SortedList) As Integer
	Function ExecuteReader(ByVal sql As String) As IDataReader
	Function ExecuteReader(ByVal sql As String, ByVal params As SortedList) As IDataReader
	Function ExecuteScalar(ByVal sql As String) As Object
	Function ExecuteScalar(ByVal sql As String, ByVal params As SortedList) As Object
	Function ExecuteDataTable(ByVal sql As String) As DataTable
	Function ExecuteDataTable(ByVal sql As String, ByVal params As SortedList) As DataTable
	Function ExecuteDataSet(ByVal sql As String) As DataSet
	Function ExecuteDataSet(ByVal sql As String, ByVal params As SortedList) As DataSet
End Interface
Random Solutions  
 
programming4us programming4us