views:

341

answers:

1

I'm setting up StructureMap and it seems like everything I want to do there are two ways to do it and it's unclear to me what the difference is between them. For instance, what's the difference between these two lines:

StructureMapConfiguration.ForRequestedType<IConsumer>().AddConcreteType<Consumer>();

StructureMapConfiguration.ForRequestedType<IConsumer>().TheDefaultIsConcreteType<Consumer>();

Similarly, what's the difference between using AddInstanceOf and ForRequestedType?

+3  A: 
StructureMapConfiguration.ForRequestedType<IConsumer>().AddConcreteType<Consumer>();

This method will add the Consumer type as a plugged type for IConsumer. If there are no other plugged types for IConsumer, then this type will be the default type returned an instance of IConsumer is requested. Otherwise, you will need to get this instance by using the concrete key (which is the assembly qualified name of the type by default).

StructureMapConfiguration.ForRequestedType<IConsumer>().TheDefaultIsConcreteType<Consumer>();

This works similar to AddConcreteType except that it also makes the type the default type. If a request for an IConsumer does not specify a concrete key, this is the type that will be returned.

As for the difference between AddInstanceOf and ForRequestedType, AddInstance of allows you to supply a delegate that will handle creating an instance of the specified type. ForRequestedType gives you an instance of CreatePluginFamilyExpression (or a GenericFamilyExpression in the case of ForRequestedType(Type t)) that allows you to configure an instance in a fluent manner.

Harry Steinhilber