gpt4 book ai didi

vb.net - 在 vb.Net 中实现继承的泛型接口(interface)

转载 作者:行者123 更新时间:2023-12-02 00:27:35 25 4
gpt4 key购买 nike

在我们当前的项目中,我使用了一个通用接口(interface) iService,它被所有其他服务接口(interface)继承。例如IServiceILogService继承。

ILogService 接口(interface)然后由 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 接口(interface)时,我仍然在 LogService 类中获得函数/子程序包含:

  • IEntity 类型的方法参数 T 而不是 Log

如何更新方法签名以便 T 显示为 Log

我做错了什么?

最佳答案

你说的是这些吗?

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

关于vb.net - 在 vb.Net 中实现继承的泛型接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8305794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com