Notification texts go here Contact Us Buy Now!

Custom Validation Interface in C# : Part 2

Custom Validation, .NET 8, Part 2
Console Bee

You can further extend this concept by adding additional validation rules or creating different validation interfaces for specific purposes 

Here are some ways to extend the concept of custom validation interfaces in C#:

1. Adding specific validation rules:

  • Extend the interface: You can define additional properties in the IValidatable interface to hold specific error messages or error codes, providing more detailed information when validation fails.
C#
public interface IValidatable
{
    bool IsValid { get; }
    string[]? ErrorMessages { get; }
}
  • Override in implementing classes: Each implementing class can then provide its own logic for setting these properties based on the specific validation rules it defines.
C#
public class Email : IValidatable
{
    public string Address { get; set; }

    public bool IsValid
    {
        get
        {
            bool valid = new System.Text.RegularExpressions.Regex(@"^[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[a-zA-Z]{2,}$").IsMatch(Address);
            if (!valid)
            {
                ErrorMessages = new string[] { "Invalid email format." };
            }
            return valid;
        }
    }

    public string[]? ErrorMessages { get; private set; }
}

2. Creating different validation interfaces for specific purposes:

  • Define specialized interfaces: Design separate interfaces that cater to specific validation needs, such as IUserValidation for user objects or IProductValidation for product objects.

  • Inherit from a base interface: Each specialized interface can inherit from the base IValidatable interface, providing core functionality while allowing for additional functionalities specific to the domain.

C#
public interface IProductValidation : IValidatable
{
    bool HasValidPrice { get; }
}

public class Product : IProductValidation
{
    public string Name { get; set; }
    public decimal Price { get; set; }

    public bool IsValid
    {
        get
        {
            return !string.IsNullOrEmpty(Name) && HasValidPrice;
        }
    }

    public bool HasValidPrice
    {
        get
        {
            bool valid = Price >= 0;
            if (!valid)
            {
                ErrorMessages = new string[] { "Price cannot be negative." };
            }
            return valid;
        }
    }

    public string[]? ErrorMessages { get; private set; }
}

3. Combining interfaces for complex validation:

  • Implement multiple interfaces: Classes can implement multiple validation interfaces if they require validation against different criteria.
C#
public class User : IValidatable, IEmailValidation
{
    // ... properties and implementation

    public bool IsValid
    {
        // Combine validation logic from both interfaces
    }

    public bool IsEmailValid
    {
        // Implement email validation logic
    }
}

By utilizing these techniques, you can create a more flexible and scalable validation framework that caters to diverse validation needs within your application. Remember to adapt these examples to your specific requirements and consider incorporating exception handling and error reporting mechanisms for a robust validation solution.

إرسال تعليق

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.
NextGen Digital Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...