mm8 さん、ご回答ありがとうございます。
このソリューションでは、外部検証が必要なクラスごとに 1 つのラッパーを作成する必要があります。作業が追加され、リファクタリング中にクラスとラッパーを編集する必要があります。
このソリューションについてどう思いますか :
- 車両のリストをバインドリストに入れました
- 私のサービスは、このリストの ListChanged イベントにサブスクライブします
- 私のサービスはINotifyDataErrorInfoを実装しています
- このリストの各変更に対して検証が実行されます
- エラーが発生した場合、ErrorsChanged イベントが発生します。ビュー モデルはこのイベントにサブスクライブし、エラー データを取得します。
- ビュー モデルはこのイベントをサブスクライブし、エラー データを取得します。
例えば :
私のサービスの実装:
public class VehicleServices : INotifyDataErrorInfo
{
private BindingList<Vehicle> _bindingListCar
public BindingList<Vehicle> BindingListCar
{
get return _bindingListCar;
}
private readonly Dictionary<string, ICollection<string>>
_validationErrors = new Dictionary<string, ICollection<string>>();
//INotifyDataErrorInfo implementation
public IEnumerable GetErrors(string propertyName)
public bool HasErrors
private void RaiseErrorsChanged(string propertyName)
public VehicleServices()
{
_bindingListCar = GetVehicles();
_bindingListCar.ListChanged += BindingListVehicleChanged;
}
private void BindingListVehicleChanged(object sender, ListChangedEventArgs e)
{
//Only modification is managed
if (e.ListChangedType != ListChangedType.ItemChanged) return;
switch(e.PropertyDescriptor.Name)
//Validate each property
//if there is ErrorsChanged is raised
}
}
そして私のViewModel
public class CarVm : BindableBase
{
private ICollection<string> _errors;
public ICollection<string> Error
{
get
{
return _errors;
}
set
{
SetProperty(ref _errors, value);
}
}
private VehicleServices _carServices;
public BindingList<Vehicle> BindingListCar { get; set; }
public CarVm(VehicleServices carServices)
{
_carServices = carServices;
BindingListCar = new BindingList<Vehicle>(_carServices.BindingListCar);
_carServices.ErrorsChanged += _carServices_ErrorsChanged;
}
private void _carServices_ErrorsChanged(object sender, DataErrorsChangedEventArgs e)
{
Error = _carServices.ValidationErrors[e.PropertyName];
}
}
これは良い習慣だと思いますか?