1

I have a class Customers. I want to put some validations on it. e.g. CustGuidId is not Guid.Empty, CustName is NOT NULL (Required).

public class Customer
{
    public int CustId;
    public string CustName;
    public Guid CustGuid;
    public Guid[] OrderGuids;
}

I have such collection of customers. So I have ended up adding code like this, which makes it look ugly.

public class BatchError
{
    public int Index;
    public string ErrorCode;
    public string ErrorMessage;
}


public void GenerateValidationErrors(List<Customer> customers, out List<BatchError> batchErrors)
    {
        int rowNum = 0;
        batchErrors = new List<BatchError>(customers.Count);

        foreach (var customer in customers)
        {
            rowNum ++;
            Guid customerGuidParsed;
            if(!Guid.TryParse(customer.CustGuid.ToString(), out customerGuidParsed))
            {
                batchErrors.Add(new BatchError { Index = rowNum, ErrorCode = "CustomerGuidcannotBeNull", ErrorMessage = "Customer guid cannot be null." });
            }
            if (string.IsNullOrEmpty(customer.CustName))
            {
                batchErrors.Add(new BatchError { Index = rowNum, ErrorCode = "CustomerNamecannotBeEmpty", ErrorMessage = "Customer Name cannot be empty." });
            }
        }
    }

Can we write separate validator classes, like GuidValidator, StringValidator. and Create array of delegates & chain their invokes ?

(Customer c) => new GuidValidator(c.CustGuid.toString()),
(Customer c) => new StringValidator(c.CustName.toString())

But what design pattern would be best suitable for this scenario?

Is there any other way to add validations in WCF?

1 Answer 1

1

There are many ways to do the validation. I prefer to validate DataContract itself before any action. It can also be done in many like :

  • DatamemberAttribute has many properties. One of them is IsRequired,it controls the minOccurs attribute for the schema element. The default value is false. You can use it like:

    [DataContract(Name ="Place", Namespace ="")] public class DataContractExample { [DataMember(IsRequired=true)] public string DataMemberExample; }

For more information refer: DataMemberAttribute Class on MSDN.

  • Easiest way is to validate property like:

    [DataContract] public class Customer { [DataMember] public string CustName { get { return this._custName; } set { if(string.IsNullOrEmpty(value)) throw new MyValidationException(); else this._custName=value; } } }

  • Another way can be to use Microsoft Enterprise Library. In order to enable validation of the properties of a request message, you only need to add a [ValidationBehavior] attribute to your service interface, just next (or before) the [ServiceContract], and a [FaultContract(typeof(ValidationFault))] on the method declaration. The ValidationBehaviorAttribute and ValidationFault classes are defined in the Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF assembly and are part of the Validation Application Block of the Enterprise Library 4.1, more specifically, of the WCF integration module. See full implementation in detail at: http://weblogs.asp.net/ricardoperes/validation-of-wcf-requests-with-the-enterprise-library-validation-block

  • Finally one more solution cane be to use WCF Data Annotations from http://wcfdataannotations.codeplex.com/. Using this you can use validations like:

    [DataMember]
    [Required]
    [StringLength(500, MinimumLength = 5)]
    public string Description{ get; set; }
    

Choose which ever suite your requirements. Cheers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.