问题标签 [simple-injector]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
7559 浏览

c# - 简单注入器:在基类中注入属性

几个星期以来,我一直在使用Simple Injector依赖注入容器,并取得了巨大的成功。我喜欢轻松配置它。但是现在我有一个我不知道如何配置的设计。我有一个基类,其中有许多派生类型,我想将依赖项注入基类的属性中,但不必为每个派生类配置它。我试图用属性来做到这一点,但 Simple Injector 不支持属性。这是我设计的精简版。

我的配置现在看起来像这样:

如何在 BaseHandler 中注入属性?

提前感谢您的帮助。

0 投票
1 回答
1246 浏览

c# - 简单的注入器:RegisterInitializer 并不总是触发

我使用该RegisterInitializer方法在基类型中注入属性,如下所示:

这很好用,但RegisterInitializer不会对从 BaseHandler 继承的所有注册类型触发。当我打电话给自己时,它似乎没有运行new

为什么会这样,我该如何解决?

0 投票
1 回答
17251 浏览

.net - Simple Injector vs Hiro vs Autofac

我是 DI 和 IoC 的新手,我正在尝试决定要学习哪个 IoC 容器。我已经看到了几个性能比较,其中引用的三个容器似乎表现得非常好。但是,我没有发现包含 Hiro 或 Simple Injector 的功能比较。Autofac 的社区似乎是最大的,但 Hiro 和 Simple Injector 在基准测试中的速度非常快,特别是 Simple Injector 声称非常容易学习。也就是说,我不想学习一个并且不得不切换到另一个,因为功能集是有限的。出于这个原因,我倾向于 Autofac,因为它相当成熟并且功能完整(这里这里的好文章) 并且是可用的最快的 IoC 容器之一。有没有人使用过这三个容器中的至少两个?你能提供任何功能比较吗?

0 投票
1 回答
4253 浏览

c# - 如何使用 Simple Injector 模拟模块/安装程序/注册表

Autofac 有模块,Windsor 有 Installers 和 StructureMap Registries ... 使用 Simple Injector 如何将配置逻辑打包到可重用的类中?

我试过了:

我在合成根中使用它:

但是,FooModule取决于容器,可能不是一个好的做法......请参阅http://code.google.com/p/autofac/wiki/BestPractices

如果组件依赖于容器,请查看它们如何使用容器来检索服务,并将这些服务添加到组件的(依赖注入的)构造函数参数中。

0 投票
2 回答
157 浏览

generics - 如何在 SimpleInjector 中注册一个比 TService 更通用类型参数的 TImpl?

我现在正在做以下事情

但我想做这样的事情

这有可能吗?

0 投票
2 回答
1968 浏览

c# - How to do open generic decorator chaining with unity + UnityAutoRegistration

Went off on an interesting tangent today after reading this article on command handler decoration. I wanted to see if I could implement the pattern using Unity instead of SimpleInjector, and so far it is proving extremely difficult.

The first thing I had to do was install UnityAutoRegistration to resolve the open generic ICommandHandler<TCommand> interface. Current solution for that aspect is as follows:

This works for the first part, resolving any single ICommandHandler<TCommand>. What's proven frustrating so far is implementing a decoration handler. As soon as I add a second ICommandHandler<TCommand> as a decorator, Unity throws a StackOverflowException. I don't know enough about Unity internals, but I'm guessing this is because it can't figure out which instance to resolve to -- the command handler, or the command handler decorator -- since both implement the ICommandHandler<TCommand> interface.

Googling around led me first to this article, which explains how to do it in what I would consider a brute force method. I also found these related pages but none is a complete solution to my problem (and I am too ignorant to figure it out for myself).

I then found this article, which seems to address my same concerns. However beefy's solution does not account for dealing with open generics. Currently most of our dependencies are loaded from a unity section in the .config file, so I don't want to write a ton of compiled code for each handler or decorator. It seems like having some kind of UnityContainerExtension and DecoratorBuildStrategy is the right way to go, but I can't figure it out. I have been playing with beefy's code for a little while now, and am completely stuck. My attempts to modify his code to account for generics has led to BadImageFormatExceptions (An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)).

I like the idea of doing this to implement the decorator chaining, because it's short, and there is only 1 line per concern:

...but I don't want to change my container from Unity to Simple Injector if I don't have to.

SO my question is how could I go about implementing open generic decorator chaining using unity (plus UnityAutoRegistration)?

0 投票
1 回答
2080 浏览

asp.net-mvc - 每个 Web 请求的 EF DbContext + Custom RoleProvider = 每个 Web 请求的 RoleProvider 还是单例?

使用封装在接口中的 EF DbContext,为每个 Web 请求注入依赖项,以确保整个请求处理相同的上下文。还有一个自定义RoleProvider,它使用DbContextby 接口来自定义授权服务。

到目前为止,我一直在使用服务定位器模式来解析DbContextcustomRoleProvider的无参数构造函数中的实例。这导致了一些小问题,因为它RoleProvider是单例的,因此它可能会DbContext无限期地保留 a 而其他请求可能希望在Application_EndRequest.

我现在有一个基于此的解决方案,尽管使用了与 windsor 不同的 ioc 容器。我可以使用 DIRoleProvider为每个 http 请求新建一个自定义实例。

我的问题是,我应该吗?

有一个开放的DbContext悬挂RoleProvider似乎很浪费。另一方面,我知道每个 MVC 都会AuthorizeAttribute命中RoleProvider(如果它有一个非空Roles属性,我们大多数人都会这样做),所以我想已经有一个DbContext等待的可能很有用。

另一种方法是为不是每个 web 请求注入不同DbContext的。RoleProvider这样DbContext,只为 web 请求而存在的 s 可以在最后被处理,而不会影响单例RoleProvider

哪种方法更好,为什么?

评论后更新

史蒂文,这基本上就是我所做的。唯一的区别是我不依赖System.Web.Mvc.DependencyResolver. 相反,我在自己的项目中基本上有相同的东西,只是名称不同:

这些类是项目核心 API 的一部分,并且与 MVC 位于不同的项目中。这样,其他项目(连同域项目)就不需要依赖System.Web.Mvc它来编译它的DependencyResolver.

鉴于该框架,到目前为止,使用 SimpleInjector 替换 Unity 已经很轻松了。下面是多用途单例 RoleProvider 设置的样子:

网络配置:

IoC 容器:

至于 CUD,我的中只实现了 1 种方法CustomRoleProvider

这是 MVC AuthorizeAttribute(and IPrincipal.IsInRole) 使用的唯一方法,在所有其他方法中,我只是

由于提供者没有角色 CUD 操作,我不担心交易。

0 投票
1 回答
8456 浏览

dependency-injection - What is the correct way to register FluentValidation with Simple Injector?

I am able to register FluentValidation AbstractValidators using a FluentValidatorFactory. However, it doesn't feel right, because not all of the IoC container registrations happen during bootstrap / composition root. Instead, the fluent validators are registered by a separate factory:

The composition root:

An abstract fluent validator factory depending only on IServiceProvider

A fluent validator factory implementation for SimpleInjector

SimpleInjector has good support for open generics, and all of my fluent validator classes have signatures similar to the following:

So, is there a better way to register the validators in the bootstrap / composition root, instead of using fluent's validator factory?

P.S. @DotNetJunkie -- would be great if you had a wiki page on this at simpleinjector.codeplex.com.

0 投票
1 回答
3708 浏览

c# - 防止 Simple Injector 在解析未注册的服务时抛出异常

GetInstance(Of TService)我想知道 Simple Injector 是否可以选择在返回时停止抛出异常Nothing?它现在似乎正在抛出它们,因为我有两个请求来获取一个实例,它不存在,并且它抛出了异常。

有没有办法阻止默认行为、某处的设置或其他什么?

0 投票
1 回答
1308 浏览

.net - SimpleInjector 不使用隐式属性注入来注入属性

我有一个ObjectA,它有一个属性 for ObjectB,它有一个属性 for ObjectC,它们都在Simple Injector容器中。当我创建时ObjectA,我会调用InjectProperties它,它会加载ObjectB. 但是,此时,它不会加载ObjectC对新创建的 的引用ObjectB;它不执行我所说的对象的“深度积累”。

有没有办法启用它?

编辑

我有一个类对象A:

其中有一个属性ObjectB

所有这些都已使用容器上的标准注册方法进行注册。为了得到ObjectA,我使用它GetInstance<ObjectA>(),,这很好,我还构建了一个初始化程序来在创建时构建所有对象:

这在它解析时ObjectB注入ObjectA,但随后不ObjectC注入。ObjectBObjectB

有解决方法吗?谢谢。