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
.
-
Open the
EventHandlers.sln
solution in Visual Studio. -
In Solution Explorer, right-click on Logic\References and select Add Reference…
-
On the Browse tab, click the Browse… button.
-
From the Select the files to reference… dialog, select
SDKPath\Cmc.Core.dll
andSDKPath\Cmc.FormsBuilder.Contracts.dll
and click Add. -
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.
-
Within the Solution Explorer, open
Logic\Properties\AssemblyInfo.cs
. -
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.
-
In Solution Explorer, right-click on the Logic project and select Add -> Class…
-
In the Name text box, enter FormTransitionEventSubscriber.cs.
-
Click the Add button.
-
Change the scope modifier of the newly added class to internal.
-
Inherit the class from EventSubscriber.
-
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.
-
Implement the abstract method RegisterHandlers to retrieve the SavingEvent from the provided IEventService. Register a handler for the Person type that does the following:
- Adds a validation message if there are no items in the Phones collection.
- 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.