Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

structuremap ObjectFactory.GetAllInstances<IHandle<TEvent>>()

I am having a rough time implementing eventing in a recent project.

I have verified that structuremap is scanning properly assemble and adding EventHandlers

Scan(cfg =>
            {
               cfg.TheCallingAssembly();
                cfg.IncludeNamespace("ABC.EventHandler");
                cfg.ConnectImplementationsToTypesClosing(typeof(IHandle<>));

           });

 public class StructureMapEventDispatcher : IEventDispatcher
    {

        public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent
        {

            foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
            {

                handler.Handle(eventToDispatch);

            }

        }

    }

Before I used to fire Event from Domain. Somthing like Dispatcher.RaiseEvent(new [domainEvent class](x,y,z));

and the event will get fired up. I had to change the design where I am now collectiong events in a collection

_domainEvents = new Collection<IDomainEvent>();

and then raising it after I have saved the domain to Repository

 public static void Raise(ICollection<IDomainEvent> domainEvents)
        {
            foreach (var domainEvent in domainEvents)
            {
                DomainEventDispatcher.Raise(domainEvent);
            }

        }

but now

ObjectFactory.GetAllInstances<IHandle<TEvent>>() returns 0 count of handlers

if I watch for

ObjectFactory.GetAllInstances<IHandle<DomainEventClass>>() it returns collection of handlers properly ( currently I have 2 and it shows 2 count)

... I am assuming this has something to do with events being raised as of type IDomainEvent instead of actual type and that is making it hard for structuremap to resolve it.

How can I solve this issue?

Regards,

The Mar

--

Edit 1:

I have conformed that struturemap container contains all event handlers scanned from the assembly.

Edit 2

I dont know how to make this question attract more attention. I am adding bounty for a solution to achieve the results desired. If the question is not clear, please ask.

Basically I want the ObjectFactory.GetAllInstances<IHandle<TEvent>>() to return handlers for TEvent where TEvent is of Type IDomainEvent. Events to be raised are stored in Collection of IDomainEvent and raised after the fact that Domain was saved (from service layer).

I am thinking there should be some way to make structuremap know that the event raised as IDomainEvent is actually of Type DomainEvent

var eventsToRaise= dealer.EventsToRaise(); Adding Information from Debug Window:

After the events have been raised in the dispatcher window

enter image description here

Edit 3: Eventhough eventToRaise shows as "DealerName Changed" and "DealerCommunicationChanged"
typeof(TEvent) gives Type as Domain.IDomainEvent

I guesss if it is possible to get be able to cast to right type ( from whereever VS watch window is getting info) the problem could get resolved

----- Result---

Both approach worked. I put both approached to 2 other members in my team and we felt that solution without reflection to be selected as right answer.

Today we will be doing a test with changed implementation and see if there are any issues with this solution in the solution.

I have upvoted reflection based solution as it is also right answer.


like image 711
TheMar Avatar asked May 23 '11 23:05

TheMar


1 Answers

As you say, the problem is that you're asking structure map for all instances of IHandle<IDomainEvent> and it has none of those, structuremap has handlers for concrete events. You'd need to construct the type using the actual type of the event and then ask for all handlers of that event:

        Type genericHandler = typeof(IHandle<>);
        Type[] typeArgs = { eventToDispatch.GetType() };
        Type neededHandler = genericHandler.MakeGenericType(typeArgs);
        var handlers = ObjectFactory.GetAllInstances(neededHandler);

the problem is that you end up with an IList of objects and you need to cast them to the correct handler type and it get's a bit tricky.... a possible solution is to use reflection to call the Handle() method:

        var methodInfo = neededHandler.GetMethod("Handle");
        object[] methodArgs = new object[] { eventToDispatch };
        foreach (var h in handlers)
        {
            methodInfo.Invoke(h, methodArgs);
        }
like image 52
Jaime Avatar answered Oct 11 '22 18:10

Jaime