如何让Autofac在Orchard CMS中进行属性注入

发布于 2024-11-25 11:02:29 字数 328 浏览 0 评论 0原文

是否可以使用 OrchardCMS 进行属性注入?

我知道 Orchard 使用 Autofac 并且 Autofac 确实进行属性注入,但我需要知道如何为 IOrchardServices 接口进行属性注入。

我们的团队正在考虑 Orchard,但我们的代码库全部采用 ASP.NET 4.0 WebForms,因此我们将继续提供 aspx 页面,并在时间允许的情况下慢慢将这些页面迁移到 Orchard。

这样,我们需要一种方法来访问 OrchardServices 对象。我想这是我必须自己想出的办法。 有人有在 Orchard 进行房产注入的好例子吗?

Is it possible to do property injection with the OrchardCMS?

I know that Orchard uses Autofac and that Autofac does do property injection, but I need to know how to do property injection for the IOrchardServices interface.

Our team is looking at Orchard but our code base is all in ASP.NET 4.0 WebForms and so we will continue to serve aspx pages and slowly migrate those said pages into Orchard as time permits.

With that, we'll need a way to get access to the OrchardServices object. I'm thinking that this is something I'd have to come up on my own. Does any one have any good examples of performing property injection in Orchard?

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

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

发布评论

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

评论(1

离笑几人歌 2024-12-02 11:02:29

这非常简单 - 查看源代码如何使用 ILogger 接口完成此操作,并对 IOrchardServices 执行相同的操作。源文件是Orchard.Framework/Logging/LoggingModule.cs。我想这正是您正在寻找的。
一切都是通过 Autofac 模块(Autofac.Module 类的实现)完成的。该类的作用是:

  • 注册 ILogger 接口的实现(Load 方法)并
  • 获取已处理对象的属性,并将适当的属性设置为从容器解析的对象(<代码> AttachToComponentRegistration 方法)。

很简单。 Autofac 模块是插入 DI 流程的好方法。

只需将该源文件复制到自定义 Orchard 模块并将 ILogger 更改为 IOrchardServices (当然还有注册的类)就足够了。我提到的类使用工厂模式来创建实例,但您可以通过 new 将其更改为简单的对象创建,并摆脱与工厂相关的东西。

It's pretty simple - look into the source how it's done with ILogger interfaces and do the same for IOrchardServices. The source file is Orchard.Framework/Logging/LoggingModule.cs. It's exactly what you are looking for, I guess.
Everything is being done via Autofac module (implementation of Autofac.Module class). What that class does is to:

  • register the implementation of ILogger interfaces (Load method) and
  • get properties of the processed object and set appropriate ones to object resolved from the container (AttachToComponentRegistration method).

Pretty simple. Autofac modules are a nice way to plug into the DI process.

It would be enough just to copy that source file to your custom Orchard module and changing ILogger to IOrchardServices (and, of course the registered class). The class I mentioned makes use of factory pattern to create instances, but you can change that to simple object creation via new and get rid of the factory-related things.

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