避免在域模型中使用JsonIgnore属性 [英] Avoid using the JsonIgnore attribute in a domain model

查看:62
本文介绍了避免在域模型中使用JsonIgnore属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个实体类的领域模型组件.在另一个组件中,我具有使用Json.NET序列化实现的实体存储库.我想在序列化过程中忽略某些实体属性,因此直接的解决方案是用JsonIgnore属性装饰这些属性.但是,从原则上讲,我想避免在我的域模型中引用其他组件,包括Json.NET之类的第三方库.

I have a domain model component with several entity classes. In another component i have entity repositories implemented using Json.NET serialization. I want to ignore some of the entity properties during serialization, so the straight forward solution would be to decorate those properties with the JsonIgnore attribute. However, out of principle, i would like to avoid references to other components - including 3rd party libraries like Json.NET - in my domain model.

我知道我可以按照

I know that I can create a custom contract resolver as described here but it is hard to generalize what to serialize and what not to serialize in the various entities. Generally I want to ignore all readonly properties, but there are exceptions as for example collections:

public List<Pixel> Pixels
{
    get { return this.Pixels; }
}

我还可以为每个实体创建专用的合同解析器,如

I can also create a dedicated contract resolver for each entity as described here but that seems like a high-maintenance solution to me - especially with numerous entities.

理想的解决方案是Json.NET是否支持.NET框架中的某些属性,但我什至找不到合适的候选人...

The ideal solution would be if Json.NET had support for some attribute within the .NET framework, but I cannot even find an appropriate candidate...

我考虑过在域模型中制作自己的自定义Ignore属性,并制作一个自定义合同解析器,该合同解析器使用反射来检测此属性,并在序列化时忽略修饰的属性.但这真的是解决给定问题的最佳解决方案吗?

I thought about making my own custom Ignore attribute in my domain model and making a custom contract resolver that uses reflection to detect this attribute and ignores the decorated properties when serializing. But is that really the best solution to the given problem?

推荐答案

DataContractAttribute .尽管您必须具有包容性而不是排他性,但这也意味着序列化可以更改为Microsoft的Binary(或xml),而不必重新设计域模型.

I believe by default that Json.net Respects the DataContractAttribute. Although you have to be inclusive instead of exclusive, it also means that the serialization can change to Microsofts Binary (or maybe xml) and not have to redesign your domain models.

如果一个类具有许多属性,而您只想序列化它们的一小部分,则将JsonIgnore添加到所有其他属性将是乏味且容易出错的.解决此情况的方法是将DataContractAttribute添加到类中,并将DataMemberAttributes添加到要序列化的属性中.这是选择加入序列化,与使用JsonIgnoreAttribute的选择退出序列化相比,只有您标记的属性才被序列化.

If a class has many properties and you only want to serialize a small subset of them then adding JsonIgnore to all the others will be tedious and error prone. The way to tackle this scenario is to add the DataContractAttribute to the class and DataMemberAttributes to the properties to serialize. This is opt-in serialization, only the properties you mark up with be serialized, compared to opt-out serialization using JsonIgnoreAttribute.

[DataContract]
public class Computer
{
  // included in JSON
  [DataMember]
  public string Name { get; set; }
  [DataMember]
  public decimal SalePrice { get; set; }

  // ignored
  public string Manufacture { get; set; }
  public int StockCount { get; set; }
  public decimal WholeSalePrice { get; set; }
  public DateTime NextShipmentDate { get; set; }
}

这篇关于避免在域模型中使用JsonIgnore属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆