RSS

Knowledge Base / Datatypes / How to attach events to C...

How to attach events to Composite C1 Data Stores

Submitted Oct 06, 2008 by Mikkel Høy Sørensen

First you need to create a Delegate method, i called my method MyDataAfterAdd, second is to make a method for attaching method called AttachEvents. The rest of the code is just pluming code to make sure that events gets reattached after flushes

MyEventRegistrator.cs

using System;

using Composite.Data;
using Composite.Data.Types;

public static class MyEventRegistrator
{
    private static bool _registred = false;
    private static object _lock = new object();

    static MyEventRegistrator()
    {
        GlobalEventSystemFacade.SubscribeToPostFlushEvent(OnPostFlushEvent);
    }

    private static void OnPostFlushEvent(PostFlushEventArgs args)
    {
        Register();
    }

    public static void Register()
    {
        if (_registred == false)
        {
            lock (_lock)
            {
                if (_registred == false)
                {
                    AttachEvents();
                    _registred = true;
                }
            }
        }
    }

    private static void MyDataAfterAdd(DataEventArgs dataEventArgs)
    {
        IPage page = (IPage)dataEventArgs.Data; //page contains data just added

        //do your magic here
    }

    private static void AttachEvents()
    {
        DataEventSystemFacade.SubscribeToDataAfterAdd<IPage>(MyDataAfterAdd);
    }
}

Next we need to attach events on system initialization, so i Global.asax call the AttachEvents method after system initialization.

Global.asax

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="Composite" %>
<%@ Import Namespace="Composite.Logging" %>
<%@ Import Namespace="Composite.Application" %>
<%@ Import Namespace="Composite.ConsoleEventSystem" %>
<%@ Import Namespace="Composite.Instrumentation" %>
<%@ Import Namespace="Composite.Licensing" %>

<script RunAt="server">

    void Application_Start(object sender, EventArgs e)
    {
        AppDomainLocker.WaitForUnlock();

        TempDirectoryFacade.OnApplicationStart();
        Composite.Types.BuildManager.InitializeCachingSytem();

        if (Composite.Licensing.LicenseFacade.Registered == true)
        {
            Composite.GlobalInitializerFacade.InitializeTheSystem();
            LoggingService.LogInformation("Global.asax", "Application started and initialized");

            MyEventRegistrator.Register(); //add the Register methode call here
        }
    }

    void Application_End(object sender, EventArgs e)
    {
        try
        {
            if (Composite.Licensing.LicenseFacade.Registered == true)
            {
                Composite.EventSystem.GlobalEventSystemFacade.ShutDownTheSystem();
                Composite.Types.BuildManager.FinalizeCachingSytem();
            }

            LicenseFacade.Stop();

            TempDirectoryFacade.OnApplicationEnd();
        }
        finally
        {
            LoggingService.LogVerbose("Global.asax", string.Format("--- Web Application End, {0} Id = {1}---", DateTime.Now.ToLongTimeString(), AppDomain.CurrentDomain.Id));

            AppDomainLocker.Unlock();
        }
    }
</script>

That is it, now go replace "//do your magic here" with your stuff. This example is done with Composite.Data.Types.IPage, but this can be done with any thing that derives from Composite.Data.IData.

 
or cancel