views:

63

answers:

1

How can I figure StructureMap to use more than one concrete Class per Interface. i.e. an IRepositoryCustomer is implemented by: RepositoryCustomerA and RepositoryCustomerB

How do I register and resolve this to/from the StructureMap IoC container?

+1  A: 

You first need to answer the question, "when do I want RepositoryCustomerA instead of RepositoryCustomerB?"

If either of those classes could be instantiated in production based on some runtime state, then you probably want to look into conditional object construction.

If RepositoryCustomerA should be instantiated at development time (for unit testing, say) while RepositoryCustomerB is the production analog, then you need two distinct Registrys: one that exists only in your test code, and one that is accessible in production. For more on this route, read about creating registries as a means of configuring StructureMap.

ladenedge
both variants seems to use strings. Is it possible to have this in a typesafe way?
Rookian
I looked at the conditional object construction tutorial, but the methods that were used there are nearly all obsolete ... is there a current version for this? I use StructureMap 2.6.1.0
Rookian
@Rookian: my apologies for missing your comments until now. [This answer](http://stackoverflow.com/questions/2425565/structuremap-howto-conditional-construction-on-deep-object) might help you with avoiding the non-typesafe conditional constructions. Registries support strings, but they aren't required in every case - it just depends on how much detail you need in your construction. Just constructing one repository or the other sounds like it could be done in a typesafe manner.
ladenedge