c# - 在 .net core 中为不同的依赖注入容器编写一个包装器
问题描述
我想在我的.net 核心项目中围绕依赖注入容器编写一个包装器,这样每当我需要在我的应用程序中注入某些东西时,我就可以使用我自己的注入器,它实际上是使用Autofac和SimpleInjection等第三方容器进行注入。这样我就可以改变我的注射器,而无需改变我的代码。
我为此目的编写了一个接口,其中包含一些需要的方法:
interface IDependencyBuilder
{
void CreateContainer();
IContainer Build();
void RegisterModule<T>() where T : Module, new();
}
我已经为Autofac实现了它,如下所示:
public class AutofacContainerBuilder : IDependencyBuilder
{
private readonly ContainerBuilder _containerBuilder;
public AutofacContainerBuilder()
{
_containerBuilder = new ContainerBuilder();
}
public void CreateContainer()
{
throw new NotImplementedException();
}
public IContainer Build()
{
return _containerBuilder.Build();
}
public void RegisterModule<T>() where T : Autofac.Module,new()
{
_containerBuilder.RegisterModule<T>();
}
}
我认为这种实现和编写包装器有问题。
1 . 签名和输入/输出模型:我不完全知道应该在包装器中写入哪些具有哪些签名的函数。
2 . 不同第三方的实现:为了创建一个容器,我必须在构造函数中拥有它,并且无法实现创建容器方法。
我希望用我的包装器处理我的模块化应用程序中的依赖注入。
为模块化 Web 应用程序执行此操作的正确方法是什么?
解决方案
推荐阅读
- android - 使用 Youtube App 等选项列表创建弹出窗口
- html - 如何使 HTML 列表环绕浮动内容
- mysql - 不能写重复键,使用组合主键
- javascript - 如何在页面加载时停止执行某个 javascript
- vb.net - DataGridView 中的复选框。更改选中单元格的 Style.Backcolor
- javascript - 阿贾克斯返回整页
- erlang - 如何在 Erlang/Elixir 5 秒后自定义重启策略?
- c# - Photon PUN 2+ Unity 仅在我在本地运行两个实例时工作
- python - 在 Python 中将递归函数更改为迭代函数
- emscripten - 如何设置基本的 C++/C 编译,然后为 emscripten 构建链接脚本