是什么在StructureMap AddConcreteType和TheDefaultIsConcreteType之间的区别?区别、StructureMap、AddConcreteType、TheDe

2023-09-04 03:30:25 作者:在我身旁你总爱哼小曲

我设立StructureMap,它好像一切我想做的事情有两种方法可以做到这一点,这是我不清楚的区别是它们之间有什么。例如,什么是这两条线之间的区别:

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

同样,有什么用AddInstanceOf之间的区别ForRequestedType?

解决方案

StructureMapConfiguration.ForRequestedType<IConsumer>().AddConcreteType<Consumer>(); 表情 南海瓜分殆尽 支离破碎的南海岛礁 表情

此方法将增加消费者类型作为插入类型IConsumer。如果没有其他的插入类型IConsumer,那么这种类型的将是默认类型返回IConsumer的一个实例被请求。否则,你将需要通过具体的键(默认情况下是该类型的程序集限定名称),以获得该实例。

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

这的工作原理类似于AddConcreteType除了这也使得类型的默认类型。如果对于一个IConsumer一个请求未指定一个具体键时,这是将要返回的类型

至于AddInstanceOf和ForRequestedType,允许你提供一个委托将处理创建指定类型的实例AddInstance之间的区别。 ForRequestedType给你CreatePluginFamilyEx pression一个实例(或GenericFamilyEx pression在ForRequestedType(T型)的情况下),让您在流畅地配置实例。

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?

解决方案

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.

 
精彩推荐