Skip to main content
 首页 » 编程设计

c#之在多个客户端使用的 Web API 中公开枚举是否是一个糟糕的设计决策

2024年02月13日28rubylouvre

<分区>

我目前正在开发一个可供多个客户使用的 RESTful API(使用 Web API)。可以向消费者公开枚举吗?比如

   public enum StatusType{Open,Closed} 

然后当客户调用 GetOrder 时,我会将其返回给他们:

   public class Order 
    { 
      public StatusType CurrentStatus{get;set;} 
      public int OrderId{get;set;} 
    } 

其中一位开发人员说不应该这样做,因为我们不应该向客户端消费者公开内部类型,而我们应该在表单中为此创建一个类。

   public class OrderStatus 
    { 
     public string Status{get;set;} 
     public int Id{get;set;} 
    } 

然后当客户调用 GetOrder 时,我会将其返回给他们:

public class Order 
{ 
  public OrderStatus CurrentStatus { get; set; } 
  public int OrderId { get; set; } 
} 

请问我应该选择哪一个,为什么?

谢谢 尼尔