Accessing Autofac container in BusinessLogic

Topics: Writing modules
Oct 31, 2012 at 5:02 PM

I'm trying to use the AutoFac container, to resolve named instances in BusinessLogic class in a CustomModule for Orchard.

I have found in the OrchardStarter.cs that the container = builder.Build(); is not exposed to the outside world.

I found that adding IOrchardServices in the constructor of my controllers, I can access the container _orchardServices.WorkContext.Resolve<>, but there is no option to retrieve named instances from the autofac container.

Am I missing something here? How can I access the container to retrieve named instances?

Coordinator
Oct 31, 2012 at 7:09 PM

Why do you need to go  against the container directly? Can't you implement IDependency and then inject? Why do you need to retrieve by name and not by type?

Oct 31, 2012 at 7:35 PM
We are building a product on top of orchard, for this specific implementation I have one interface and a concrete class that has a constructor string parameter. This parameter is a connectionstring to a different database to RavenDb. So I registered two ISessionHelper with named instances for resolving the correct ravendb session provider for a specific database. Also I don't like the sevice locator pattern to resolve in non controller classes.

So because we are using Orchard as a framework for our product, it would be great to have access to the container itself to do more advanced stuff.

From: bertrandleroy

Why do you need to go against the container directly? Can't you implement IDependency and then inject? Why do you need to retrieve by name and not by type?

Coordinator
Oct 31, 2012 at 7:44 PM

I don't know then, sorry.

Developer
Oct 31, 2012 at 7:48 PM

Perhaps you could inject an IEnumerable of your service interface, and select the desired implementation by name. Your service interface would have to implement a name property of course.