问题标签 [ioc-container]

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 回答
516 浏览

vb.net - VB .Net(WebForms)中的结构图

我正在尝试将 StructureMap 挂接到现有的 webforms 应用程序中。由于它是网络表单,我必须使用 Setter Injection,这并不理想,但总比没有好。

我要解决的问题是翻译成 VB(我真的是一名 C# 开发人员,目前在 VB 商店工作)。我编写了一个自定义扫描仪,它在 C# 中运行良好,但我完全不知道如何将它翻译成 VB。

原始的 C# 如下所示:

我能在 VB 中得到的最接近的是:

我希望反射器能够帮助我,但它提供的代码与我的类似,也无法编译。

那么,翻译是什么?

0 投票
1 回答
1164 浏览

dependency-injection - 使用 IoC 容器检索运行时设置的最佳方式

我有一个 C# dll 项目,我必须将运行时设置存储在外部 XML 文件中,并且此 dll 将用于 ASP.NET/ASP.NET MVC 应用程序,我还必须将运行时设置存储在外部文件。

哪个 IoC 容器可用于创建具有从特定外部文件(或 app.config/web.config)加载的运行时设置的对象,并且还适用于以中等信任度运行的 Web 应用程序?任何方法/教程将不胜感激。

到目前为止,我只找到了这篇文章:

更新

根据当前的记录类型,我正在从我的 dll 向可变数量的 SMTP 服务器发送邮件。对于类型 A,我使用给定的 SMTP 服务器+端口,对于类型 B,我使用一组备用服务器+端口值。当然,我希望能够在部署后修改这些值,所以我将它们存储在一个 XML 文件中。

如果我将 SMTP 设置存储为具有 2 个属性的 SMTPConfiguration 类(SMTPServer 作为字符串,SMTPPort 作为 Int32),是否可以根据给定的记录类型从 IoC 容器返回所需的对象,最好的方法是什么读取运行时设置以构建返回对象?

更新2

假设我在配置文件中存储了以下参数:ASMTPServer、BSMTPServer、ASMTPPort、BSMTPPort。我可以使用 Castle DictionaryAdapter 将所有这些设置读取为 AppConfiguration 类的属性。

如果我使用 A 类记录作为参数,那么指定所需 SMTPConfiguration 类应使用 ASMTPServer 和 ASMTPPort 值的推荐方法是什么(如果我使用 B 类记录作为参数,则应使用 BSMTPServer 和 BSMTPPort 值) ? 另外,如何指定在此过程中需要 AppConfiguration?

0 投票
1 回答
746 浏览

c# - Castle IoC - 支持 Release(...) 的 ISubDependencyResolver?

是否可以构建类似于 ISubDependencyResolver 但也支持 Release(...) 的东西?

我有一种情况,我希望能够在 Blender 的构造函数中解析 Fruit 的派生类:

不幸的是,Apple 处于不同的程序集中,除非需要,否则我不想加载,因为有数百种不同的 Fruit 都有自己的程序集。

ISubDependencyResolver 非常适合这个,除了我的一些水果是一次性的,所以我需要一种方法来释放它们。

从 DefaultDependencyResolver 派生是实现此目的的唯一方法吗?

编辑:更全面的例子。

基本上我想要一种将“new Apple()”视为需要处理的瞬态对象的方法。我很高兴在这个上采用完全不同的轨道,但是需要在解析时(而不是启动时)加载“Apple”类型。

0 投票
4 回答
1573 浏览

c# - Typical IoC container usage - passing data down the line

I've recently started using an IoC container for the first time, but I'm not educated on the best practices for using it. More specificaly I'm using Unity in a C# .NET project, and I started using it because it came with Prism.

I use the container to resolve the "top level" objects, and they get the correct objects injected based on the container. However, I can't see the best practice clearly when I have an object with children and children's children, and I need some data from the IoC container all the way down, but not in between. How you'd typically organize the use of IoC container?

Initially I'd think that you'd pass the container everywhere it is needed instead of extracting the needed data from the container on top-level and passing this data on. But then again I get problems when I reach objects which take other specific data in addition to the injected interfaces, and I'd prefer not to inject these through properties or init-methods after resolving the object.

I hope this was clear enough, but let's look at a fictional (and slightly stupid..) example.

So, this example doesn't really make much sense, but the essence is the same of what I need to do. I have some application commands I pass down the line through my ViewModel classes, because some of them need to be able to trigger commands to display something. I also have common storage, etc. which may be needed for some classes but currently are passed through and stored in middle classes. With only commands it's no big deal if you store commands or container, but would one in a typical IoC-usage pass the IoC container instead, and use this for resolving objects down the line? And what about specific data like the customer name? You can't just pass this in on the Resolve(), so you need to inject that afterwards?

Sorry - this was as short as I was able to make it. Won't require answers of the same length ;-) .. Just; what's the best practice of doing stuff like this with IoC containers?

0 投票
2 回答
3483 浏览

inversion-of-control - AutoFac 自动装配约定

StructureMap 能够在扫描时应用约定。因此 IFoo => Foo,没有显式注册。

AutoFac 中是否有类似的功能?环顾四周,找不到任何有用的东西。

谢谢,

0 投票
1 回答
185 浏览

asp.net-mvc - 如何使用 Windsor 将依赖项注入 ASP.NET MVC 模型?

我有一个模型类需要访问我的存储库类(用于数据库访问)。

我已经为我的存储库创建了一个接口,并成功配置了 Castle Windsor,以通过自定义 ControllerFactory 将我的适当的基于 IRepository 的类注入到我的控制器中。

我在弄清楚如何对我的模型做同样的事情时遇到了一些麻烦。

有谁知道使用 Windsor 将依赖项注入 MVC 模型的方法?

顺便说一句,我需要 Windsor 来处理这个问题的原因是,当数据发布到我的控制器时,MVC 会自动实例化我的模型实例,而这种自动实例化不允许我传递任何构造函数参数。

0 投票
7 回答
1709 浏览

asp.net-mvc - 如何在 ASP.NET MVC 模型中使用依赖注入?

我想将依赖项注入到 ASP.NET MVC 模型中,但我不知道在管道中的哪个位置进行注入。

使用 ControllerFactory 非常简单,但在处理模型时却没有那么简单。

0 投票
4 回答
1868 浏览

.net - IoC 容器之间的差异

我正在寻找一些关于如何为 ASP.NET MVC 应用程序选择 IoC 容器的指导。

(例如)StructureMap、Ninject、Castle Windsor、Unity、autofac 和其他有什么区别?任何人都可以提供一些可能有助于选择一个库的提示或资源链接吗?

更新:有一个问题(Enterprise Library Unity vs Other IoC Containers)讨论了 IoC 容器初始化的差异。

但是在功能上是否存在任何差异,这会使某些 IoC 容器成为 ASP.NET MVC 应用程序的更好选择?

0 投票
2 回答
1443 浏览

dependency-injection - 依赖注入最佳实践

我在我的代码中使用依赖注入(使用Ninject)并认为我做得很好,直到我遇到一个性能问题,这是由于误解了 DI 容器适合你的代码的位置。似乎有很多关于如何使用 DI 框架的信息,但没有太多关于在哪里不使用它们或如何最好地使用它们的信息(至少我能找到)

我想我会写出我认为的一些最佳实践,看看其他人是否同意我的观点,以及人们可以提出哪些其他最佳实践。

  • 每个应用程序或 AppDomain 使用一个内核
  • 仅对长寿命的单例对象使用 DI 容器,对短寿命的瞬态对象使用工厂(或其他方法)
  • 构造函数注入优于属性或字段注入
  • 请求对象,不要构建它们
  • 其他??指向好的博客全文/文章的指针??
0 投票
1 回答
222 浏览

inversion-of-control - 除了 DI,我还能从 IoC 容器中获得什么好处?

IoC 框架只是让 DI 变得更加容易,但它们所做的不仅仅是 DI,它们还提供了许多超越 DI 的其他好处。

我在一个关于 DI 和 IoC 的问题的答案中看到了这个评论。有人可以更详细地解释一下吗?IoC 的其他好处是什么?