简体   繁体   English

使用Simple Injector的控制台应用程序中的SetResolver

[英]SetResolver in console application using Simple Injector

I am looking to do the equivalent of this: 我正在寻找等效的方法:

DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

But in a console application. 但是在控制台应用程序中。 I also have cases where I need to do this in a worker role. 在某些情况下,我需要以工作者角色执行此操作。 I don't want to have to add a reference to the MVC dll all over the place. 我不想在整个地方都添加对MVC dll的引用。 I am wondering if there is an equivalent for non-mvc projects? 我想知道非mvc项目是否有等效项目? I need to stash the container I create so I can access it throughout my application. 我需要隐藏创建的容器,以便可以在整个应用程序中访问它。 I thought of creating a static variable, but I'd rather not do this. 我曾想创建一个静态变量,但我不想这样做。

I saw there is a CommonSerivceLocator nuget for Simple Injector but oddly it requires 2.8.1 and the latest non-mvc Simple Injector is 2.8.0. 我看到有用于Simple Injector的CommonSerivceLocator nuget,但奇怪的是它需要2.8.1,而最新的非mvc Simple Injector是2.8.0。

I don't want to have to add a reference to the MVC dll all over the place. 我不想在整个地方都添加对MVC dll的引用。

This indicates that you are not applying dependency injection but are doing Service Location , which is an anti-pattern . 这表明您不是在应用依赖项注入,而是在进行Service Location ,这是一种反模式

If you apply constructor injection to all your components, you will be able to let Simple Injector build up a complete object graph of dependent components (of many levels deep) without any of your components to know about the existence of an IoC container such as Simple Injector or an IoC abstraction such as the Common Service Locator and MVC's IDependencyResolver . 如果将构造函数注入应用于所有组件,则可以让Simple Injector建立依赖组件(深度很深)的完整对象图,而无需让任何组件知道IoC容器(例如Simple)的存在注入器或IoC抽象,例如Common Service Locator和MVC的IDependencyResolver This prevents you from referencing your IoC container or such abstraction "all over the place". 这样可以防止您“遍地”引用IoC容器或此类抽象。

So in a Console Application that means that you typically have one place where you resolve the object graph(s). 因此,在控制台应用程序中,这通常意味着您有一个解析对象图的地方。 This is the place in the application that already knows about the existence of the IoC container. 这是应用程序中已经知道IoC容器存在的位置。 This place is typically referred to as the Composition Root . 此位置通常称为“ 合成根”

I saw there is a CommonSerivceLocator nuget for Simple Injector but oddly it requires 2.8.1 and the latest non-mvc Simple Injector is 2.8.0. 我看到有用于Simple Injector的CommonSerivceLocator nuget,但奇怪的是它需要2.8.1,而最新的非mvc Simple Injector是2.8.0。

I seem to have made an error in the build scripts that create the NuGet packages and although NuGet usually applies checks when a package is uploaded, for some reason it failed to check this. 我似乎在创建NuGet程序包的构建脚本中出错,尽管NuGet通常在上载程序包时应用检查,但由于某种原因,它无法对此进行检查。 I fixed this and pushed v2.8.2 of the CommonServiceLocator adapter to NuGet. 我修复了此问题,并将CommonServiceLocator适配器的v2.8.2推送到了NuGet。 But note that we are dropping support for the CSL adapter in v3 of Simple Injector because it leads to bad practice. 但是请注意,我们在Simple Injector的v3版本中放弃了对CSL适配器的支持,因为这会导致不良做法。 Don't use it; 不要使用它; you don't need it. 您不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM