命名实例和StructureMap默认实例?实例、StructureMap

2023-09-03 07:55:00 作者:耳根太软

在我StructureMap引导code我使用一个自定义的约定来扫描组件,并添加接口/实现对对象图的命名实例。基本上我有一些逻辑,检查配置设置和向下钻取本声明根据不同的情况:

In my StructureMap bootstrapping code I'm using a custom convention to scan assemblies and add interface/implementation pairs to the object graph as named instances. Essentially I have some logic which checks configuration settings and drills down to this statement depending on various conditions:

registry.For(interfaceType).Use(type)
    .Named(implementationName);

这将所有的命名实例不够好了。不过,我也想在未指定实例名称的情况下增加一个默认实例。但是,默认实例不是总是最后一个加入到曲线图。有时其他命名实例在扫描过程中之后添加。这似乎,虽然,取其实例最后加入,不管是否它的命名,始终是默认

This adds all of the named instances well enough. However, I'd also like to add a default instance in the event that an instance name is not specified. However, the default instance isn't always the last one added to the graph. Sometimes other named instances are added afterward during the scanning. It would seem, though, that whichever instance is added last, regardless of whether or not it's named, is always the default.

我已经试过了流畅的API的各种组合,其中包括:

I've tried various combinations of the fluent API, including:

registry.For(interfaceType).Add(type);

registry.For(interfaceType).Use(type);

即使是一些标为pcated德$ P $的人。但由此产生的行为始终是最后一个是默认的。因此,如果将实现的顺序是这样的:

Even some of the ones marked as deprecated. But the resulting behavior is always that the last one is the default. So if the order of adding implementations is something like this:

对于记录器界面中使用名为log4net的实施log4net的 对于记录器界面中使用log4net的执行默认 对于记录器界面中使用名为模拟的模拟实施

由此产生的行为是没有指定名称时,模拟的实施作为默认。调试到 AllInstances 在容器中,我按以下顺序见:

The resulting behavior is that the "Mock" implementation is used as the default when no name is specified. Debugging into AllInstances in the container, I see in the following order:

log4net的记录器的实例名为log4net的 log4net的记录器与一个GUID的名称的一个实例(像任何其他默认实例,据我可以告诉) 名为模拟的模拟记录器的实例

调试到从容器中没有一个实例名称叫当一个logging语句,然而,导​​致模拟实现使用。

Debugging into a logging statement when called from the container without an instance name, however, results in the Mock implementation being used.

有没有办法为默认实例添加到对象图,同时还能够为后来添加命名实例?

Is there a way to add a default instance to the object graph while still being able to add named instances afterward?

推荐答案

添加方法将添加实例(如果您需要添加命名实例或添加多个实例随着收藏/枚举使用)。如果没有明确的默认注册(使用使用法),添加的最后一个实例将成为默认实例。该使用方法用于设置默认实例。如果调用使用多次,注册的最后一个实例将成为默认的。

The Add method will add instances (if you need to add named instances or add multiple instances for to use with collections/enumerations). If no explicit default is registered (using the Use method), the last instance added will become the default instance. The Use method is intended for setting the default instance. If you invoke Use multiple times, the last instance registered will become default.

为了设置一个默认实例,然后注册你应该能够做到像这样进一步命名实例:

In order to set a default instance and then register further named instances you should be able to do it like this:

registry.For(typeof(Logger)).Use(typeof(Log4Net)).Named("Log4Net");
registry.For(typeof(Logger)).Add(typeof(Mock)).Named("Mock");

这将使 log4net的实例的默认,也可访问作为命名实例。该模拟实例将作为命名实例。

This will make the Log4Net instance the default and also accessible as a named instance. The Mock instance will be available as a named instance.