有没有一种方法可以用更精确的方法替换此Visual Basic词典中的“对象”值类型?

时间:2019-12-16 23:26:33

标签: vb.net types interface factory-pattern generic-interface

我正在使用工厂从实现相同通用接口的多个服务中进行选择。我的工厂写成这样:

Public Class JurisdictionServiceFactory
    Private JurisdictionTypeMapper As Dictionary(Of String, Object)

    Sub New()
        JurisdictionTypeMapper = New Dictionary(Of String, Object)
        JurisdictionTypeMapper.Add("CountryJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of CountryJurisdiction)))
        JurisdictionTypeMapper.Add("StateJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of StateJurisdiction)))
        JurisdictionTypeMapper.Add("CountyJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of CountyJurisdiction)))
        JurisdictionTypeMapper.Add("CityJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of CityJurisdiction)))
        JurisdictionTypeMapper.Add("OtherJurisdiction", ProjectGlobals.UnityContainer.Resolve(Of IJurisdictionService(Of OtherJurisdiction)))
    End Sub

    Public Function getJurisdictionService(type As String) As Object
        Return JurisdictionTypeMapper(type)
    End Function
End Class

我想用允许编译器知道对象上存在什么方法的东西来代替'Object'作为值类型。例如,我希望能够使用自动完成功能。我已经尝试过这样做:Private JurisdictionTypeMapper As Dictionary(Of String, IJurisdictionService(Of )),但是我只收到一条消息:“预期类型”。

这是IJurisdictionService:

Public Interface IJurisdictionService(Of t)
    Inherits IDisposable

    Function GetJurisdictionsByCompany(companyId As Integer) As List(Of t)

    Sub AddJurisdiction(jurisdiction As t)
End Interface

这是示例实现:

Public Class CountryJurisdictionService
    Implements IJurisdictionService(Of CountryJurisdiction)

    Private jurisdictionRepository As IRepository(Of CountryJurisdiction)

    Public Sub New(jurisdictionRepository As IRepository(Of CountryJurisdiction))
        Me.jurisdictionRepository = jurisdictionRepository
    End Sub

    Public Sub AddJurisdiction(jurisdiction As CountryJurisdiction) Implements IJurisdictionService(Of CountryJurisdiction).AddJurisdiction
        jurisdictionRepository.Add(jurisdiction)
        jurisdictionRepository.Commit()
    End Sub

    Public Function GetJurisdictionsByCompany(companyId As Integer) As List(Of CountryJurisdiction) Implements IJurisdictionService(Of CountryJurisdiction).GetJurisdictionsByCompany
        Return jurisdictionRepository.GetMany(Function(j) j.CompanyID = companyId, False)
    End Function
End Class

编辑: 这是将在以下环境中使用工厂的上下文:

Public Sub AddJurisdiction(jurisdiction)
        Using jurisdictionService = jurisdictionServiceFactory.getJurisdictionService(TypeName(jurisdiction))
            jurisdictionService.AddJurisdiction(jurisdiction)
        End Using
End Sub

1 个答案:

答案 0 :(得分:0)

谁在呼叫getJurisdictionService?只要呼叫者知道他们要求的类型,您就可以执行以下操作。

如何进行更改:

Public Function getJurisdictionService(type As String) As Object
    Return JurisdictionTypeMapper(type)
End Function

对此:

Public Function getJurisdictionService(Of T)() As IJurisdictionService(Of T)
    Return DirectCast(JurisdictionTypeMapper(GetType(T)), IJurisdictionService(Of T))
End Function

调用方执行此操作以获得强类型服务:

service = jurisdictionServiceFactory.getJurisdictionService(Of CountryJurisdictionService)()

当然,还需要从Type而不是类名中键入字典:

Private JurisdictionTypeMapper As Dictionary(Of Type, Object)

编辑:

考虑到您提供的新细节,在这种情况下,我经常喜欢创建基类或父接口以在通用类之间创建同级关系,即使这只是基于代码的文档的一部分:

Public Interface IJurisdictionService
    Inherits IDisposable

End Interface

Public Interface IJurisdictionService(Of t) 
    Inherits IJurisdictionService

    Function GetJurisdictionsByCompany(companyId As Integer) As List(Of t)

    Sub AddJurisdiction(jurisdiction As t)

End Interface

对于司法管辖区对象也是如此,它们可以使用父接口或基类:

Public Interface IJurisdictionService
End Interface

然后Private JurisdictionTypeMapper As Dictionary(Of String, Object)变成Private JurisdictionTypeMapper As Dictionary(Of String, IJurisdictionService)