Skip to content

Creating Validations

Eros Stein edited this page Jan 8, 2017 · 2 revisions

Validations are useful when we need to execute actions every time a record is created/modified/removed. Let's say that for every User that is created/modified we need to make sure the name is informed and the chosen username is not taken:

  1. Right-click your Validations folder inside the Domain project and select Add > New Item
  2. Select Blayer and choose Validate
  3. Use the same name you used for the POCO class screen shot 2017-01-06 at 17 01 51

A file is created with a similar content to this one:

using System.Linq;
using Blayer.Data;
using Your.Namespace.Poco;

namespace Your.Namespace.Domain.Validations
{
    /// <summary>
    /// Validators for User
    /// </summary>
    public class UserValidate : IValidate
    {
        /// <summary>
        /// Validates the entity User before saving
        /// </summary>
        /// <param name="state">Entity's current state</param>
        /// <param name="entityObject">Entity</param>
        /// <param name="originalEntity">Unmodified entity</param>
        public void Validate(System.Data.Entity.EntityState state, object entityObject, object originalEntity)
        {
            var entity = (User)entityObject;
            var dbEntity = originalEntity as User;
            var ctx = entity.Context;

            switch (state)
            {
                // If the entity was added (data creation)
                case System.Data.Entity.EntityState.Added:
                    {

                    }
                    break;
                // If the entity was removed (data removal)
                case System.Data.Entity.EntityState.Deleted:
                    {

                    }
                    break;
                // If the entity was updated (data update)
                case System.Data.Entity.EntityState.Modified:
                    {

                    }
                    break;
                default:
                    break;
            }
        }
    }
}

After we've added the validations it'll look like this:

using System.Linq;
using Blayer.Data;
using Your.Namespace.Poco;

namespace Your.Namespace.Domain.Validations
{
    /// <summary>
    /// Validators for User
    /// </summary>
    public class UserValidate : IValidate
    {
        /// <summary>
        /// Validates the entity User before saving
        /// </summary>
        /// <param name="state">Entity's current state</param>
        /// <param name="entityObject">Entity</param>
        /// <param name="originalEntity">Unmodified entity</param>
        public void Validate(System.Data.Entity.EntityState state, object entityObject, object originalEntity)
        {
            var entity = (User)entityObject;
            var dbEntity = originalEntity as User;
            var ctx = entity.Context;

            switch (state)
            {
                // If the entity was added (data creation)
                case System.Data.Entity.EntityState.Added:
                    {
                        // see [1]
                        if (ctx.GetRepository<User>().GetAll().Any(u => u.Username == entity.Username))
                            throw new BusinessException("The username you entered already exists, please choose another one.");
                        // see [1]
                        if (!string.IsNullOrWhiteSpace(entity.Name))
                            throw new BusinessException("Please inform the name.");
                    }
                    break;
                // If the entity was removed (data removal)
                case System.Data.Entity.EntityState.Deleted:
                    {

                    }
                    break;
                // If the entity was updated (data update)
                case System.Data.Entity.EntityState.Modified:
                    {
                        // see [1]
                        if (ctx.GetRepository<User>().GetAll().Any(u => u.Username == entity.Username && u.UserId != dbEntity.UserId))
                            throw new BusinessException("The username you entered already exists, please choose another one.");

                        // see [1]
                        if (!string.IsNullOrWhiteSpace(entity.Name))
                            throw new BusinessException("Please inform the name.");
                    }
                    break;
                default:
                    break;
            }
        }
    }
}

After that is done we need to go back to our repository class and tell it to use this validation. Like this:

using Blayer.Data;
using Your.Namespace.Poco;

namespace Your.Namespace.Domain.Repositories
{
    public class UserRepository : Repository<User>
    {
        public override IValidate GetValidate()
        {
            return new Validations.UserValidate();
        }
    }
}

That's it, now Blayer.Data knows about this customization.

Now every time we add or update a record of type User the name and username will be validated.

[1] BusinessException is included as part of the library, though you don't need to use. You can use your own custom exception.

Clone this wiki locally