Injecting correct object graph using StructureMap in Queue of different Objects
Posted
by
davy
on Stack Overflow
See other posts from Stack Overflow
or by davy
Published on 2013-11-08T11:50:44Z
Indexed on
2013/11/08
15:55 UTC
Read the original article
Hit count: 247
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
© Stack Overflow or respective owner