简体   繁体   English

有没有办法用更精确的东西替换这个 Visual Basic 字典中的“对象”值类型?

[英]Is there a way I can replace the 'Object' value type in this Visual Basic dictionary with something more precise?

I'm using a factory to select from several services that are implementing the same generic interface.我正在使用工厂从实现相同通用接口的多个服务中进行选择。 I have the factory written like this:我的工厂是这样写的:

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

I would like to replace 'Object' as the value type with something that would allow the compiler to know what methods exist on the object.我想将“对象”作为值类型替换为允许编译器知道对象上存在哪些方法的东西。 For example, I want to be able to use autocomplete.例如,我希望能够使用自动完成功能。 I've tried doing this: Private JurisdictionTypeMapper As Dictionary(Of String, IJurisdictionService(Of )) , but I just get a message: "Type expected".我试过这样做: Private JurisdictionTypeMapper As Dictionary(Of String, IJurisdictionService(Of )) ,但我只收到一条消息:“Type expected”。

This is IJurisdictionService:这是 IJurisdictionService:

Public Interface IJurisdictionService(Of t)
    Inherits IDisposable

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

    Sub AddJurisdiction(jurisdiction As t)
End Interface

This is an example implementation:这是一个示例实现:

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

Edit: This is the context that the factory will be used in:编辑:这是工厂将用于的上下文:

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

Who is calling getJurisdictionService ?谁在调用getJurisdictionService As long as the caller knows what Type they are requesting you can do something like this.只要调用者知道他们请求的是什么类型,你就可以做这样的事情。

How about changing this:如何改变这个:

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

To this:对此:

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

And the the caller does this to get a strongly-typed service:调用者这样做是为了获得强类型服务:

service = jurisdictionServiceFactory.getJurisdictionService(Of CountryJurisdictionService)()

And of course the dictionary needs to be keyed off of the Type instead of the class name:当然,字典需要关闭类型而不是类名:

Private JurisdictionTypeMapper As Dictionary(Of Type, Object)

Edit:编辑:

Given the new details you have provided, in that situation I often like to create base class or parent interface to create a sibling relationship between the generic classes, even if this is just for a bit of code-based documentation:鉴于您提供的新细节,在这种情况下,我经常喜欢创建基类或父接口来创建泛型类之间的兄弟关系,即使这只是一些基于代码的文档:

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

And that goes for the Jurisdiction objects as well, they could use a parent Interface or base class:这也适用于 Jurisdiction 对象,它们可以使用父接口或基类:

Public Interface IJurisdictionService
End Interface

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM