基本的に問題は、BL を実行する前に、データベース (EF) から取得した値に対して検証を実行したいということです。現在、ViewModel のプロパティの属性で INotifyDataErrorInfo を使用しています。カスタムバリデーター (CustomValidation 属性) を使用してこの検証を実行しようとしました。
private string unit;
[Required(AllowEmptyStrings = false, ErrorMessage = Constants.Error6)]
[RegularExpression(Constants.AlphabeticRegEx, ErrorMessage = Constants.Error10)]
[CustomValidation(typeof(ProductSelectionViewModel),"IsInRegisteredUnits")]
public string Unit
{
get { return unit; }
set
{
if (value == unit)
return;
unit = value;
RaisePropertyChanged(vm => vm.Unit);
UpdateUnitPrice(selectedProduct, unit);
}
}
ただし、この検証の実行を担当するメソッドは静的でなければならないため、このシナリオではリポジトリが静的ではないため、リポジトリにアクセスできません。
public static ValidationResult IsInRegisteredUnits(object obj, ValidationContext context)
{
var productSelectionViewModel = (ProductSelectionViewModel)context.ObjectInstance;
if (!unitService.GetAllUnitsAbbreviation().Any(x=>x.Equals(productSelectionViewModel.Unit, StringComparison.CurrentCultureIgnoreCase)))
return new ValidationResult("La unidad ingresada no es válida", new List<string> { "Unit" });
return ValidationResult.Success;
}
これを解決するにはどうすればよいですか (unitService.GetAllUnitsAbbreviation() は、リポジトリを使用しているため、静的メソッドにすることはできません)、この種の検証を摩耗した場所 (間違った設計) で実行している可能性があります。