我有一个包含一些服务的类库。
使用结构图,我希望这些服务能够在该类库中注册。这导致使用此类库的程序集不应为此类库设置依赖项,而只需调用在库本身内设置此依赖项的模块。
例如,使用 autofac 可以做到这一点吗?假设我有一个包含 IAAAService 和 IBBBService 的类库。使用 autofac 我可以在这个类库中执行以下操作:
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 中,有一个类似的概念,称为注册表。请查看此处了解更多详细信息:http://structuremap.github.io/registration/
请注意,我不建议您在类库中使用容器。容器只能在应用程序内部使用 ( 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.
关于c# - Structuremap类库模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40300268/