Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dependency injection 有关引导和ioc容器的最佳实践是什么?_Dependency Injection_Inversion Of Control_Ioc Container_Bootstrapping - Fatal编程技术网

Dependency injection 有关引导和ioc容器的最佳实践是什么?

Dependency injection 有关引导和ioc容器的最佳实践是什么?,dependency-injection,inversion-of-control,ioc-container,bootstrapping,Dependency Injection,Inversion Of Control,Ioc Container,Bootstrapping,以下是我的故事: 我正在开发一个不大的WPF/C应用程序,它(第一次)借助Unity框架实现控制反转。基本完成了。我的问题来自我的“bootstrapper”类,我在Unity容器中注册我的类型。由于涉及到大量的类,我的引导程序变得比我想要的要长得多,尽管有评论/区域,但要坚持这一点对我以外的任何人来说都是痛苦的。所以我想知道是否有更好的方法来处理注册。我在想,也许: 1。“你在抱怨什么?每个人都是这样做的”嗯,我怀疑 2。我误解了国际奥委会的兴趣,我有太多的课程需要注册。那么,国际奥委会的兴趣

以下是我的故事:

我正在开发一个不大的WPF/C应用程序,它(第一次)借助Unity框架实现控制反转。基本完成了。我的问题来自我的“bootstrapper”类,我在Unity容器中注册我的类型。由于涉及到大量的类,我的引导程序变得比我想要的要长得多,尽管有评论/区域,但要坚持这一点对我以外的任何人来说都是痛苦的。所以我想知道是否有更好的方法来处理注册。我在想,也许:

1。“你在抱怨什么?每个人都是这样做的”嗯,我怀疑

2。我误解了国际奥委会的兴趣,我有太多的课程需要注册。那么,国际奥委会的兴趣是什么

3。我只是创建了一个包含太多不同类的馄饨应用程序,我应该合并一些,我想我已经尽了最大努力找到一个平衡点

4.我不应该有一个宏引导程序,但是主引导程序应该调用专门的引导程序,例如Library的引导程序好吧,听起来不错。毕竟,天秤座的人自己应该比其他人更清楚他们所包含的东西。但我绝对不知道如何处理它而不让我的容器在每个库中移动,这些库依赖于Unity

那么,这一点是好的,还是我只是错过了一篇关于处理注册的好方法,以及任何人都可以使用的舒适的可维护性的帖子

PS:我没有在标签中提到technos,因为这个问题对于使用ioc容器的每个应用程序来说都很常见

编辑:为bootstrepper(错误命名的引导加载程序)添加的代码 这是一个旧版本,到现在它已经变长了,大约+15-20%

类引导加载程序
{
公开募捐
{
#区域启动屏幕
Sequenceur.IHM.Views.SplashScreen screen=新Sequenceur.IHM.Views.SplashScreen();
SplashScreenViewModel screenVM=新的SplashScreenViewModel();
screen.Show();
#端区
#区域容器初始化
UnityContainer容器=新的UnityContainer();
container.RegisterType();
#端区
#区域DAL注册
container.RegisterType();
container.RegisterType();
container.RegisterType();
#端区
#区域BOL注册
//序列准备
container.RegisterType();
container.RegisterType();
container.RegisterType();
//序列处理
RegisterType(新的ContainerControlledLifetimeManager());
//结果持久性
container.RegisterType();
RegisterType(新的ContainerControlledLifetimeManager());
#端区
#区域更新注册
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
RegisterType(新的ContainerControlledLifetimeManager());
#端区
#区域助手注册
//散列
container.RegisterType();
//配置
container.RegisterType();
container.RegisterType();
//伐木
container.RegisterType();
//信使
RegisterType(新的ContainerControlledLifetimeManager());
RegisterType(新的ContainerControlledLifetimeManager());
RegisterType(新的ContainerControlledLifetimeManager());
RegisterType(新的ContainerControlledLifetimeManager());
RegisterType(新的ContainerControlledLifetimeManager());
container.RegisterType();
#端区
#区域IHM注册
container.RegisterType();
container.RegisterType();
container.RegisterType();
#端区
#区域转换器注册
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
container.RegisterType();
#端区
#区域启动
ShellView mainView=新的ShellView();
mainView.DataContext=container.Resolve();
screen.Close();
mainView.Show();
#端区
}
}
这位名人写了一篇博文解释。他基本上说的是,当只使用“显式注册器”(Explicit Register)时,DI容器的用处是有限的,容器中每个注册类型都有一行代码(这就是您正在做的)。真正的好处始于使用约定而非配置。使用约定优先于配置,您将能够在一行代码中注册大量类型,并且可以防止组合根成为维护噩梦

虽然我并不完全同意“显式寄存器”模型的一些用法,但他说得对,使用约定而不是配置可以获得最大的好处。在您的应用程序中,已经有几个明显的点可以让您受益于约定而不是配置,这是在注册通用抽象时,例如
IManageData
、IConvertModels和
IMessagePublisher

因此,您可以创建以下方便的扩展方法:

public static void RegisterManyForOpenGeneric(此UnityContainer容器,
类型openGenericServiceType,参数程序集[]程序集)
{
container.RegisterTypes(
所有类中的from类型。FromAssembly(程序集)
惠尔