using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Microsoft.Practices.EnterpriseLibrary.Validation;using ValidationResult = System.ComponentModel.DataAnnotations.ValidationResult;namespace Blackice.Suite.Domains.Validations{ ////// 企业库验证操作 /// public class Validation : IValidation { ////// 验证 /// /// 验证目标 public ValidationResultCollection Validate(object target) { var validator = ValidationFactory.CreateValidator(target.GetType()); var results = validator.Validate(target); return GetResult(results); } ////// 获取验证结果 /// private ValidationResultCollection GetResult(IEnumerableresults) { var result = new ValidationResultCollection(); foreach (var each in results) result.Add(new ValidationResult(each.Message)); return result; } } /// /// 验证操作 /// public interface IValidation { ////// 验证 /// /// 验证目标 ValidationResultCollection Validate(object target); } ////// 验证结果集合 /// public class ValidationResultCollection : IEnumerable{ /// /// 初始化验证结果集合 /// public ValidationResultCollection() { _results = new List(); } /// /// 验证结果 /// private readonly List_results; /// /// 是否有效 /// public bool IsValid { get { return _results.Count == 0; } } ////// 验证结果个数 /// public int Count { get { return _results.Count; } } ////// 添加验证结果 /// /// 验证结果 public void Add(ValidationResult result) { if (result == null) return; _results.Add(result); } ////// 添加验证结果集合 /// /// 验证结果集合 public void AddResults(IEnumerableresults) { if (results == null) return; foreach (var result in results) Add(result); } /// /// 获取迭代器 /// IEnumeratorIEnumerable .GetEnumerator() { return _results.GetEnumerator(); } /// /// 获取迭代器 /// IEnumerator IEnumerable.GetEnumerator() { return _results.GetEnumerator(); } public IEnumerator GetEnumerator() { return _results.GetEnumerator(); } }}