Can those more familiar with Orchard CMS help me with a problem? I am writing an Orchard module and my controllers have constructor arguments (service interfaces).
Where in Orchard do I set up a custom controller factory to handle this (without interfering with other Orchard modules that a user has installed)
Is it something done in Global.asax? I have found
ControllerBuilder.Current.SetControllerFactory(new OrchardControllerFactory());
in OrchardStarter.cs so I'm confused as to where any other controller factories would be set.
Is it just done using Autofac DI? If so, if anyone has a short code sample that would be great! (I'm new to Autofac and Orchard)
Thanks
You shouldn't change the controller factory. Orchard provides easier ways to use dependency injection out of the box.
The easiest way to do this is to inherit your interface from Orchard.IDependency
. Orchard will then automatically register your implementation with Autofac and inject it into any constructor. If you need more control over the registration process, then you can create an Autofac module and register your service there. Orchard should also automatically pick up that module and add it to the dependency injection container.
For example if you create an interface:
using Orchard;
public interface IFoo : IDependency {
...
}
And create an implementation of your interface:
public class Foo : IFoo {
...
}
Then Orchard will automatically inject a Foo
instance to any constructor which defines a parameter of type IFoo
.