Question

I have a queuing service that has to inject a different dependency graph depending on the type of object in the queue. I'm using Structure Map.

So, if the object in the queue is TypeA the concrete classes for TypeA are used and if it's TypeB, the concrete classes for TypeB are used.

I'd like to avoid code in the queue like:

if (typeA)
{
   // setup TypeA graph
}
else if (typeB) {
   // setup TypeB graph
}

Within the graph, I also have a generic classes such as an IReader(ISomething, ISpomethingElse) where IReader is generic but needs to inject the correct ISomething and ISomethingElse for the type. ISomething will also have dependencies and so on.

Currently I create a TypeA or TypeB object and inject a generic Processor class using StructureMap into it and then pass a factory manually inject a TypeA or TypeB factory into a method like:

Processor.Process(new TypeAFactory)  // perhaps I should have an abstract factory...

However, because the factory then creates the generic IReader mentioned above, I end up manually injecting all the TypeA or TypeB classes fro there on.

I hope enough of this makes sense.

I am new to StructureMap and was hoping somebody could point me in the right direction here for a flexible and elegant solution.

Thanks

Was it helpful?

Solution

I don't know if I fully understand your question, but in general your queue processor needs access to some sort of factory for processing those objects. The most convenient approach would be if your queue consist of messages/commands (DTOs) and you have some sort of abstraction over command handling logic, such as ICommandHandler<TCommand>.

In that case your queue processor might look like this:

private readonly ICommandHandlerFactory factory;

public void Process(IEnumerable<object> commandQueue)
{
    foreach (object command in commandQueue)
    {
        dynamic handler = this.factory.CreateHandlerFor(command.GetType());

        handler.Handle((dynamic)command);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top