Thursday 14 March 2019

custom exception on your business logic layer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Utilities.ExceptionHandler
{
    public class CustomRule
    {
        [JsonConstructor]
        public CustomRule(string name, string description)
        {
            Name = name;
            Description = description;
            ErrorCategory = CustomRuleEnum.Validation.ToStringValue();
        }

        public CustomRule(string errorCategory, string name, string description)
        {
            ErrorCategory = errorCategory;
            Name = name;
            Description = description;
        }



        public string ErrorCategory { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }


    public enum CustomRuleEnum
    {
        [StringValue("General")]
        General,

        // Use this for pre-processing checks; e.g. before any database activity
        [StringValue("Validation")]
        Validation,

        // Use this for any post checks cause from a data related issue after trying to process a request
        [StringValue("Data")]
        Data
    }

    public class StringValueAttribute : Attribute
    {
        public StringValueAttribute(string value)
        {
            Value = value;
        }

        public string Value { get; protected set; }
    }


   public static class ExtensionHelper
    {
        /// <summary>
        ///     https://cisart.wordpress.com/2013/09/11/string-value-attribute-for-enums/
        ///     Example Enum to String:
        ///     TestEnum test = TestEnum.Value2;
        ///     string str = test.ToStringValue();
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToStringValue(this Enum value)
        {
            var attributes =
                (StringValueAttribute[])
                    value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(StringValueAttribute), false);
            return ((attributes.Length > 0)) ? attributes[0].Value : value.ToString();
        }
    }

    public class CustomValidationException : System.Exception
    {
        public IEnumerable<CustomRule> CustomRules;

        public CustomValidationException(string message, IEnumerable<CustomRule> customRules)
                : base(message)
        {
            CustomRules = customRules;
        }

        public CustomValidationException(string message)
            : base(message)
        {
        }

        public CustomValidationException(string message, System.Exception inner)
            : base(message, inner)
        {
        }
    }
}

// apply on class

public bool IsValid()
        {
            return !CustomRules().Any();
        }

        public IEnumerable<CustomRule> CustomRules()
        {
            if (string.IsNullOrWhiteSpace(Name))
                yield return new CustomRule("MissingName", "Test name is required.");

        }

// apply on service method
 // validation
                if (!dbTest.IsValid())
                    throw new CustomValidationException("Validation Error", dbTest.CustomRules());



No comments:

Post a Comment