locked
How do I properly use .NET Data Annotations for data validation in WPF with a WCF backend? RRS feed

  • Question

  • Question title pretty much explains what I am trying to do.

    Simplification of my code for example purpose:

    Bits of an example WCF Service:

            pulic class Restaurant
            {
                 //RegEx to only allow alpha characters with a max length of 40
                 //Pardon if my regex is slightly off
                 [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")]
                 public string Name { get; set; }
            }


            public class RestaurantService
            {
                 List<Restaurant> restaurants = new List<Restaurant>();
    
                 public AddRestaurant(string name)
                 {
                      Restaurant restaurant = new Restaurant();
                      restaurant.Name = name;
                      restaurants.Add(restaurant);
                 }
            }



    Bits of example XAML:

            <TextBox name="txt1" Text="{Binding Restaurant.Name, ValidatesOnDataErrors=True}"/>

    How do I make my view do something when my data annotation is violated?

    All of the examples I can find here and elsewhere are either not exactly what I am looking for or that have to do with ASP.NET.  I don't know enough about WPF and Data Annotations and I am very green with WCF.

    I have tried implementing the IDataErrorInfo interface, but I can't seem to get anything to fire in it.  I implemented this in my Restaurant class in the WCF service.

    		public string this[string columnName]
    		{
    			get 
    			{
    				if (columnName == "Name")
    				{
    					return ValidateProperty(this.Name, columnName);
    				}
    				return null;
    			}
    		}



    		protected string ValidateProperty(object value, string propertyName)
    		{
    			var info = this.GetType().GetProperty(propertyName);
    			IEnumerable<string> errorInfos =
    				  (from va in info.GetCustomAttributes(true).OfType<ValidationAttribute>()
    				   where !va.IsValid(value)
    				   select va.FormatErrorMessage(string.Empty)).ToList();
    
    			if (errorInfos.Count() > 0)
    			{
    				return errorInfos.FirstOrDefault<string>();
    			}
    			return null;
    		}

    Friday, April 6, 2012 4:15 AM

Answers

  • Hello, currently there's no built-in solution to pass data annotations between client and service. After all, data annotation is considered as either client detail or service detail, and according to SOA principle, should not be shared between client and service. However, if you want to use traditional N-tire architecture instead of SOA, this may be a common practice. If you use Silverlight and WCF RIA Services, there's built-in support to share data annotation between client and service. So if Silverlight is an option, you can move your client to Silverlight. If you're building business applications, Silverlight offers almost everything WPF offers. If you have to use WPF, then you must perform validation separately. On the client, you use WPF features like IDataErrorInfo to validate user input. On the service side, you write some custom validation logic, or delegate the validation to the data access layer (such as Entity Framework).

    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.

    Sunday, April 8, 2012 12:33 PM