具有依赖项的 XmlSerializer 导致 Null 引用异常 [英] XmlSerializer with dependencies results in Null reference exception

查看:31
本文介绍了具有依赖项的 XmlSerializer 导致 Null 引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了我自己的类的 Xml 序列化问题.它是一个派生类,它自然没有无参数构造函数 - 我不得不添加一个只是为了序列化.当然,因此我遇到了依赖性/顺序问题.

I'm running into an issue with Xml serialization of my own class. It is a derived class, which doesn't naturally have a parameterless constructor - I had to add one just for the sake of serialization. Of course, because of that I'm running into dependency/order issue.

这是一个简化,我希望它仍然可以说明问题(如果我没有捕捉到问题,我保留增加插图的权利 - 我只是不想把复杂的对象模型倾倒在你身上:))

Here's a simplification, which I hope still illustrates the problem (I reserve the right to augment the illustration if it turns out I didn't capture the problem - I just didn't want to dump a complicated Object Model on you :))

public class Base{
  public virtual Vector Value{ get; set;}
}

public class Derived : Base{

  public Vector Coefficient { get; set; }
  public override Vector Value{
     get { return base.Value * Coefficient; }
     set { base.Value = value / Coefficient; }
  }
}

EDIT:为避免混淆,我将原始帖子中的值类型 double 替换为未在此处显示的 Vector 类型

EDIT: to avoid confusion, I substituted the value type double in the original post with a not-shown-here Vector type

当 XmlSerializer 反序列化 Derived 时,我遇到了空值异常 - base.Valuethis.Coefficient 都是 null.

When XmlSerializer de-serializes Derived, I run into null value exception - Both base.Value and this.Coefficient are null.

有什么办法可以解决这个问题吗?

Is there any way to fix this?

推荐答案

这里的很多问题似乎都源于使用域模型进行序列化.现在,这可以工作,但如果您的域模型与序列化器想要做的甚至轻微偏离,它也可能会产生巨大的问题.

It seems that a lot of the issues here stem from using your domain model for serialization. Now, this can work, but it can also be hugely problematic if your domain model deviates even slightly from what the serializer wants to do.

我强烈建议尝试添加数据的第二个并行表示,作为DTO 模型" - 意思是:一组对象,其工作是表示要序列化的数据.S 而不是具有计算和依赖关系的复杂属性,您只需:

I strongly suggest trying to add a second parallel representation of the data, as a "DTO model" - meaning: a set of objects whose job is to represent the data for serialization. S instead of a complicated property with calculations and dependencies, you just have:

public double SomeValue { get; set; }

等等.关键是它很简单,代表数据,而不是你系统的规则.您序列化到/从这个模型 - 这不应该是简单的 - 并将它映射到/从您的域模型.转换运算符可能很有用,但简单的ToDomainModel"/FromDomainModel"方法也可以正常工作.同样,像 AutoMapper 这样的工具可能会有所帮助,但 15 行 DTO-to/from-Domain 代码也不会受到伤害.

etc. The key point is that is is simple and represents the data, not your system's rules. You serialize to/from this model - which should not be simple - and you map this to/from your domain model. Conversion operators can be useful, but a simple "ToDomainModel" / "FromDomainModel" method works fine too. Likewise, tools like AutoMapper might help, but 15 lines of DTO-to/from-Domain code isn't going to hurt either.

这避免了以下问题:

  • 构造函数
  • 非公开成员
  • 分配顺序
  • 只读成员
  • 版本控制

以及序列化中的一系列其他常见痛点.

And a range of other common pain points in serialization.

这篇关于具有依赖项的 XmlSerializer 导致 Null 引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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