如何阻止抽象类属性传递到最终结果 - WebAPI?

时间:2016-02-27 11:59:54

标签: c# asp.net-mvc-4 inheritance asp.net-web-api

我有以下场景,我从ThirdParty库获得OrderBase obstract类。我必须将这个抽象类继承到我的模型Order中以获取基本属性。只有在基本属性之下才需要作为响应的一部分返回。

  1. 编号
  2. 名称
  3. OrderHistory
  4. 但实际上,由于继承,它会将所有基本属性作为响应的一部分返回。那么在没有引入中间模型和映射的情况下,我们可以通过哪种方式限制在结果中传递基本属性。

    代码示例 - 第三方

    [DataContract]
    [Serializable]
    public abstract class OrderBase
    {
        public OrderBase(DatabaseObject obj)
        {
            this.Id = obj.Id;
            this.Name = obj.Name;
            this.Description = obj.Description;
            this.ClosingDate = obj.ClosingDate;
            this.Price = obj.Price;
        }
    
        [DataMember]
        public string Id { get; set; }
    
        [DataMember]
        public string Name { get; set; }
    
        [DataMember]
        public string Description { get; set; }
    
        [DataMember]
        public decimal Price { get; set; }
    
        [DataMember]
        public string ClosingDate { get; set; }
    }
    
    public class DatabaseObject
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public string ClosingDate { get; set; }
        public string OrderHistory { get; set; }
    }
    

    型号:

    [DataContract]
    [Serializable]
    public class Order : OrderBase
    {
        public Order(DatabaseObject dbObject)
            : base(dbObject)
        {
            this.OrderHistory = dbObject.OrderHistory;
        }
    
        [DataMember]
        public string OrderHistory { get; set; }
    }
    

    API代码:

    public class OrderController : ApiController
    {
            public Order GetOrder()
            {
                var dbObj = new DatabaseObject
                {
                    Id = "O001",
                    Name = "Masala Packets",
                    ClosingDate = "01/02/2016",
                    Description = "Payment Successful",
                    OrderHistory = "",
                    Price = 10000
                };
    
                var orderObj = new Order(dbObj);
    
                return orderObj;
            }
     }
    

    当前的JSON结果:

     {
          "OrderHistory": "",
          "Id": "O001",
          "Name": "Masala Packets",
          "Description": "Payment Successful",
          "Price": 10000.0,
          "ClosingDate": "01/02/2016"
        }
    

    预期的JSON结果:

    {
          "OrderHistory": "",
          "Id": "O001",
          "Name": "Masala Packets"     
    }
    

2 个答案:

答案 0 :(得分:1)

您直接序列化您的域模型。这可能不是一个好主意。最好创建一个视图模型来发送序列化数据,您将完全控制序列化的内容以及更好的关注点分离。像OrderDTO

这样的东西
public class OrderDTO {

    public string Id { get; set; }

    public string Name { get; set; }

    public string OrderHistory { get; set; }
}

在您的网络API方法中:

public class OrderController : ApiController
{
        public OrderDTO GetOrder()
        {
            // return an OrderDTO instead;
        }
}

或者,如果要公开域类,可以使用JsonIgnore属性从序列化中排除属性:

[DataContract]
[Serializable]
public abstract class OrderBase
{
    public OrderBase(DatabaseObject obj)
    {
        this.Id = obj.Id;
        this.Name = obj.Name;
        this.Description = obj.Description;
        this.ClosingDate = obj.ClosingDate;
        this.Price = obj.Price;
    }

    [DataMember]
    public string Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    [JsonIgnore]
    public string Description { get; set; }

    [DataMember]
    [JsonIgnore]
    public decimal Price { get; set; }

    [DataMember]
    [JsonIgnore]
    public string ClosingDate { get; set; }
}

答案 1 :(得分:0)

使用您不希望序列化为JSON的媒体资源上的[ScriptIgnore]属性。

如果您不想在父类中执行此操作,则应该遮蔽或重载子类中的属性并在其中添加属性。

How to exclude property from Json Serialization