如何让Autofac在Orchard CMS中进行属性注入
是否可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这非常简单 - 查看源代码如何使用
ILogger
接口完成此操作,并对IOrchardServices
执行相同的操作。源文件是Orchard.Framework/Logging/LoggingModule.cs。我想这正是您正在寻找的。一切都是通过 Autofac 模块(
Autofac.Module
类的实现)完成的。该类的作用是:ILogger
接口的实现(Load
方法)并很简单。 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 forIOrchardServices
. 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:ILogger
interfaces (Load
method) andAttachToComponentRegistration
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
toIOrchardServices
(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.