In this tutorial I will show you how you can easily extend the various events that occur when records in the 42Windmills system are saved, loaded or deleted from the system. In order to demonstrate this I will change the OnSaving event of the Lease entity in our reference application 42Towers. After my modifications, whenever a Lease is saved, the system will call a (fictitious) webservice that checks if the credit card number entered is valid.

Step 1. Extend the OnSaving event.
Extend the partial factory class of the entity by creating a new file (any name you like) within the App.Lib solution in the BLL folder. The classname should be <prefix><entityname>Factory where prefix is the prefix of the application (standard 'new') and enityname is the name of the entity. In our case this will be: newLeaseFactory Next, we override the method AddEventHooks(). This method allows us to add eventhandlers for the various events to our code. In this case we add the newLeaseFactory_Saving method as an event handler for the Lease.saving event. (See code below)

Copy code using System; using System.Collections.Generic; using System.Linq; using System.Text; using App.Lib.CreditServiceSoap; namespace _42Towers { public partial class newLeaseFactory { /// <summary> /// Overridden function to add the event handlers /// </summary> public override void AddEventHooks() { this.Saving += new SavingEventHandler(newLeaseFactory_Saving); } protected void newLeaseFactory_Saving(newLeaseEntity entity, FortyTwoCancelEventArgs e) { try { CreditServiceSoapClient client = new CreditServiceSoapClient("CreditServiceSoap"); if (client != null) { ReturnIndicator ri = client.CheckCredit(entity.CreditcardNO); if (ri == null || !ri.CardValid) { entity.Issuer = "Not Valid"; } else { entity.Issuer = ri.CardType; } } } catch (Exception) { throw new Exception("Error connecting to Credit Check"); } } } }


Well, that's basically all there's to it! As you can see, the newLeaseFactory_Saving method works just like any other event handling method. In this example I've created a reference to a SOAP webservice and I am using this client to verify the credit card number, but the possibilities of what you could do in this method are endless. Note that I do NOT have to save the changes to the newLeaseEntity as I have overridden the Saving event, which is fired before the entity object is saved to the database. Any changes to this object are therefore directly entered into the database once the eventhandler is finished.