How to get all instances of a universal class with a structural map

In the Structural map, I have the following line working with domain events:

public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent { foreach (var handler in ObjectFactory.GetAllInstances<IDomainEventHandler<TEvent>>()) { if (handler.IsActive) handler.Handle(eventToDispatch); } } 

I register them inside the StructureMap registry as follows:

 x.AddAllTypesOf(typeof(IDomainEventHandler<>)); 

The first block above throws an Unknown error - Map structure code 400. Does anyone know how I can get certain types of a generic class from a strcuture map container?

TIA

Andrew

+4
source share
1 answer

The first thing I checked was the following outputs:

 Console.WriteLine(ObjectFactory.WhatDoIHave()); 

Make sure event handlers are registered as you expect.

If the classes are registered as you expect, I think that is exactly how you want to solve your IDomainEventHandler:

 foreach (var handler in ObjectFactory.ForObject(eventToDispatch) .GetAllClosedTypesOf(typeof(IDomainEventHandler<>)) .As<IDomainEventHandler<TEvent>>()) 
+5
source

All Articles