结构图 class 库模块

Structuremap class library module

我有一个 class 包含一些服务的库。

使用结构图,我希望这些服务在 class 库中注册。这会导致使用此 class 库的程序集不应为此 class 库设置依赖关系,而是简单地调用在库本身内设置它的模块。

例如,这是否可以使用 autofac 来完成。假设我有一个带有 IAAAService 和 IBBBService 的 class 库。使用 autofac 我可以在这个 class 库中执行以下操作:

public class AutofacModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<AAAService>().As<IAAAService>();
        builder.RegisterType<BBBService>().As<IBBBService>();
    }
}

从应该使用库的程序集我可以这样做:

builder.RegisterModule(new AutofacModule());

我处于必须使用结构图的情况,我想做类似的事情。

这在结构图中可能吗?如果是这样我该怎么做?

谢谢:)

在StructureMap中,有一个类似的概念叫做Registry。在这里查看更多详细信息:http://structuremap.github.io/registration/

请注意,我不建议您在 class 库中使用容器。容器只能在应用程序内部使用 (if they are to be used at all)。

引用自this article

A DI Container should only be referenced from the Composition Root. All other modules should have no reference to the container.