使用Catel验证和相当复杂的对象

时间:2015-05-29 12:26:39

标签: catel

我试图以更智能的方式实施验证,因为我可以...... 从一开始我就是一个网格视图,其中包含从DB加载的一些数据(该对象不会从ModelBase继承)。当用户右键单击项目结束时,将显示编辑弹出窗口。

加载弹出窗口的代码

public override async Task UpdateDeal(IDeal deal)
    {
        var item = (DealForward)deal.Clone();
        var modello = viewModelFactory.CreateViewModel<DealForwardUpdateViewModel>(item);



        var dependencyResolver = this.GetDependencyResolver();
        var uiVisualizerService = dependencyResolver.Resolve<IUIVisualizerService>();

        await uiVisualizerService.ShowDialogAsync(modello);
    }

这是DealForwardUpdateViewModel

public class DealForwardUpdateViewModel : DealUpdateViewModel
{
    #region Model
    [Model]
    public DealForwardUpdateModel Model
    {
        get { return GetValue<DealForwardUpdateModel>(DealForwardUpdateModelProperty); }
        set { SetValue(DealForwardUpdateModelProperty, value); }
    }
    public static readonly PropertyData DealForwardUpdateModelProperty = RegisterProperty("Model", typeof(DealForwardUpdateModel), null);
    #endregion

    #region Properties
    [ViewModelToModel("Model")]
    public DealForward Deal
    {
        get { return GetValue<DealForward>(DealProperty); }
        set { SetValue(DealProperty, value); }

    }

    public static readonly PropertyData DealProperty = RegisterProperty("Deal", typeof(DealForward), null);

    #endregion

    #region Ctor
    public DealForwardUpdateViewModel(DealForward forward, IDynamicContainer container)
    {
        this.Model = new DealForwardUpdateModel(forward);

        this.container = container;
    }

我不确定在DealForwardUpdateModel中放什么,我应该只使用DealForward还是我必须在构造函数中将其分解为自己的字段?

public class DealForwardUpdateModel : ModelBase
{
    public double? Cambio { get; set; }

    public DealForwardUpdateModel(DealForward deal)
    {
        this.Deal = deal;
        this.Cambio = deal.Cambio;
    }
    public DealForward Deal
    {
        get { return GetValue<DealForward>(DealProperty); }
        set { SetValue(DealProperty, value); }
    }
    public static readonly PropertyData DealProperty = RegisterProperty("Deal", typeof(DealForward), null);
}

[ValidatorDescription("DealForwardUpdateModel", Catel.Data.ValidationResultType.Error, Catel.ValidationType.Field)]
public class DealForwardUpdateModelValidator : AbstractValidator<DealForwardUpdateModel>
{
    public DealForwardUpdateModelValidator()
    {
        RuleFor(x => x.Deal.Split).SetValidator(new SplitValidator());

        RuleFor(x => x.Cambio).GreaterThan(0).WithMessage(DealResources.STR_VALIDATION_CAMBIO);

        RuleFor(x => x.Deal.QtaDiv1).LessThan(0).When(x => x.Deal.Segno == 1).WithMessage(DealResources.STR_VALIDATION_NEGATIVE_QTA1);
        RuleFor(x => x.Deal.QtaDiv1).GreaterThanOrEqualTo(0).When(x => x.Deal.Segno == 0).WithMessage(DealResources.STR_VALIDATION_POSITIVE_QTA1);

        RuleFor(x => x.Deal.QtaDiv2).LessThan(0).When(x => x.Deal.QtaDiv1 > 0).WithMessage(DealResources.STR_VALIDATION_OPPOSITE_QTA2);
        RuleFor(x => x.Deal.QtaDiv2).GreaterThan(0).When(x => x.Deal.QtaDiv1 < 0).WithMessage(DealResources.STR_VALIDATION_OPPOSITE_QTA2);
    }
}

第二个选项似乎有效,但在将我的DealForward粉碎成一个字段列表之前我会问是否有更好的方法。

请考虑我有3/4种不同的交易,并且它们具有可共享的可编辑/可验证部分

由于

1 个答案:

答案 0 :(得分:1)

您可以通过多种方式实施验证:

  1. Validar(Fody插件),就像你在第二个例子中所做的那样
  2. FluentValidation(Catel有此扩展)
  3. 通过覆盖ValidateFields和ValidateBusinessRules在模型/视图模型中提供的简单验证
  4. 1和2给出了一个很好的SoC(关注点分离)。