Best way to ensure something gets run before IDependency gets instantiated

Topics: Writing modules
Developer
Nov 19, 2011 at 4:56 PM
Edited Nov 30, 2011 at 1:41 PM

I'd like to run some code (actually Autofac calls to register generic types, circumventing this issue) before any IDependency (in my case, a service class) gets instantiated. What would you advise, where is the best place for this?

I'd like to register generic types that can be consumed by a service class which is an IDependency. I thought the best place is in a handler's ctor, and that's good if the module where the dependencies are registered and consumed is the same. However if the registering module's name (where the handler is) comes after the consuming one's (where the service is) in the alphabet, than the handler gets instantiated after the service.

The Priority property in Module.txt doesn't seem to have any effect, although the module where the dependency is consumed is declared dependent on the module where the dependency is registered.

Thanks in advance!

Developer
Nov 30, 2011 at 1:41 PM

I thought maybe not the feature, but the module name should be set as a dependency, but it's not working either...

Nov 30, 2011 at 2:43 PM

Have you tried using an IModule?

Developer
Nov 30, 2011 at 2:57 PM

Thanks, although I haven't tried I think this would lead to the same problem: modules also have to be registered somewhere but I can't find a way to register anything before the service gets instantiated. Or were you hinting about something else?

Nov 30, 2011 at 3:34 PM

Interesting ... I'd assumed at some point that Module/IModule were autowired, didn't realise they needed manual registration.

Well, IOrchardShellEvents is the earliest thing you can hook into. Generally not many IDependencies will have been instanced by that point (and Priority / dependencies could help you try and make yours run first I guess)

Developer
Nov 30, 2011 at 4:24 PM
Edited Nov 30, 2011 at 4:29 PM

I was wrong, they get indeed picked up automatically. So dropping registrations in a simple module's Configure() works like a charm. Thank you very much, that'll come very handy. Also thanks for the IOrchardShellEvents tip, I think I'll need that one too. (Interesting is that modules get instantiated earlier than IOrchardShellEvents Activation() runs.)