none
What validation approach to use for WCF Request object? RRS feed

  • Question

  • Hi,

    What approach should I implement for validation on request objects in WCF service?
    I have already implemented a class specifically designed for Validation. This class contains several overloaded methods that will validate different request object.
    On success it will return true and on failure it will return false and there is also a ref parameter that will contain the appropiate success/failure message.
    But the problem I am facing is that for each new type of Request object I have to define a new validation method and that is increasing day by day.
    I was searching any validation framework that will do the work in less code and also my existing application should not break due to any new changes.
    Can anyone suggest which approach should I implemet here?


    • Edited by NorthValley Monday, September 9, 2013 6:40 AM
    Monday, September 9, 2013 6:39 AM

Answers

  • You need to implement the validation as it's not automatic in this case. The System.ComponentModel.DataAnnotations.Validator class will help you on that.

    So at the place of when you want to validate you would need to do validation, below code show how.

    public bool SaveCustomer(Customer customer)
    {
        bool result = false;
    
        // Do validation
    
        //// validation context
        var context = new ValidationContext(customer, null, null);
        
        //// store validation result per attribute 
        var results = new List<ValidationResult>();
    
        //// try validate objects all properties, true = valid
        result = Validator.TryValidateObject(customer, context, results, true);
    
    
    
        return result;
    }

    • Marked as answer by NorthValley Tuesday, September 10, 2013 6:35 AM
    Monday, September 9, 2013 8:27 AM
  • You can create your own validation attribute by creating attribute that inherits ValidationAttribute class and override one of its methods to provide your own custom validation logic.

    Of course the business validation usually might require more advanced validation that can not be solved with validation attributes. For those cases you need the reqular business logic code to make sure that operation can be done.

    • Marked as answer by NorthValley Tuesday, September 10, 2013 6:35 AM
    Tuesday, September 10, 2013 5:11 AM

All replies

  • I get DataAnnotation after lot of search over internet, but I think on getting error it raises exception.
    Please clarify if anyone knows this.
    I do not want to raise any exception on getting invalid data. I just want to return false from a validation method and appropriate success/failure message.
    Is this possible through DataAnnotation?
    Monday, September 9, 2013 6:45 AM
  • I tested with DataAnnotation on request objects, but it is not giving any exception or any validation check.

    Created a class:

    [DataContract]
        public class Customer
        {
            [Required(AllowEmptyStrings = false, ErrorMessage = "First Name should not be empty.")]
            [DataMember]
            public string FirstName { get; set; }
    
            [Required(AllowEmptyStrings = false, ErrorMessage = "Last Name should not be empty.")]
            [DataMember]
            public string LastName { get; set; }
    
            [Required(AllowEmptyStrings = false, ErrorMessage = "Age should not be empty.")]
            [DataMember]
            public int Age { get; set; }
        }

    Service implementation is : 

    public bool SaveCustomer(Customer customer)
            {
                bool result = false;
    
                // Do validation
    
    
                return result;
            }

    Client code is :

     public static void Main()
            {
                Service1Client client = new Service1Client();
                Customer cust = new Customer();
                cust.FirstName = "";
                cust.Age = 23;
    
                bool res = client.SaveCustomer(cust);
           
            }

    Since new Customer object contains invalid data, i was expecting any exception or anything else in WCF side when this request object was received, but nothing such exception or validation happens.
    WCF simply retuens false.
    Can anyone suggest what can I do here ?

    Monday, September 9, 2013 7:36 AM
  • You need to implement the validation as it's not automatic in this case. The System.ComponentModel.DataAnnotations.Validator class will help you on that.

    So at the place of when you want to validate you would need to do validation, below code show how.

    public bool SaveCustomer(Customer customer)
    {
        bool result = false;
    
        // Do validation
    
        //// validation context
        var context = new ValidationContext(customer, null, null);
        
        //// store validation result per attribute 
        var results = new List<ValidationResult>();
    
        //// try validate objects all properties, true = valid
        result = Validator.TryValidateObject(customer, context, results, true);
    
    
    
        return result;
    }

    • Marked as answer by NorthValley Tuesday, September 10, 2013 6:35 AM
    Monday, September 9, 2013 8:27 AM
  • Hi MasaSam,

    Your logic looks good. But one thing I am confused is how can I write code to apply custom validation on any property. 
    e.g., in my Customer class if for any property I need to implement any further validation that does not exist in the DataAnnotation framework then how can I do those custom validations provided actual validations code provided by you remains the same.

    Regards,

    Monday, September 9, 2013 9:34 AM
  • You can create your own validation attribute by creating attribute that inherits ValidationAttribute class and override one of its methods to provide your own custom validation logic.

    Of course the business validation usually might require more advanced validation that can not be solved with validation attributes. For those cases you need the reqular business logic code to make sure that operation can be done.

    • Marked as answer by NorthValley Tuesday, September 10, 2013 6:35 AM
    Tuesday, September 10, 2013 5:11 AM