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; }
}
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));
}
}
}
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.
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.
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