问题标签 [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 投票
2 回答
1517 浏览

entity-framework - 在类库中注册容器(IoC)?

我正在使用以下部分创建一个 n 层应用程序。

  • MyApp.DAL - 数据访问层(EF 5,SQL 后端)
  • MyApp.BLL - 业务层
  • MyApp.WcfService - 服务层
  • MyApp.WpfClient - 前端
  • MyApp.Models 是一个包含所有 POCO 的共享项目。

我将通过每一层给出一个“国家”实体的例子。首先是模型。请注意,StatefulObject 将帮助我跟踪来自断开连接的客户端的实体状态(根据 Julia Lerner 的编程 EF 第 2 版第 18 章)。

在 DAL 中,我有一个 ICountryRepository(基本 CRUD)和一个 CountryRepository。这是构造函数。

我的 BLL 有一个 ICountryLogic、CountryLogic、IBusinessLayer 和 BusinessLayer,如下所示:

然后是一个示例服务方法,它实例化一个新的业务层并执行一些操作,如下所示:

然后 WPF 客户端可以像这样使用该服务:

现在,在将 WCF 服务放在 BLL 和 WPF 前端之间之前,我的 WPF 前端可以只使用依赖注入,我不会有这样的丑陋:

相反,我会像这样使用 DI 和构造函数注入:

然后只需在 WPF 中的容器中注册接口和实现即可。我的问题是如何在类库中注册它们(在 BLL 中)?由于类库中没有组合根或入口点,我不知道该怎么做。我正在使用 SimpleInjector。

或者,我可以在 Wcf 服务中使用 DI,但这需要服务具有对 DAL 的引用,以便它可以将 ICountryRepository 注册到 CountryRepository。这似乎不对。服务层应该只引用 BLL,这就是我希望在 BLL 中设置 DI 的原因。谢谢。

0 投票
1 回答
497 浏览

c# - 使用 Simple Injector 注册开放的通用单例

我有以下代码,并想为泛型的每个不同变体注册一个单例。这可能吗?目前,断言失败,因为它们不是相同的对象类型。

0 投票
1 回答
4535 浏览

c# - IoC Registration differences between Unity and Simple Injector

I have one project functioning perfectly using Unity. I try switching to use Simple Injector instead and now NO changes ever get saved in my database. I believe it has to do with the lifetime of the registered components. Here is the Unity container registration:

And here is the new Simple Injector registration.

I'm not sure how the HttpContextLifetimeManager comes into play with Simple Injector. MVC is the client for the unity example, but I'm changing to a WPF project and Simple Injector. Any suggestions are much appreciated. Thanks.


@Steven. Thanks for your comment. I just discovered that since my RepositoryBase and my UnitOfWork inject an IDatabaseFactory in their constructors that I needed to use container.RegisterSingle<IDatabaseFactory, DatabaseFactory>(). This resolved one issue. I still have a problem with lifetime though. Since my consuming app is WPF, how will the RegisterPerWebRequest work?

My project has a DataLayer >> BusinessLayer >> WcfService >> WPF Front end. Simple Injector is set on the WcfService project and the business layer has Boostrapper to register items there. As of now, my WPF client will GetAllCountries() and display in a grid. If I change the name of one and try to update, I get the "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." error. I've done some debugging and find that after the GetCountries service call in the WPF client, when I go back to try to update, I see ALL of the countries are attached to the context via dbContext.ChangeTracker.Entries(). At this point I should have NO entities being tracked as my context should have been disposed after the first unit of work.

In an MVC app the RegisterPerWebRequest fixes that, but what is the equivalent for WPF? I'm going to install the extension now and try it anyway but I have a feeling it isn't the solution I'm looking for.. or is it? Thanks again for the help.


OK. I did a bit more digging and found a solution that works. I'm just not sure if it's the correct one. Anyway, now in my BLL where there is a bootstrapper to register things, I can register like this:

That gives me what I was looking for. Only a single instance of DatabaseFactory is ever created and thus my repository and unit of work share it like they should. Also, after GetCountries() on the client, when I do my second call to the service to perform and update, I check the dbContext.ChangeTracker.Entries() and see that there are NO entities being tracked, which is correct. I can now attach, set to modify, and call SaveChanges without getting the duplicate key error. Does this seem ok? Thanks.

0 投票
1 回答
233 浏览

c# - SimpleInjector 是否有能力在使用 RegisterManyForOpenGeneric/GetAllInstances 时分配优先级顺序

我使用 SimpleInjector 作为我的 IoC 容器。我已经创建了一个发布/订阅框架,我现在需要能够通过让订阅者指出他们的执行优先级来对其进行改进。例如,我可以创建一个订阅者来预加载事务剩余部分所需的所有数据。

我能想到的一种方法是[SubscriberPriority]在注册过程中创建一个属性来指示需求,RegisterManyForOpenGeneric但我还没有深入研究。

有没有办法管理GetAllInstances返回注册的订单?

更新:

我刚刚想到的另一个选择是使用 CoC(Convention over Configuration)在实例返回时对它们进行排序GetAllInstances

0 投票
1 回答
3368 浏览

c# - 针对每个 Web 请求和生命周期范围的 SimpleInjector 混合生活方式

我正在使用 Simple Injector 作为我的 IoC 容器,并采用以下技术来为某些对象注册“混合”生活方式,无论是按 Web 请求还是每个线程。

我对这个解决方案并不完全满意,因为对于每个要求,我必须定义额外的空接口以使其工作并确保它们被我的具体类引用。

有什么理由我不应该使用以下扩展方法而不是定义额外的接口?如果这些方法存在问题,是否有其他方法可以完全确定我当前的容器实例正在 IIS 中运行?


更新

上述问题和后续问题是基于对 SimpleInjector 和混合注册的误解。上面和 SO 其他地方描述的技术适用于容器可以为 Web 请求和不在 Web 请求上下文中运行的后台进程的请求提供服务的情况。我一直在尝试实现的是变量注册,以适应适用于 Web 请求或的容器的配置线程请求的容器配置。即我需要将我的容器配置为在 IIS 中工作并在 Windows 服务中工作。我不需要可以同时满足两者的动态注册。

结果是以下扩展方法,我从我的解决方案中删除了“额外”接口:-)

0 投票
1 回答
465 浏览

c# - 您什么时候需要 SimpleInjector 混合注册?(如何在没有 HttpContext 的情况下调用 IIS 中托管的对象?)

我最近关于 SimpleInjector 和混合 Web 请求/线程生活方式的问题之后,我似乎没有完全理解技术要求,并且一直在做一些我实际上不需要做的事情。

使用此代码

我的理解是,对于在 IIS 中运行的 AppDomain,IWebUnitOfWork将返回,否则会出现错误,除非我明确声明 LifetimeScope 的实例来包装对容器的调用(它将返回IThreadUnitOfWork)。

以下陈述让我意识到我并不完全理解我一直在做什么!

但是,您似乎不需要混合生活方式。混合生活方式是一种可以动态切换的生活方式(在每次调用 GetInstance 和每次注入时),而您似乎只需要在启动期间进行切换。

我的问题是:在什么情况下可以在不存在 HttpContext 的情况下调用在 IIS 中运行的 AppDomain 中加载的容器(或任何其他类),无论是静态的还是实例的?

0 投票
1 回答
728 浏览

c# - Migration Ninject -> 简单注入器

我们Ninject在我们的项目中使用,但有一些性能问题。用 .测试同一个项目会很有趣Simple Injector

你知道一些使迁移更容易的指南吗?

0 投票
1 回答
67 浏览

c# - 调用链中同一个装饰器的多个注册

我正在使用 SimpleInjector 进行依赖注入,我刚刚发现我可以在单个调用链中多次注册相同的装饰器,并且将根据请求的配置应用装饰器。例如,我可以插入我的 RepositoryTraceDecorator 作为我的存储库类的第一个和最后一个装饰器:

这是设计使然,因此我可以依赖此功能吗?

0 投票
1 回答
138 浏览

c# - 使用 simpleinjector 注册 opengeneric

我有以下课程:

我需要将其注册SubIdentifierItemCreateCommandHandler为 singleton-open-generic,以处理对类型服务的任何请求 IDbCommandHandler<SubIdentifierItemCreateCommand<,>,>

这可能吗?我尝试了各种方法,但总是出错。

我希望能够拨打以下电话并工作:

0 投票
1 回答
2714 浏览

c# - 使用 Simple Injector abd WebFormsMVP 将运行时值传递给构造函数

我正在尝试将SimpleInjector 与 WebFormsMvp结合起来。

为方便 DI WebFormsMvp 提供了IPresenterFactory接口。
它包含Create提供要解析的演示者类型视图实例的方法。
我需要将视图实例注入到Presenter构造函数。 演示者还有其他需要容器创建的依赖项。

这是我到目前为止得到的,但并不理想。
问题的正确解决方案是什么?

演示者构造函数:

工厂: