How to make Autofac perform property injection in Orchard CMS
Asked Answered
C

1

6

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?

Cb answered 19/7, 2011 at 15:41 Comment(0)
F
3

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.

Faeroese answered 24/8, 2011 at 22:27 Comment(1)
Excellent! Thanks for pointing out LoggingModule as an example, giving a concrete example of exactly what I need :DLorettelorgnette

© 2022 - 2024 — McMap. All rights reserved.