定义退货状态代码

tal*_*tal 1 c#

这是一个非常普遍且可能很常见的问题,但我在网上找不到答案.

我正在寻找一种合理的方法来为一个应用程序创建一个状态代码,该代码可能在某些方面失败但在其他方面仍然成功.

可以说我有一个应用程序运行一些算法.在我们得到结果后,该应用程序应该做3件事:

  1. 通过电子邮件发送结果
  2. 将结果保存到Db
  3. 将结果发送到另一个集成的外国应用程序

我需要创建一个状态代码,其中包含每个独立步骤的状态,并且可以指示单个或多个故障.

它有什么常见的做法吗?

San*_*ser 6

你应该使用Flag Enum:

[Flags]
public enum Options 
{
  None    = 0,
  Option1 = 1,
  Option2 = 2,
  Option3 = 4,
  Option4 = 8
}
Run Code Online (Sandbox Code Playgroud)

https://msdn.microsoft.com/library/ms229062(v=vs.100).aspx

在这里你可以找到一些很好的例子与.HasFlag https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx

在你的情况下,它可能是这样的(它可以扩展的程序,它真的取决于你的设计和要求):

[Flags]
public enum ReturnStatus
{
  NoErrors = 0,
  DBError = 1,
  ThirdPartyError = 2,
  EmailError = 4,
  EmailSend = 8 
  //This could also be an option, so i just added it here as example, but i'm a bit confused if this is used as a return status or the current state of a task
  //Example: when database failed, and the algorithm doesn't event attempts to send a mail, and when you want to rerun a task it could be usefull
}

ReturnStatus ret = ReturnStatus.DBError | ReturnStatus.EmailError;

if( ret.HasFlag(ReturnStatus.EmailError) ) {
  //Email failed to send
}
if( ret.HasFlag(ReturnStatus.DBError) ) {
  //Db save failed
}
Run Code Online (Sandbox Code Playgroud)