I was massively overthinking/overengineering this. Instead of all that effort, I simply add the handler class to the container using the name of the cdefinition class it handles...
vb.net Code:
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
(This function goes in the initial container registration code e.g. )
vb.net Code:
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
Since there is a one-to-one mapping this works fine.




Reply With Quote
