2011-11-29 66 views
3

在我們當前的項目中,我使用了通用接口iService,該接口由所有其他服務接口繼承。例如IServiceILogService繼承。在vb.Net中實現繼承的通用接口

的ILogService接口,然後通過LogService實現如下:

Public Interface IService(Of T) 
    Sub Save(ByVal T As IEntity) 
    Sub Remove(ByVal T As IEntity) 
    Function FindBy(ByVal Id As Guid) As T 
    Function FindAll() As IEnumerable(Of T) 
End Interface 

Public Interface ILogService 
    Inherits IService(Of Log) 


    Function FindLogsByOwner(ByVal Owner As Guid, ByVal visibility As LogVisibility) As IList(Of Log) 
    Function FindAllLogsByVisibility(ByVal visibility As LogVisibility) As IList(Of Log) 
    Function FindAllLogsByType(ByVal type As LogType) As IList(Of Log) 

End Interface 

Public Class LogService 
    Implements ILogService 


    Public Function FindAll() As System.Collections.Generic.IEnumerable(Of Model.CSLS.Log) Implements Infrastructure.Domain.IService(Of Model.CSLS.Log).FindAll 

    End Function 

    Public Function FindBy(Id As System.Guid) As Model.CSLS.Log Implements Infrastructure.Domain.IService(Of Model.CSLS.Log).FindBy 

    End Function 

    Public Sub Remove(T As Infrastructure.Domain.IEntity) Implements Infrastructure.Domain.IService(Of Model.CSLS.Log).Remove 

    End Sub 

    Public Sub Save(T As Infrastructure.Domain.IEntity) Implements Infrastructure.Domain.IService(Of Model.CSLS.Log).Save 

    End Sub 

    Public Function FindAllLogsByType(type As Model.CSLS.LogType) As System.Collections.Generic.IList(Of Model.CSLS.Log) Implements Model.CSLS.ILogService.FindAllLogsByType 

    End Function 

    Public Function FindAllLogsByVisibility(visibility As Model.CSLS.LogVisibility) As System.Collections.Generic.IList(Of Model.CSLS.Log) Implements Model.CSLS.ILogService.FindAllLogsByVisibility 

    End Function 

    Public Function FindLogsByOwner(Owner As System.Guid, visibility As Model.CSLS.LogVisibility) As System.Collections.Generic.IList(Of Model.CSLS.Log) Implements Model.CSLS.ILogService.FindLogsByOwner 

    End Function 
End Class 

幫助需要:我想了解的是,當我實現ILogService界面我仍然得到的功能/潛艇在LogService類含:

  • 方法參數TIEntity類型的,而不是Log

如何更新方法簽名以便T顯示爲Log

我在做什麼錯?

回答

3

你在說這些嗎?

Sub Save(ByVal T As IEntity) 
Sub Remove(ByVal T As IEntity) 

這是非常混亂的,因爲在上述方法T是一種方法參數,泛型類型參數的名稱。它可能很容易被foobar。在每種情況下,T的類型是IEntity

如果這裏的意圖是SaveRemove應該在每次接受T類型的參數,但該類型T必須實現IEntity,這是你將如何表達:

Public Interface IService(Of T As IEntity) 
    Sub Save(ByVal entity As T) 
    Sub Remove(ByVal entity As T) 
    Function FindBy(ByVal Id As Guid) As T 
    Function FindAll() As IEnumerable(Of T) 
End Interface 
0

@DanTao是如果這就是意圖,那麼更正。但是,如果您只是想讓泛型類型指定方法參數的名稱,那是不可能的。

但是,你可以指定你的實現方法喜歡的任何名稱,所以你可以使用Log如果你想,但你不能強制執行(和一些FxCop的規則將警告你還沒有使用相同的參數名稱在接口中指定)。