Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi
I am using Unity and want to map my handler classes to the query definitions they handle e.g.:-

VB
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))


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:-
VB
<DataContract>
Public Class GetServerDateTimeQueryDefinition
    Inherits QueryDefinitionBase(Of DateTime)


How do I get it to return:-
VB
Public Class GetServerDateTimeQueryHandler
    Implements IQueryHandler(Of Query.Definitions.GetServerDateTimeQueryDefinition, DateTime)


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?
Posted
Updated 11-Apr-14 0:21am
v2

1 solution

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.

VB
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


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

VB
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


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900