Unity-将类映射到其通过接口处理的内容 [英] Unity - map a class to what it handles by interface

查看:112
本文介绍了Unity-将类映射到其通过接口处理的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用Unity,并想将我的处理程序类映射到它们处理的查询定义,例如:-

Hi
I am using Unity and want to map my handler classes to the query definitions they handle e.g.:-

Public Shared Sub RegisterTypes(container As IUnityContainer)

     Dim interfaceType As Type = Nothing

     'Get the interface definition for the generic query handler interface we are looking for
     interfaceType = GetType(IQueryHandler(Of ,))

     container.RegisterTypes(AllClasses.FromAssembliesInBasePath(includeUnityAssemblies:=False, includeSystemAssemblies:=False).Where(Function(t) t.ImplementsInterface(interfaceType)),
                    getName:=Function(i) WithName.TypeName(i),
                    getLifetimeManager:=Function(i) WithLifetime.PerThread(i))



所以-如何注册它,以便在传递类时可以找到实现该类的IQueryHandler的适当类?

例如如果我通过:-



So - how do I register it so that when I pass in a class it can find the appropriate class that implements IQueryHandler of that class?

e.g. if I pass in:-

<DataContract>
Public Class GetServerDateTimeQueryDefinition
    Inherits QueryDefinitionBase(Of DateTime)



如何获得退货:-



How do I get it to return:-

Public Class GetServerDateTimeQueryHandler
    Implements IQueryHandler(Of Query.Definitions.GetServerDateTimeQueryDefinition, DateTime)



在此调用之后,容器中确实具有实现IQueryHandler(Of,)的所有类.现在,我只需要找到一个与给定的IQueryDefinition相匹配的对象即可.

有任何想法/链接吗?



After this call the container does have all the classes that implement IQueryHandler(Of ,) in it. Now I just need to find the one that matches my given IQueryDefinition.

Any ideas/links?

推荐答案

事实证明,我对此进行了过多的思考或过度设计.
相反,我使用处理程序处理的定义类的名称将处理程序类添加到Unity容器中.

It turns out I was massively over-thinking or over-engineering this.
Instead I add the handler classes to the Unity container using the name of the definition class that the handler handles.

Public Shared Function GetNameFromCommandDefinition(ByVal handler As Type) As String

    'find out what tyoe the handler handles...
    Dim handlerInterface As Type = handler.GetInterface("ICommandHandler`1", False)
    If (handlerInterface IsNot Nothing) Then
        ' Get the name of the first generic class
        If (handlerInterface.GetGenericArguments.Count > 0) Then
            Return handlerInterface.GetGenericArguments(0).Name
        End If
    End If

    Return handler.Name()


End Function



然后您可以在Unity注册代码中调用此函数:-



and you call this function in the Unity registration code thus:-

Public Shared Sub RegisterTypes(container As IUnityContainer)

    Dim interfaceType As Type = Nothing

    'Get the interface definition for the generic interface we are looking for
    interfaceType = GetType(ICommandHandler(Of ))

    container.RegisterTypes(AllClasses.FromAssembliesInBasePath(includeUnityAssemblies:=False, includeSystemAssemblies:=False).Where(Function(t) t.ImplementsInterface(interfaceType)),
                            getName:=Function(i) GetNameFromCommandDefinition(i),
                            getLifetimeManager:=Function(i) WithLifetime.PerThread(i))

End Sub



它仍然没有100%解决(没有双关语),但我正朝着正确的方向前进...



It''s still not 100% resolved (no pun intended) but I am heading in the right direction...


这篇关于Unity-将类映射到其通过接口处理的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆