Module Extensibility - Dynamic Dependencies

Topics: Customizing Orchard, General, Writing modules
Jun 24, 2011 at 11:52 PM

Do we have any way to find/bind new implementations of interfaces at runtime?  For example, say I write a module that has an interface called ISomeService.  Say I have a couple default implementations in my module.  In that module, I've got a dropdown somewhere that allows users to select the implementation that they want for a specific content item. 

Say that user wants to create their own implementation (not override, as you would with OrchardSuppressDependency), so they create a new implementation and I can include it as an option in the drop-down.

Basically, I'm looking for something like DependencyResolver.Current.GetServices<ISomeService>();

I'm not too familiar with Castle, however with Ninject I know you can do named binding.  This would remove the need to being tied to having to save the fully qualified namespace/assembly/class or whatever.  I could do something like this:

Bind<ISomeService>().To<MyImplementation1>().Named("Some sweet implementation");
Bind<ISomeService>().To<MyImplementation2>().Named("A really crappy implementation");

And you can access it like this:

kernel.Get<ISomeService>("Some sweet implementation");

Thoughts?  Maybe I'm missing something, and there's some fantastic way to provide an extensibility point like this for modules.

Jun 24, 2011 at 11:57 PM

Just inject an enumeration of your interface type, and then select for those with a certain property value.

Jun 25, 2011 at 12:07 AM

Hey Bertrand, thanks but I'm not sure I follow.  Can you give me a simple example of what you mean?

Jun 25, 2011 at 12:09 AM

If you inject an IEnumerable<ISomeService>, you will get one of each known implementation. You can then call Select on the enumeration to extract one that satisfies whatever criterion you want.

Jun 25, 2011 at 2:37 AM

Ah gotcha, sorry I was thinking you meant an enum.  Thanks!