Module standard entry point

Jan 31, 2011 at 4:34 PM
Edited Jan 31, 2011 at 4:36 PM

What should be considered as a standard entry point for Module? Like global.asax Application start event or BeginRequest one?

For now I am using Load method for Autofac module:

public class CommunitySecurityModule : Module 
{ 
    protected override void Load(ContainerBuilder builder) 
    {
           //your one-time operation here 
    }
 }

And also where should I put my code that will be performed only on feature "enable" or "disable" ? Thanks!
Coordinator
Jan 31, 2011 at 10:40 PM

Implement IFeatureEventHandler. There are several examples in the current code-base.

Coordinator
Feb 1, 2011 at 2:48 AM

Would you mind sharing what you are trying to achieve? I'm curious if this is a scenario we should better support (we never had a need for this for the modules we've written).

 

Thanks,

Renaud

Feb 1, 2011 at 2:55 PM
Edited Feb 1, 2011 at 2:55 PM

Thank you sebastienros.

2 rpaquay,

For instance it could be adapter registration for client side validation:

 

   DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(CreditCardCheckAttribute), typeof(DataAnnotationsCreditCardCheckValidator));

 

Or starting Rhino NHibernate profiler

 

 HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();

Or anything else that is needed to be configured/initialized in per-application lifetime basis.

 

Developer
Feb 2, 2011 at 12:22 AM

Hi!

If you need to register an object and make it have per-application lifetime you just have to make it implement ISingletonDependency. Orchard will register your instance under the hood with underlying Autofac container on a per-application (or more specifically - per-tenant - basis). Then, you can do your initialization logic simply in its constructor as it gets called only once.

You can also do it your way - by using Autofac.Module custom implementation. But in this approach the Load method gets called on every request (as far as I remember) and not on per-application basis. Remember that the Orchard module concept != Autofac modules.

  • The former is more sophisticated concept of ASP.NET MVC zones, loaded dynamically by Orchard framework.
  • The latter is just an object in your Orchard module that gets loaded into container (via RegisterModule method). You can have many of these in your Orchard module. It should be used in situations where you need to register your objects in a container in some fancy way or when you want to modify the underlying Autofac registrations. It is pretty low-level stuff you shouldn't play with when not absolutely necessary (consider abstract situation when Autofac gets replaced with othe IoC container...:))

Subscribing to Orchard module lifetime events can be done as Sebastien wrote - just implement IFeatureEventHandler and you're here.

Could you please elaborate a bit more about what you would like to achieve? It would help me find a more specific solution:)

Cheers, Piotr

Feb 2, 2011 at 1:50 PM

Hi pszmyd, thanks for your irrefragable reply.

According to my tests module Load gets called only once per application start. A very good point with ISingletonDependency, I can have some time with static constructor and call it like OnApplicationStart, but it looks like workaround. Is is nice to have native support for this.

>Remember that the Orchard module concept != Autofac modules.

Sure =)

It is pretty low-level stuff you shouldn't play with when not absolutely necessary (consider abstract situation when Autofac gets replaced with othe IoC container...:))

I will pray that this won't happen. We already have a code that is replacing some orchard services with our own ones. (IAutorizationService and IMembershipService)

Subscribing to Orchard module lifetime events can be done as Sebastien wrote - just implement IFeatureEventHandler and you're here.

Probably not a module, but a feature.

Could you please elaborate a bit more about what you would like to achieve? It would help me find a more specific solution:)

As I sad it is for instance enabling of MVC validation and enabling NHibernate profiler. You know, we have a pretty complex module now and having an ability to make some long-live object initialization is acommon thing.

Thank you once again for you effort!