I've been using Structuremap for a few months now, and it has worked
great on some smaller (green field) projects. Most of the
configurations I have had to set up involve a single implementation
per interface. In the cases where I needed to choose a specific
implementation at runtime, I created a factory class that used
ObjectFactory.GetNamedInstance<(). In the smaller projects, there
were few enough of these cases where I was comfortable with the
references to ObjectFactory. My understanding is that you want to
limit these references as much as possible, and ideally only reference
the ObjectFactory once.
I am working to refactor a larger codebase to use IOC/StructureMap, and am
finding that I may need many of these factory classes with
ObjectFactory references to get what I need.
Essentially, I am creating a "root service" with the ObjectFactory, so
that everything in the dependency chain is managed by the container.
The root service is created by name (i.e. "BuildCar", "BuildTruck"),
and the services needed deeper in the dependency chain could also be
constructed using the same name--so the "IAttachWheels" service could vary
based on whether a car or truck is being built. Since the class that
depends on IAttachWheels is the same in both configurations, I don't
think I can use ConstructedBy in the registry to choose the
implementation.
Also, to be clear, the IAttachWheels implementations need to be
managed by the container as well, because the dependency chain runs
fairly deep.
I looked briefly at Profiles as an option, but read (here on
StackOverflow) that changing profiles essentially changes
implementations for all threads. Is there a feature that
is similar to profiles that is thread/request specific? Is the factory class that references ObjectFactory approach the right way to go?
Any thoughts would be appreciated.