Lab 04 - Entity Validation

Duration: ~5mins

The validation of entities' properties is automatically performed using partial methods.

Here an example to validate the Email value of the Client entity.

Open the Models\Entities\Client.custom.cs file and add the partial OnEmailValidation() method.

namespace Com.Example.Labs.Models
{
    using System;

    using Com.Example.Labs.Core;

    public partial class Client
    {
        #region [ Initialize Method ]

        protected override void Initialize()
        {
            // ...
        }

        #endregion

        #region [ Calculated Properties ]

        public string Fullname
        {
            get { return string.Format("{0} {1}", this.Firstname, this.Lastname); }
        }

        #endregion

        #region [ Validation Methods ]

        // Enter the 'partial' keywork and press 'SPACE' key to get the partial methods that can be defined...

        partial void OnEmailValidation(ref TranslationEnum? error)
        {
            if (!ValidationHelper.IsEmailAddress(this.Email))
            {
                error = TranslationEnum.ModelClientEmailBadValueOrFormat;
            }
        }

        #endregion
    }
}

Finally to validate the entity invoke the IsValid() method. All the defined partial methods are automatically invoked.