Quantcast
Channel: CSLA .NET
Viewing all articles
Browse latest Browse all 764

I got a problem using a Validation Attribute in a non CSLA property

$
0
0

I got a problem using a Validation Attribute in a non CSLA property. The test code follow below.

[Serializable]

public class Foo : BusinessBase<Foo>

{

       public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);

       public string Name

       {

             get { return GetProperty(NameProperty); }

             set { SetProperty(NameProperty, value); }

       }

 

       public static readonly PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id);

       public int Id

       {

             get { return GetProperty(IdProperty); }

             set { SetProperty(IdProperty, value); }

       }

 

       [Required]

       public int Code { get; set; }

}

In the method AddDataAnnotationsFromType of the class BusinessRules the linq clause First used to seek the IPropertyInfom returns a exception only when no results are found. Follows the  code that's generating the error.

privatevoid AddDataAnnotationsFromType(Type metadataType)

    {

      var attList = metadataType.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.ValidationAttribute),true);

      foreach (var att in attList)

        AddRule(new CommonRules.DataAnnotation(null, (System.ComponentModel.DataAnnotations.ValidationAttribute)att));

 

      // attributes on properties

      var propList = metadataType.GetProperties();

      foreach (var prop in propList)

      {

        attList = prop.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.ValidationAttribute),true);

        foreach (var att in attList)

        {

          var target = (IManageProperties)_target;

          var pi = target.GetManagedProperties().First(c => c.Name == prop.Name);

          AddRule(new CommonRules.DataAnnotation(pi, (System.ComponentModel.DataAnnotations.ValidationAttribute)att));

        }

      }

    }

A sugestion to avoid the exception is use the clause FirstOrDefault, but should be cosidered the real necessity for create a rule for a non CSLA property.

 


Viewing all articles
Browse latest Browse all 764

Trending Articles