2011-11-29 64 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的规则将警告你还没有使用相同的参数名称在接口中指定)。