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

问题描述

你好

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

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

推荐答案

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...


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

09-22 22:57