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

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

Old forum URL: forums.lhotka.net/forums/t/12000.aspx


Dakianth posted on Monday, June 03, 2013

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.

private void 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.

 

JonnyBee replied on Monday, June 03, 2013

Hi,

Why not use a CSLA property?  

Considering all - CSLA is made to work on a number of platforms - and using DataAnnotation rules on a CSLA object without adding it to the CSLA Rules is not recommended on any platform. This will cause the broken rules collection to not work as expected - the IsSavable/IsValid will not work as expected and so on. 

You can of course create your own version of AddDataAnnotationFromType but I would not recommend to do so. 

Dakianth replied on Monday, June 03, 2013

I will consider the use of a csla property, but analysis the feasibility of do a error treatment for this case, the standard message of the .Net for null references isn't intuitive for this case.

JonnyBee replied on Monday, June 03, 2013

Hi,

Yes, the error message might be better but you should be very careful when/if mixing 2 different rule systems. 

Copyright (c) Marimer LLC