13.5. Working with Interfaces

As consumers of generic types, J# developers are likely to find themselves working heavily with the generic interfaces that participate in these APIs. Given this reality, let's take a moment to consider how J# will work with these interfaces. To see these interfaces in action, you'll first need to create some generic types that leverage generic interfaces. This is achieved in the following example, which creates a general-purpose DataAccessManager class (in VB) that supports a basic interface for retrieving, inserting, updating, and deleting database objects:

[VB code]
Public Enum DataObjectStatus
    Active
    Inactive
    Unknown
End Enum

Public Interface IDataObject
    Property Id() As Int32
    Property Name() As String
    Property Satus() As DataObjectStatus
End Interface

Public Interface IDataObjectCollection(Of T As IDataObject)
    Inherits ICollection(Of T)
    Function getObjectsById(ByVal id As Int32) As T
    Function getObjectsByName(ByVal id As String) As ICollection(Of T)
    Function getObjectsByStatus(ByVal id As DataObjectStatus) As ICollection(Of T)

End Interface

Public Class DataAccessManager(Of T As IDataObject)
    Public Function findObjects(ByVal status As DataObjectStatus) As
                                                     IDataObjectCollection(Of T)
        ...
    End Function

    Public Function insertObject(ByVal dataObject As T) As Boolean
        ...
    End Function

    Public Sub updateObjects(ByVal dataObjects As IDataObjectCollection(Of T))
... End Sub Public Sub deleteObjects(ByVal dataObjects As IDataObjectCollection(Of T)) ... End ...

Get Professional .NET 2.0 Generics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.