您将如何将依赖解析器隔离到 MVC3 中的一个区域?

发布于 2024-10-03 10:10:02 字数 163 浏览 6 评论 0原文

本着最近隔离内容并通过 NuGet 将其插入的精神,有人知道如何将依赖解析器限制为 MVC3 中的一个区域吗?将视图和控制器限制在一个区域内很容易,但除非我没有看到明显的钩子,否则看起来设置 IDependencyResolver 不可避免地具有全局范围。我想将其限制在一个区域。

有什么建议吗?

In the recent spirit of isolating stuff and plugging it in via NuGet, does anyone have an idea about how you'd limit a dependency resolver to just one area in MVC3? It's easy enough to keep views and controllers limited to an area, but unless I'm not seeing an obvious hook, it looks like setting an IDependencyResolver is something that unavoidably has global scope. I'd like to limit it to just one area.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月光色 2024-10-10 10:10:03

IDependencyResolver 在设计上是全局的。如果您想按区域改变行为,您应该查看各种 *Activator 类型和接口,它们可以根据上下文对象做出决策。

你具体想做什么?

IDependencyResolver is global by design. If you want to vary behavior by area, you should look at the various *Activator types and interfaces that can make decisions based on context objects.

What specifically are you trying to do?

我是有多爱你 2024-10-10 10:10:03

我建议为此使用公共服务定位器。基本上每个区域都可以为其特定容器设置 CSL。

您可能需要在依赖项解析器之间创建一个适配器,或者完全放弃它并严格使用 CSL。

一般来说,我不支持在业务线应用程序中使用 CSL。其目的是让使用 DI(如 MassTransit)的开源组件更容易集成到业务线应用程序中。然而,这可能是一个例外。

另外,请查看 mvccontrib 可移植区域。它是为这种类型的事情而设计的。

I would suggest using the Common Service Locator for this. Basically each area could setup up the CSL for their specific container.

You will probably need to create an adapter between the dependency resolver or forgo it altogether and strictly use the CSL.

In general I am not a proponent of using the CSL in a line of business app. It's intent is to make it easier for open source components that use DI (like MassTransit) easier to integrate into line of business apps. This might be an exception however.

Also, look into the mvccontrib portable areas. It's designed for this type of thing.

梦纸 2024-10-10 10:10:03

你想做的事情听起来是个坏主意。依赖项注入的目的是让您可以隔离特定的依赖项,而不用让您的代码关心它们来自哪里。

如果您尝试将某些对象或类限制在某个 MVC 区域,请配置您的依赖注入器以在正确的时间调用正确的对象或类。

有关您正在尝试执行的操作的更多详细信息将有助于生成更好的答案。

What you're trying to do sounds like a bad idea. The point of dependency injection is so you can isolate specific dependencies and not have your code care where they come from.

If you're trying to restrict some objects or classes to a certain MVC area, configure your dependency injector to call the proper ones at the right time.

Some more details about what you're trying to do will help generate better answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文