Create Event Handlers in .NET

This topic describes how to create a few simple event handlers for the Person entity to perform validations during the save process.

Subscribe to an Event

In this example we are working within the Logic project. This project contains our event handles and references the CMC framework and contracts that define the events.

Step 1: Add Required References

To utilize the CMC framework, you need to add a reference to Cmc.Core.dll.

  1. Open the EventHandlers.sln solution in Visual Studio.

  2. In Solution Explorer, right-click on Logic\References and select Add Reference…

  3. On the Browse tab, click the Browse… button.

  4. From the Select the files to reference… dialog, select SDKPath\Cmc.Core.dll and SDKPath\Cmc.FormsBuilder.Contracts.dll and click Add.

  5. From the Reference Manager dialog, click OK.

Step 2: Make your Assembly Visible to the CMC Framework

To make types defined within this assembly discoverable by the CMC framework, we need to add the ExtensionAssembly assembly level attribute.

  1. Within the Solution Explorer, open Logic\Properties\AssemblyInfo.cs.

  2. Add the [assembly: ExtensionAssembly] attribute to the file.

    //...
    [assembly: ExtensionAssembly]
    //...

Step 3: Create the EventSubscriber Type

During initialization, the EventService uses a container to discover all types that implement the IEventSubscriber interface. After discovery, the EventService invokes the RegisterHandlers method on each implementation of the interface, giving the implementer an opportunity to register event handlers.

The EventSubscriber type is an abstract class that simplifies the implementation of IEventSubscriber.

  1. In Solution Explorer, right-click on the Logic project and select Add -> Class…

  2. In the Name text box, enter FormTransitionEventSubscriber.cs.

  3. Click the Add button.

  4. Change the scope modifier of the newly added class to internal.

  5. Inherit the class from EventSubscriber.

  6. Click on the class name and pull down the smart tag to implement the abstract method, RegisterHandlers.


    using Cmc.Core.Eventing; namespace Logic { internal class FormTransitionEventSubscriber : EventSubscriber { public override void RegisterHandlers(IEventService eventService) { throw new System.NotImplementedException(); } } }

Step 4: Register an Event Handler

Next, implement the RegisterHandlers method to register a handler for the FormTransitionEvent that validates a FormEntity instance prior to it being saved to the database.

  1. Implement the abstract method RegisterHandlers to retrieve the SavingEvent from the provided IEventService. Register a handler for the Person type that does the following:

    1. Adds a validation message if there are no items in the Phones collection.
    2. Adds a validation message if there are no items in the Addresses collection.

    using System; using Cmc.Core.Eventing; using Cmc.FormsBuilder.Contracts; namespace Logic { public class FormTransitionEventSubscriber : EventSubscriber { public override void RegisterHandlers(IEventService eventService) { eventService.GetEvent<FormTransitionEvent>().RegisterHandler<FormEntity>((e, a) => { if (e.Fields.ContainsKey("CURRCNTRY") && (e.Fields["CURRCNTRY"] == "25")) { if (String.IsNullOrEmpty(e.Fields["CURRZIP"]) && !String.IsNullOrEmpty(e.Fields["CURRSTATE"])) { a.ValidationMessages.Add(new ValidationMessage("Current Zip is required for country USA")); } else { a.DefaultFields["CITIZEN"] = "8"; // Default to US } } else { a.DefaultFields["CITIZEN"] = "3"; // Default to Non-US Citizen } }); } } }

Test the Library

Copy the Logic\bin\Debug\Logic.dll to the bin folder of your host application and create a Person without any phone numbers or addresses. When you save the Person, you should receive two errors.