流利验证班 [英] Fluent Validation Class

查看:59
本文介绍了流利验证班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我一直在尝试为特定的类创建一个验证器.

I have been trying to create a validator for a specific class.

我有以下抽象类,该抽象类来自服务层:

I have the following abstract class which a Reply from a Service Layer:

  public abstract class Reply {
    public Exception Exception { get; set; }
  } // Reply

我的服务层命令返回各种答复.一个例子:

My service layer commands return various replies. An example:

public class VerifyUserReply : Reply {
  public enum ReplyStatus { UserNotFound, UserLocked, UserDisabled }
}

每个回复具有不同的ReplyStatus值.我收到以下方法的回复:

Each reply has different ReplyStatus values. I receive the reply in a method:

public virtual ValidationResult Verify(String token) { VerifyUserReply reply = _service.VerifyUser(token); // Validation STARTS Here if (reply.Exception != null) return new ValidationResult("...") switch (reply.Status) { case VerifyUserReply.ReplyStatus.UserDisabled: return new ValidationResult("...") case VerifyUserReply.ReplyStatus.UserNotFound: return new ValidationResult("...") case VerifyUserReply.ReplyStatus.UserLocked: return new ValidationResult("...") // AND SO ON ... } // Validation ENDS Here

返回新的ValidationResult(); }

return new ValidationResult(); }

因为我多次重用此代码,所以我试图创建一些更优雅"的代码.然后这个.

Because I reuse this code a lot I was trying to create something more "elegant" then this.

我尝试了许多选择,但从未完全正确……我的想法如下:

I tried many options but never get quite right ... My idea would be something as follows:

ValidationResult result = 
  Validate(reply)
    .OnException(new ValidationResult(""))
    .For(reply.Info)
      .(VerifyUserReply.UserNotFound, new ValidationResult(""))
      .(VerifyUserReply.UserDisabled, new ValidationResult(""))

// I am returning the result on a different line because it seems impossible to integrate it the previous code since if all conditions are false then nothing would be returned

if (result != null)
  return result;

我一直在尝试创建它,但是在OnException部分之后,以及如何使它变得流畅时,我遇到了问题.

I have been trying to create this but I am getting problems after the OnException part and how to make this fluent.

我该怎么做?

谢谢

Miguel

推荐答案

我认为您要执行的操作的方法是在Validate对象中定义所需的方法.例如,代码的Validate(reply)部分将设置一个私有变量.然后,Validate(reply).OnException(new ValidationResult("))将简单地 创建新的验证结果.班级看起来像这样……

I think the way to do what you ask for is to define the methods you want in the Validate object. For example the Validate(reply) portion of the code would set a private variable.  Then the Validate(reply).OnException(new ValidationResult("") would simply create a new validation result.  The class would look like this...

public class Validate{

  private static dynamic _reply;
  public static Validate(dynamic reply){
    _reply = reply;
}
public static ValidationResult _TheResult;
public static void OnException(ValidationResult TheResult)
   _TheResult = The Result
}
private static Info _ReplyInfo;
public static void For(Info ReplyInfo){
  _ReplyInfo = ReplyInfo;
}


不确定下一部分...


Not sure about next part...


这篇关于流利验证班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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