• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

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
0
JRockFL
Asked:
JRockFL
1 Solution
 
Jeff CertainCommented:
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.

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

Open in new window

0
 
JRockFLAuthor Commented:
Perfect! Thank you
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now