You are here: Catalog System > Catalog How To Code Samples > Writing Event Handlers

Writing Event Handlers

Key classes and files involved

How it works

In the Events element of the ecf.catalog.config configuration file, you reference types to be used by the EventContext class that perform specific functions as a result of events in the Catalog System. CatalogLogger is provided by default. You can specify more types with the requirement that they must implement the interface ICatalogEventListener.

<Events>
<add type="Mediachase.Commerce.Catalog.Loggers.CatalogLogger, Mediachase.Commerce"></add>
</Events>


In the EventContext class the LoadInterfaces() method is used to register handlers for all the specific events defined within it.

/// <summary>
/// Loads interfaces and binds them to appropriate delegates
/// </summary>
private void LoadInterfaces()
{
// Find assembly that contains the needed class
EventCollection events = CatalogConfiguration.Instance.Events;
if (events != null)
{
foreach (EventDefinition eventDef in events)
{
Type type = Type.GetType(eventDef.ClassName);
 
if (type == null)
{
// Log Exception here
continue;
}
 
if (type.IsAbstract)
continue;
 
foreach (Type intf in type.GetInterfaces())
{
if (intf == typeof(ICatalogEventListener))
{
ICatalogEventListener listener = (ICatalogEventListener)Activator.CreateInstance(type);
 
EntryUpdating += new EntryEventHandler(listener.EntryUpdating);
EntryUpdated += new EntryEventHandler(listener.EntryUpdated);
 
NodeUpdating += new NodeEventHandler(listener.NodeUpdating);
NodeUpdated += new NodeEventHandler(listener.NodeUpdated);
 
CatalogUpdating += new CatalogEventHandler(listener.CatalogUpdating);
CatalogUpdated += new CatalogEventHandler(listener.CatalogUpdated);
 
AssociationUpdating += new AssociationEventHandler(listener.AssociationUpdating);
AssociationUpdated += new AssociationEventHandler(listener.AssociationUpdated);
 
RelationUpdating += new RelationEventHandler(listener.RelationUpdating);
RelationUpdated += new RelationEventHandler(listener.RelationUpdated);
 
// Save a handle
_Handles.Add(new ObjectHandle(listener));
}
}
}
}
}

In the specific implementation of ICatalogEventListener, CatalogLogger here, the interface member is implemented and work is done. Below is the example of implementing ICatalogEventListener.EntryUpdating. A handler for the CatalogEntryRowChanging of the CatalogEntryDto.CatalogEntryDataTable within the CatalogEntryDto is registered. Within the handler, a call to the private method Record() is made.

/// <summary>
/// Entries updating.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="Mediachase.Commerce.Catalog.Events.EntryEventArgs"/> instance containing the event data.</param>
public void EntryUpdating(object source, EntryEventArgs args)
{
CatalogEntryDto dto = (CatalogEntryDto)source;
 
if (_admin == null)
_admin = new LogAdmin(new LogDto());
 
dto.CatalogEntry.CatalogEntryRowChanging += new CatalogEntryDto.CatalogEntryRowChangeEventHandler(CatalogEntry_CatalogEntryRowChanging);
}
 
// ...
 
/// <summary>
/// Catalogs the entry_ catalog entry row changing.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
void CatalogEntry_CatalogEntryRowChanging(object sender, CatalogEntryDto.CatalogEntryRowChangeEvent e)
{
Record(e.Row, e.Action);
}
 
// ...
 
/// <summary>
/// Records the specified entry.
/// </summary>
/// <param name="entry">The entry.</param>
/// <param name="action">The action.</param>
private void Record(CatalogEntryDto.CatalogEntryRow entry, DataRowAction action)
{
if (
action == DataRowAction.Commit ||
action == DataRowAction.Delete ||
action == DataRowAction.Change
)
{
if (entry.RowState == DataRowState.Added ||
entry.RowState == DataRowState.Deleted ||
entry.RowState == DataRowState.Modified
)
{
LogDto dto = _admin.CurrentDto;
LogDto.ApplicationLogRow row = dto.ApplicationLog.NewApplicationLogRow();
row.ApplicationId = CatalogConfiguration.Instance.ApplicationId;
row.Created = DateTime.UtcNow;
row.Notes = String.Empty;
if (entry.RowState == DataRowState.Deleted)
row.ObjectKey = entry["CatalogEntryId", DataRowVersion.Original].ToString();
else
row.ObjectKey = entry.CatalogEntryId.ToString();
 
row.Source = "catalog";
row.ObjectType = "entry";
row.Operation = entry.RowState.ToString();
row.Succeeded = true;
row.Username = GetUsername();
row.IPAddress = GetIPAddress();
dto.ApplicationLog.AddApplicationLogRow(row);
}
}
}

 


Last updated: 2011-05-17 | Copyright © EPiServer AB | Send feedback to us