I have the following class that checks for errors:
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;
}
and the following extension class to merge my IDictionary and the 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);
}
}
When I call the validate and the then try to merge it doesn’t look clean:
var errors = _accountService.ValidateAccount(vm.Account);
if (errors.Count > 0)
this.ModelState.Merge(errors)
Is there some way that I could maybe have a different constructor to merge and then just feed in the output
of:
_accountService.ValidateAccount(vm.Account);
I am trying to think of how I could do this but can’t think of a simple way
Since your
Mergemethod is basically doing nothing in the case of no errors you could just doThe check for error count is unnecessary.