在Winforms脱机客户端中重新使用ServiceStack验证 [英] re-using ServiceStack validation in Winforms offline client

查看:60
本文介绍了在Winforms脱机客户端中重新使用ServiceStack验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个正常工作的网站,使用ServiceStack作为后端,相当于一个复杂的数据输入表.

We have a working website using ServiceStack as the back end that amounts to a complex data-entry form.

我的用户已请求表单的离线编辑器".要使用脱机程序,用户将必须连接到ServiceStack服务,创建表单的空实例,然后使用ServiceStack的JSON序列化程序将服务中的POCO保存到磁盘.用户可以从那里注销该服务并编辑POCO.完成后,他们将重新连接到服务,并发布/放置已编辑的POCO对象.

My users have requested an "offline editor" for the forms. To use the offline program, the user will have to connect to the ServiceStack service, create empty instances of the forms, and then I will save the POCOs from the service to disk using ServiceStack's JSON serializer. From there the user can log off the service and edit the POCOs. When they're done, they reconnect to the service, and post/put the edited POCO object.

这一切都很好.我的问题涉及验证.验证逻辑内置于我的Service.Interface库中,该库不能脱机使用. Winforms程序仅引用POCO库和ServiceStack公共"库,它们看起来不包含ServiceStack.Validation命名空间.

This all works great. My question involves validation. The validation logic is built into my Service.Interface library, which isn't available offline. The winforms program references only the POCO library and the ServiceStack "common" libraries, which do not look like they include the ServiceStack.Validation namespace.

是否可以重新安排项目,以便服务和Winforms客户端都可以针对POCO运行Validation,以便他们可以在脱机时进行数据验证?

Is there a way I can rearrange my project so that both the service and the Winforms client can run Validation against the POCOs, so that they can have data validation while offline?

更新: 我想越来越近了-我将所有Validation类都移到了自己的项目中.现在,从Winforms项目中,我可以为POCO类手动设置验证器,如下所示:

UPDATE: getting closer, I think - I moved all of the Validation classes into their own project. From my Winforms project, I can now manually set up a validator for a POCO class like this:

 ServiceStack.FluentValidation.IValidator<SomePOCO> IValidator;
 IValidator = new Tonto.Svc.Validation.SomePOCOValidator();

 ServiceStack.FluentValidation.Results.ValidationResult vr = 
    IValidator.Validate(_rpt);

我可以看到验证器构造函数已设置并且规则已初始化,但是.Validate方法似乎没有任何作用. (对象恢复为有效,并且进入自定义验证器代码的断点永远无法到达那里.)

I can see the validator constructor being set up and the rules being initialized, but the .Validate method doesn't seem to do anything. (object comes back as valid, and breakpoints into custom validator code never get there).

更新#2

我发现验证程序代码未从Winforms运行,因为我的验证程序都只指定了一个服务堆栈ApplyTo Put/Post(请参见下面的示例代码).但是,当我删除整个Ruleset子句时,验证就会在我的GETs服务中进行-这是我永远都不需要的.

I discovered my validator code wasn't running from Winforms because my validators all specify a servicestack ApplyTo Put/Post only (see sample code below). When I remove the entire Ruleset clause, though, then validation happens in my service on GETs - something I never want.

谁能想到一种配置验证器规则的方法,使其仅在从ServiceStack调用时才针对POST/PUT运行,但也始终在不在ServiceStack中时始终运行?太近了!

Can anyone think of a way to configure the validator rules to run for POST/PUT only when called from ServiceStack, but to also always run when NOT in servicestack? So close!

public class SomePOCOValidator : AbstractValidator<SomePOCO>    
    {
        public SomePOCO()
        {
            RuleSet(ApplyTo.Put | ApplyTo.Post, () =>
            {
               (rules)
            });
        }
    }

推荐答案

我能够制定出一个解决方案,使我可以在ServiceStack客户端和脱机客户端上使用ServiceStack验证库.这是详细信息.

I was able to work out a solution that allowed me to use ServiceStack validation libraries on both a ServiceStack client and an offline client. Here are the details.

  1. 将所有AbstractValidator移至他们自己的项目:Proj.Svc.Validation.

  1. Move all AbstractValidators to their own project: Proj.Svc.Validation.

摆脱AbstractValidators中的所有规则集.

get rid of all RuleSets in your AbstractValidators.

从Proj.Svc.Interface和Proj.OfflineWinformsClient项目中引用Proj.Svc.Validation.

Reference Proj.Svc.Validation from Proj.Svc.Interface and Proj.OfflineWinformsClient projects.

关闭服务中的ValidationFeature()插件.所有验证都必须手动完成.这意味着您的服务类别中没有iOC注入的验证器.

Turn OFF the ValidationFeature() plugin in your service. All validation will have to be done manually. This means no iOC injected validators in your service classes.

是时候从您的服务或脱机客户端进行验证了,手动声明验证器并像这样使用它.

When it's time to validate, either from your service or the offline client, manually declare the validator and use it like this.

IValidator验证程序=新 Tonto.Svc.Validation.SomePOCOValidator(); ServiceStack.FluentValidation.Results.ValidationResult vr = validateator.Validate(poco);

IValidator validator = new Tonto.Svc.Validation.SomePOCOValidator(); ServiceStack.FluentValidation.Results.ValidationResult vr = validator.Validate(poco);

如果(!vr.IsValid) (引发异常或以某种方式通知用户);

if (!vr.IsValid) (throw exception or notify user somehow);

这篇关于在Winforms脱机客户端中重新使用ServiceStack验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆