What's the difference between AddConcreteType and TheDefaultIsConcreteType in StructureMap?

戏子无情 提交于 2019-12-07 21:45:02

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/144272/whats-the-difference-between-addconcretetype-and-thedefaultisconcretetype-in-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!