M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

On Validating Event

M# framework raises events while applying data persistence of an entity instance, explained in tutorial Database.Save. This tutorial explains an event “OnValidating”, raised in M# framework when an entity instance is saved or updated, which triggers server side validation.

This event is very useful when you have to perform any actions on entity instances just before validation happens. M# framework implements a public virtual method “OnValidating” in “Entity” abstract class which should be overridden to perform any complex initialization of values.

Important: This method must not be used to perform custom validation rules. You should always write the business validation rules in the “Validate” method as explained in the Introduction of this chapter.

For the purpose of understanding the importance of this event, let’s take a scenario of generating a discount voucher for a new member of an ecommerce website. We generate a discount voucher for our new member as a welcome treat. For this purpose, we have created a mandatory and unique property “DiscountVoucher” of type string in our Member “entity” to store and accommodate the discount voucher code. We have also implemented a method which generates a random discount voucher.

We can use the “Default” attribute on “DiscountVoucher” property to initialize it with a new voucher code (please read tutorial Properties for more inflation on property attributes) but an error could occur if the voucher code has already been generated for another member.

Although, we have implemented the logic in our method to generate a unique voucher code by comparing it against existing vouchers in database, but as the website serves many users on the internet and we are populating “DiscountCode” property at the time of initializing new “Member” instance for registration form, the probability of generating same voucher code is high as it might have already been assigned to another “Member” whose registration process hasn’t yet completed.

In this case, we can benefit by moving our voucher generation code from initialization to just before saving an entity instance. We can override the “OnValidating” method of in “Member” logic class as shown below:

The screenshot above shows the overridden “OnValidating” method in the logic partial class of “Member” entity type (Partial Classes and Business Logic provides more details on partial classes). “IsNew” property of the entity instance is checked to validate it is a new instance of “Member” entity type (The Role of IsNew provides more detail on “IsNew” property) and “DiscountVoucher” is populated.

As voucher generation and assignment happens just before saving new instances, the probability of generating same voucher code is very low because, the time gap between generating a new voucher and saving it in database is reduced.