Something like this...makes it possible to react to EPiServer events like page published etc....
[ModuleDependency(typeof(EPiServer.Web.InitializationModule), typeof(StructureMapInitialization))]
public class PageEventsModule : IInitializableModule ....
public void Initialize(InitializationEngine context)
{
....
if (!_eventPublishingPageAttached)
{
DataFactory.Instance.PublishingPage += InstancePublishingPage;
_eventPublishingPageAttached = true;
}
...
}
public void Uninitialize(InitializationEngine context)
{
if (_eventPublishingPageAttached)
{
DataFactory.Instance.PublishingPage -= InstancePublishingPage;
}
}
I get the idea @Daniel Ovaska.
So every event you create needs to inherit with IInitializableModule ?
Ok, something like this:
1. Let's say you need to know when a page is published and deleted and send someone a mail when that happens. For a normal project you can easily add code to global.asax for application start to set up event handlers like the above.
In Application_Start you can add
DataFactory.Instance.PublishingPage += InstancePublishingPage;
DataFactory.Instance.DeletedPageLanguage += InstanceDeletedPageLanguage;
where InstancePublishingPage is the method you want to run when the page is published, in this case this would sent the email. Information about the page is sent in event args to the method btw. The eventhandler method has a signature like this:
private static void InstancePublishingPage(object sender, PageEventArgs e)
e.PageLink will give you a reference to the page that is being published if you need it.
So why use your own class that implements IInitializableModule and with the ModuleDependency attribute you might ask. Since you can do this easily using just global.asax. Well, sometime you don't have access to the global.asax because you are implementing an addon or similar for another site that you don't have the code to. Then you can use IInitializableModule and the attribute instead of global.asax and Application_Start. But for normal web projects, use Application_Start for this type of code instead. You might want a separate flag to make sure you never add this eventhandler more than once btw...
if (!_eventDeletedPageAttached)
{
DataFactory.Instance.DeletingPage += InstanceDeletingPage;
_eventDeletedPageAttached = true;
}
Does that answer your question?
and no, you never need more than one IInitializableModule normally. You can add eventhandlers to all events you are interested in there...
Hello!
I'm current trying to figure out how EventHandlers work.
This is how I think they work:
You need to implement the Interface IInitializableModule and also inherit from the class InitializableModule when creating an event.
Why do you create eventhandlers?
I think you create em for adding/changing for example a template of a pagetype in the CMS?
If you know how eventhandlers work and if I'm wrong. Please correct me :)