为 Web 服务实现我自己的 IXmlSerializer
我正在制作一个传递几个对象的 asmx Web 服务,我有两个问题:第一是我使用字典,第二是某些对象具有只读属性。
我已经对网络服务做出了足够的妥协,我不会再给他赢得另一场战斗的喜悦,所以我决定为我的对象编写自己的序列化器。这是一个好的决定吗?我说的是 7 个类和一个字典,它们都具有某种只读属性。
我必须为每个类制作一个架构文件吗?看看 IXmlSerializer 实现,它看起来并不太复杂,我所要做的就是在 ReadXml 中为该类编写一个“构造函数”,并在 WriteXml 中编写一个“解构函数”,不是吗?
编辑:好的,这回答了一个问题,现在当我在客户端获取对象时,它们会作为 Dataset[] 被接收,如何将它们转换为适当的类?
I'm making an asmx web service that passes a couple of objects, I have two issues with that: the first is that I use a dictionary and the second is that some of the objects have read only properties.
I've made enough compromises with the web service and I'm not going to give him the joy of winning another battle so I've decided to write my own serializer for my objects. Is that a good decision? I'm talking about 7 classes and one dictionary that all have some sort of a read only property.
Do I have to make a schema file for each class? And looking at the IXmlSerializer implementation it doesn't seem too complicated, all I have to do is write a "constructor" for the class in ReadXml and a "deconstructor" in WriteXml, No?
Edit: Okay, that answers one question, now when I get the objects in the client they are received as a Dataset[], how do I convert them to the appropriate class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
总的来说,你是对的,对于给定的单个类来说并不是很复杂。
如果您有一个如您所描述的完整的对象图,那么事情就会变得复杂。您只需提出一个 XML 结构,其中包含构造类型实例和所有依赖类型所需的所有信息。
这可能需要对您的类进行更改才能工作,即,如果依赖类型的实例通常由您的主类在内部构造,那么您现在如何在外部构造该实例并将其注入到您的类中?不太漂亮 - 因此,如果对象图在对象之间有很多依赖关系,我会三思而后行。如果它符合您当前为对象依赖关系和创建策略定义的内容,请务必使用它。
Overall you are right it is not very complicated for a given single class.
Where it can get complicated is if you have a full object graph as you describe. You just have to come up with an XML structure that contains all the information you need to construct an instance of your type and all dependent types.
This might require changes in your classes as well to work, i.e. if an instance of a dependent type is usually internally constructed by your main class, how to you construct this instance now externally and inject it into your class? Not pretty - So if there is an object graph with a lot of dependencies between objects I would think twice about this approach. If it fits with what you currently have defined though for object dependencies and creation policies by all means go for it.