0
Follow
0
View

Result class to pass between project layers

dyl1207 注册会员
2023-01-25 16:35

For the "avoid wrong usage" part, you could do something like this (I personally wouldn't, but hey... )

public class FooBarResult
{
     public bool IsSuccessful {get; set;}

     // just doing one to demonstrate
     private string _payload;
     public string Payload
     {
         // InvalidOperationException is the ...
         // "The exception that is thrown when a method call is invalid 
         //  for the object's current state."
         get { 
               if(IsSuccessful) return _payload; 
               throw new InvalidOperationException(); 
             }
         set { _payload = value; }
     }
}

See https://dotnetfiddle.net/fqpSQS

For reference:

InvalidOperationException

: This may get you into trouble as soon as you try to serialize / deserialize.

About the Author

Question Info

Publish Time
2023-01-25 16:35
Update Time
2023-01-25 16:35

Related Question

WinAPI BluetoothFindFirstDevice在c#中不返回任何结果

Operation not allowed after ResultSet closed

从其他表中更新表的top 1字段

无法显示bottomnavigationview组件

keyring.get_password()在从Windows凭据管理器读取凭据时抛出NameError

java.lang.NoSuchMethodError: org.apache.hadoop.conf.Configuration.getPassword(Ljava/lang/String;

How to add extra class with easepick

artifactory和conan的问题

不是boto3 Bucket.objects.all()返回的所有文件夹

已解决- Rcpp在MacOS大苏尔的Vector.h中返回错误