root / trunk / hammock / src / net35 / Hammock / Attributes / Validation / SpecificationAttribute.cs @ 0eea575a
History | View | Annotate | Download (1.2 kB)
1 |
using System; |
---|---|
2 |
using Hammock.Extensions; |
3 |
using Hammock.Specifications; |
4 |
using Hammock.Validation; |
5 |
|
6 |
namespace Hammock.Attributes.Validation |
7 |
{ |
8 |
#if !SILVERLIGHT |
9 |
[Serializable] |
10 |
#endif |
11 |
public class SpecificationAttribute : ValidationAttribute |
12 |
{ |
13 |
public SpecificationAttribute(Type specificationType) |
14 |
{ |
15 |
if (!specificationType.Implements(typeof (ISpecification))) |
16 |
{ |
17 |
throw new ValidationException("You must provide a valid specification type."); |
18 |
} |
19 |
|
20 |
SpecificationType = specificationType as ISpecification; |
21 |
} |
22 |
|
23 |
public ISpecification SpecificationType { get; private set; } |
24 |
|
25 |
public override string TransformValue(System.Reflection.PropertyInfo property, object value) |
26 |
{ |
27 |
if(SpecificationType != null && !value.Satisfies(SpecificationType)) |
28 |
{ |
29 |
var message = |
30 |
"The value for '{0}' does not satisfy {1}." |
31 |
.FormatWith(property.Name, SpecificationType); |
32 |
|
33 |
throw new ValidationException(message); |
34 |
} |
35 |
|
36 |
return base.TransformValue(property, value); |
37 |
} |
38 |
} |
39 |
} |