我有一个由数据访问对象包装的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
DAL层看起来像这样:
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
现在,我需要添加第二个web服务。目标是重用当前编码的UI控件以使用来自第一个Web服务的代理对象。大约有10种常见属性,大约20种不同。要添加第二个web服务,我将创建一个实现相同接口的第二个DAL对象。问题是它当前从第一个Web服务返回代理。
我对如何解决这个问题的想法是从每个代理对象中提取一个接口并将它们混合在一起。然后在两个代理对象上实现新接口。这将创建一个巨大的类/接口,其中不使用某些属性。然后让DAL返回界面。
我面临的问题不是真正的错误或问题,但是提取2个界面并将它们粉碎在一起只是觉得有点不对劲。我认为它在技术上会起作用,但它有点味道。有更好的主意吗?
生成的界面如下所示:
Public Interface IProxyObject
' Common
Property Name() As String
Property Address() As String
' Specialized
Property Count() As Integer
Property Foo() As Integer
End Interface
答案 0 :(得分:0)
为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