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

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.
Instead of a reflection-based approach, I would recommend using a record to hold onto the type information for you. Something like this:
If you find instantiating an event records to be cumbersome, a helper could infer the type parameter like this:
Which would allow you to instantiate event records like this:
Then instead of holding onto a collection of
IDomainEvents, hold onto a collection ofIEventRecordsthat hold the necessary type data to raise themselves: