Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 基于代理对象的基类_Vb.net_Oop_Interface - Fatal编程技术网

Vb.net 基于代理对象的基类

Vb.net 基于代理对象的基类,vb.net,oop,interface,Vb.net,Oop,Interface,我有一个Web服务,它由一个数据访问对象包装,并由许多不同的UI控件访问 代理对象如下所示: Public Class WebProxyObject1 ' Common properties, there are about 10 of these Public Name As String Public Address As String ' Specialized properties there are about 20 of these Pub

我有一个Web服务,它由一个数据访问对象包装,并由许多不同的UI控件访问

代理对象如下所示:

Public Class WebProxyObject1
    '  Common properties, there are about 10 of these
    Public Name As String
    Public Address As String

    ' Specialized properties there are about 20 of these
    Public count As Integer
End Class
Public Class DataAccessObject
    Implements IDataAccessObject


    '  These are called in MANY, MANY, MANY locations
    Public Function GetObject(ByVal name As String) As WebProxyObject1 Implements IDataAccessObject.GetObject
        ' Makes call to a webservice

        Return New WebProxyObject1
    End Function

    Public Function ListObjects() As System.Collections.Generic.List(Of WebProxyObject1)         Implements IDataAccessObject.ListObjects
        ' Makes call to a webservice
        Dim list As New List(Of WebProxyObject1)

        Return list
    End Function
End Class
Public Interface IProxyObject
    ' Common
    Property Name() As String
    Property Address() As String

    ' Specialized
    Property Count() As Integer
    Property Foo() As Integer

End Interface
DAL层看起来像这样:

Public Class WebProxyObject1
    '  Common properties, there are about 10 of these
    Public Name As String
    Public Address As String

    ' Specialized properties there are about 20 of these
    Public count As Integer
End Class
Public Class DataAccessObject
    Implements IDataAccessObject


    '  These are called in MANY, MANY, MANY locations
    Public Function GetObject(ByVal name As String) As WebProxyObject1 Implements IDataAccessObject.GetObject
        ' Makes call to a webservice

        Return New WebProxyObject1
    End Function

    Public Function ListObjects() As System.Collections.Generic.List(Of WebProxyObject1)         Implements IDataAccessObject.ListObjects
        ' Makes call to a webservice
        Dim list As New List(Of WebProxyObject1)

        Return list
    End Function
End Class
Public Interface IProxyObject
    ' Common
    Property Name() As String
    Property Address() As String

    ' Specialized
    Property Count() As Integer
    Property Foo() As Integer

End Interface
现在,我需要在组合中添加第二个Web服务。目标是重用当前编码为使用来自第一个Web服务的代理对象的UI控件。大约有10种常见属性和20种不同属性。为了添加第二个Web服务,我将创建一个实现相同接口的第二个DAL对象。问题是它当前从第一个Web服务返回代理

我对如何解决这个问题的想法是从每个代理对象中提取一个接口,并将它们混合在一起。然后在两个代理对象上实现新接口。这将创建一个巨大的类/接口,其中一些属性不被使用。然后让DAL返回接口

我面临的问题不是一个真正的bug或问题,但是提取这两个接口并将它们粉碎在一起感觉有点不对劲。我认为从技术上讲它会起作用,但有点异味。有更好的办法吗

生成的界面如下所示:

Public Class WebProxyObject1
    '  Common properties, there are about 10 of these
    Public Name As String
    Public Address As String

    ' Specialized properties there are about 20 of these
    Public count As Integer
End Class
Public Class DataAccessObject
    Implements IDataAccessObject


    '  These are called in MANY, MANY, MANY locations
    Public Function GetObject(ByVal name As String) As WebProxyObject1 Implements IDataAccessObject.GetObject
        ' Makes call to a webservice

        Return New WebProxyObject1
    End Function

    Public Function ListObjects() As System.Collections.Generic.List(Of WebProxyObject1)         Implements IDataAccessObject.ListObjects
        ' Makes call to a webservice
        Dim list As New List(Of WebProxyObject1)

        Return list
    End Function
End Class
Public Interface IProxyObject
    ' Common
    Property Name() As String
    Property Address() As String

    ' Specialized
    Property Count() As Integer
    Property Foo() As Integer

End Interface

为要从中继承的WebProxyObjects创建基类

Public MustInherit Class WebProxyObjectBase
    '  Common properties
    Public Property Name As String
    Public Property Address As String
End Class
接下来创建两个WebProxyObjects:

Public Class WebProxyObject1
    Inherits From WebProxyObjectBase

    ' Specialized properties
    Public Property count As Integer
End Class

Public Class WebProxyObject2
    Inherits From WebProxyObjectBase

    ' Specialized properties
    Public Property foo As Integer
End Class
接下来,让DAL返回基类:

Public Class DataAccessObject
    Implements IDataAccessObject


    '  These are called in MANY, MANY, MANY locations
    Public Function GetObject(ByVal name As String) As WebProxyObjectBase Implements IDataAccessObject.GetObject
        ' Makes call to a webservice

        Return New WebProxyObjectBase
    End Function

    Public Function ListObjects() As System.Collections.Generic.List(Of WebProxyObjectBase)         Implements IDataAccessObject.ListObjects
        ' Makes call to a webservice
        Dim list As New List(Of WebProxyObjectBase)

        Return list
    End Function
End Class
然后,在调用DataAccessObject时,您将能够键入返回到适当类的返回:

Dim DAO as New DataAccessObject
Dim Pxy1 as WebProxyObject1 = TryCast(DAO.GetObject("BOB"), WebProxyObject1)

If Pxy1 IsNot Nothing Then
    'Do stuff with proxy
End If