通用存储库vb

时间:2012-08-06 11:12:33

标签: vb.net repository

我正在尝试使用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中共享方法的具体实现。

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

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

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