0

我正在尝试用 vb 实现一个通用接口。

Public Interface IGenericRepository(Of T)

    Function getAll() As IQueryable(Of T)
    Function [get](ByVal id As Integer) As T
    Sub saveOrUpdate(BaseDTO As T)
    Sub delete(BaseDTO As T)
End Interface

Public Interface IWorkItemRepository
    Inherits IGenericRepository(Of WorkItem)

    Function getWorkItemsByRequestor(ByVal username As String) As IList(Of WorkItem)
End Interface

Public Class WorkItemRepository
    Inherits genericRepository(Of WorkItem)
    Implements IWorkItemRepository

End Class

我不想在 Iworkitemrepository 中复制共享方法的具体实现。

如何通过继承实现这一目标?

4

2 回答 2

1

如果您有不想复制的共享代码,则需要一个基本实现。

Public Interface IInterface(Of T)
  Sub X
  Sub Y
End Interface

Public MustInherit Class BaseImpl
 Implements IInterface(Of MyClass)

 Sub X() Implements IInterface(Of MyClass)
   '//stuff here
 End Sub

 MustOverride Sub Y() Implements IInterface(Of MyClass)

End Class

Public Class DerivedImpl
  Inherits BaseImpl

  Overrides Sub Y()
     'do different stuff
  End Sub
End Class

Public Class AnotherImpl
  Inherits BaseImpl

  Overrides Sub Y()
     'do different stuff
  End Sub
End Class

以上所有应该能够案例到IInterface(Of MyClass)

于 2012-08-06T11:51:25.550 回答
0

可能在您的界面上定义扩展方法?那么你就不需要一个抽象基类了。

于 2012-08-06T11:54:05.163 回答