What if you want StructureMap to return a different Type for users in one role versus user without that role? What if you’d like to return one Type if the database is reachable and another Type if the database is determined to be offline? I kept getting questions like that on the StructureMap group, so I applied some Lambda-fu in the latest release to create the new “Conditional Instance.” This blog is excerpted from the StructureMap documentation here.
There have been several questions on the StructureMap users list about doing
conditional construction (i.e., return this object if this condition, else this
other object). In order to meet this apparent need,
StructureMap 2.5.2 introduces the new ConditionalInstance that allows a user to
effectively switch the active Instance based on a Predicate<IContext> boolean
test. Here’s a quick example of using the new Conditional() syntax of
The syntax above is configuring and attaching a ConditionalInstance object.
Internally, this syntax is telling the ConditionalInstance to:
- Return the concrete type “ARule” if the condition “c => false” is met (in real
usage the predicate would do something more intelligent 😉 )
- Else, return a Rule object specified by the variable named GREEN if the
condition “c => true” is met
- Finally, if none of the predicates match, return the Rule object specified by
the variable named RED
The syntax “If( predicate ).*************” uses an InstanceExpression and all
possible Instance types are available.
Internally, the ConditionalInstance looks like this:
When a call is made to
- Internally, the Container object finds the ConditionalInstance object that was
configured and named “conditional” for the PluginType “Rule”
- The Container invokes the ConditionalInstance.Build(Type, BuildSession) method
- The ConditionalInstance evaluates its InstanceCase collection to find the first
InstanceCase that matches the current IContext and invokes the internal Instance
of that InstanceCase
- Lastly, if ConditionalInstance does not find any matching InstanceCase objects,
it will invoke its default Instance to build the requested object
It might be easier to just see the code for this:
Please see Using the Session Context for
more information on what is possible with the IContext.