2011-12-13 3 views
0

나는 다음과 같은 오류를 확인 클래스가 : 병합병합 행과 함께 IDictionary의 행 수를 결합하는 방법은 무엇입니까?

public bool ValidateAccountDeletion(Account account) 
    { 
     var _errors = new Dictionary<string, string>(); 
     var accounts = _accountRepository.GetPk(account.PartitionKey); 
     if (_productRepository.GetPk("0000" + account.RowKey).Count() != 0) 
      _errors.Add("", "Account contains products"); 
     return _errors; 
    } 

다음과 같은 확장 클래스를 내 IDictionary과 ModelStateDictionary

public static class ModelExtensions { 
    public static bool HasValue(this string value) { 
     return !string.IsNullOrEmpty(value) && value.Trim().Length > 0; 
    } 

    public static void Merge(this ModelStateDictionary modelState, IDictionary<string, string> dictionary) { 
     Guard.AgainstNullParameter(modelState, "modelState"); 
     Guard.AgainstNullParameter(dictionary, "dictionary"); 

     foreach(var item in dictionary) { 
      modelState.AddModelError(item.Key, item.Value); 
     } 
    } 

나는 확인하고 다음을 병합하려고 전화 깨끗하게 보이지 않습니다.

 var errors = _accountService.ValidateAccount(vm.Account); 
     if (errors.Count > 0) 
      this.ModelState.Merge(errors) 

어쩌면 다른 생성자를 병합 할 수있는 방법이 있습니까? 단지 출력에 먹이의 :

_accountService.ValidateAccount(vm.Account); 

나는이 작업을 수행 할 수있는 방법을 생각하려고하지만, 간단한 방법을 생각할 수 없다

답변

4

당신의 Merge 방법은 기본적으로 경우에 아무것도하지 않기 때문에 당신이 할 수있는 오류가 전혀 없음

ModelState.Merge(_accountService.ValidateAccount(vm.Account)); 

오류 개수를 확인하지 않아도됩니다.

+0

예. 기본적으로,'merge'는'errors.Count == 0' 사례 자체를 어떻게 처리할지 결정합니다. – ladenedge

관련 문제