My favorites | Sign in
Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using uNhAddIns.Adapters;
using System.ComponentModel.DataAnnotations;

namespace uNhAddIns.DataAnnotations
{
public class EntityValidator : IEntityValidator
{

#region IEntityValidator Members

///<summary>
/// Returns true if the entity is valid.
///</summary>
///<param name="entityInstance"></param>
///<returns></returns>
public bool IsValid(object entityInstance)
{
var validators = from property in entityInstance.GetType().GetProperties()
from attribute in property.GetCustomAttributes(typeof (ValidationAttribute), true)
.OfType<ValidationAttribute>()
select new {
Validator = attribute,
ValueToValidate = property.GetValue(entityInstance, null)
};

return validators.Any(validation => validation.Validator.IsValid(validation.ValueToValidate));
}

///<summary>
/// Validates an entity and returns the information about invalid values.
/// </summary>
///<param name="entityInstance"></param>
///<returns></returns>
public IList<IInvalidValueInfo> Validate(object entityInstance)
{
Type type = entityInstance.GetType();

var validators = from property in type.GetProperties()
from invalidMessage in GetInvalidValues(entityInstance, property, property.GetValue(entityInstance, null))
select invalidMessage;

return validators.ToList();
}

///<summary>
/// Validates a property of the entity and returns the information about invalid values.
///</summary>
///<param name="entityInstance"></param>
///<param name="property"></param>
///<typeparam name="T"></typeparam>
///<typeparam name="TP"></typeparam>
///<returns></returns>
public IList<IInvalidValueInfo> Validate<T, TP>(T entityInstance, Expression<Func<T, TP>> property) where T : class
{
MemberInfo propertyInfo = GetMemberInfo(property);
var value = property.Compile()(entityInstance);

return GetInvalidValues(entityInstance, propertyInfo, value);
}

private static IList<IInvalidValueInfo> GetInvalidValues<T, TP>(T entityInstance, MemberInfo propertyInfo, TP value)
{
var validators = propertyInfo.GetCustomAttributes(typeof (ValidationAttribute), true)
.OfType<ValidationAttribute>();

var result = from v in validators
where !v.IsValid(value)
select new InvalidValueInfo(entityInstance.GetType(), propertyInfo.Name, v);

return result.OfType<IInvalidValueInfo>().ToList();
}

private static MemberInfo GetMemberInfo(LambdaExpression lambda)
{
return ((MemberExpression)lambda.Body).Member;
}

///<summary>
/// Validates a property of the entity and returns the information about invalid values.
///</summary>
///<param name="entityInstance"></param>
///<param name="property"></param>
///<typeparam name="T"></typeparam>
///<typeparam name="TP"></typeparam>
///<returns></returns>
public IList<IInvalidValueInfo> Validate(object entityInstance, string property)
{
PropertyInfo propertyInfo = entityInstance.GetType().GetProperty(property);
var value = propertyInfo.GetValue(entityInstance, null);
return GetInvalidValues(entityInstance, propertyInfo, value);
}

#endregion
}
}
Show details Hide details

Change log

r780 by jfromaniello on Oct 06, 2009   Diff
uNhAddIns.DataAnnotations first commit.
Abstraction block for
System.ComponentModel.DataAnnotations.
Go to: 
Sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 3473 bytes, 100 lines
Hosted by Google Code