HGM
HGM

Reputation: 93

Implementing inherited generic interface in vb.Net

In our current project I am using a generic interface iService which is inherited by all other service interfaces. For instance IService is inherited by ILogService.

The ILogService interface is then implemented by LogService as below:

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

Help Needed: I am trying to understand that when I am implementing ILogService interface I still get the functions/subs in the LogService class containing:

How can I update the method signature so T is displayed as Log?

What am I doing wrong?

Upvotes: 3

Views: 4269

Answers (2)

Mark Hurd
Mark Hurd

Reputation: 10931

@DanTao is correct if that is the intention. If, however, you just intended the name of the method parameter to be specified by the generic type, that is not possible.

However, you can specify any name you like in the implementing method, so you can use Log if you want, but you can't enforce it (and some FxCop rule will warn you haven't used the same parameter name as specified in the interface).

Upvotes: 0

Dan Tao
Dan Tao

Reputation: 128307

Are you talking about these?

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

This is very confusing, because in the above methods T is the name of a method parameter, not a generic type parameter. It could just as easily have been foo or bar. In each case the type of T is IEntity.

If the intention here was that Save and Remove should each accept an argument of type T, but that type T must implement IEntity, this is how you would express that:

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

Upvotes: 3

Related Questions