2016-06-30 4 views
0

FluentValidation 오류 목록을 허용하는이 클래스를 사전에 배치합니다. 목록은 하나의 특정 열거 형 (예. CustomerError)의 값을 포함 할 때열거 형별로 다른 열거 형 목록 그룹

public class ErrorList<T> : ISerializable where T : struct, IConvertible 
{ 
    public IReadOnlyDictionary<T, string> Errors { get; private set; } 

    public ErrorList(IList<ValidationFailure> errors) 
    { 
     Errors = (from e in errors 
        select new 
        { 
         e.PropertyName, 
         e.CustomState 
        }).ToDictionary(t => (T)t.CustomState, t => t.PropertyName); 
    } 
} 

생성자는 상기 작동합니다. (. 예를 들어 CustomerError, CustomerBankAccountError 등)

은 이제 전달 된 오류가 다른 열거 유형을 포함 할 수 있도록 위를 확장해야, 그래서 나는 새 속성 추가 거라고 :

public IReadOnlyDictionary<Type, IReadOnlyDictionary<object, string>> ExtraErrors { get; private set; } 

Linq 사용을 은 "오류"사전에

  1. 필터 출력 및 유형 T의 열거 값을 배치 : 어떻게 할 수 있습니까?
  2. "ExtraErrors"내에서 나머지 열거 형 값을 별도의 사전으로 필터링하고 그룹화 하시겠습니까? 당신은 LINQ를 사용하려면
+0

,'ValidationFailure.CustomState' 다른 박스 열거를 포함 할 수있는'object' 속성입니다 값을 열거 형으로 그룹화하고 싶습니까? 또한,이 클래스는 일반적인 'ErrorList '이 아닙니다. – Groo

+0

@Groo 부분적으로 일반화 될 예정입니다! –

+0

아마 조금은 정교 할 수 있습니다. 이 수업에서 일반적인 것은 무엇입니까? – Groo

답변

0

, 당신은 단순히 두 개의 컬렉션으로 요소를 필터링 할 where을 사용할 수 있습니다 : 그래서

public ErrorList(IEnumerable<ValidationFailure> errors) 
{ 
    // e.CustomState is of type T ? 
    Errors = errors 
     .Where(e => (e.CustomState is T)) 
     .ToDictionary(e => (T)e.CustomState, e => e.PropertyName); 

    // e.CustomState is not of type T ? 
    ExtraErrors = errors 
     .Where(e => !(e.CustomState is T)) 
     .GroupBy(e => e.CustomState.GetType()) 
     .ToDictionary(
      g => g.Key, 
      g => (IReadOnlyDictionary<object, string>)g.ToDictionary(
         e => e.CustomState, 
         e => e.PropertyName)); 
}