简体   繁体   English

序列化有状态+无状态对象的引用

[英]Serializing reference of stateful + stateless objects

This is kind of a design problem: 这是一种设计问题:

class Suite
{
  List<Plugins> Plugins {get;set;} 
}

class DataCollector : Plugin
{
  string Property1 {get;set;}
  string Property2 {get;set;}

  public void PrintMyName();    
}

class Measurement: Plugin
{
  string Property1 {get;set;}
  string Property2 {get;set;}

  public void PrintMyName();    
}

Now, I want the class suite to be serializable. 现在,我希望类套件可序列化。 The problem here is this: 这里的问题是这样的:

  1. XMl serialization is basically used to serialize DTO kind of objects so you basically serialize/deserialize your stateful properties and that is fine XMl序列化基本上用于序列化DTO类型的对象,因此您基本上可以序列化/反序列化您的有状态属性,这很好

  2. This particular class which is going to serialized contains of Type Plugin(which contains combination of properties which contains some property values) + functionalists. 这个要序列化的特定类包含Type Plugin(包含一些属性组合,其中包含一些属性值)和泛函。

  3. Which means I need to use factory to get the real instance of the object for Plugin to come to life with all its functionality with the property values. 这意味着我需要使用工厂来获取对象的真实实例,以使Plugin的所有功能与属性值一起实现。

Am I looking at an XMLSrializable + Factory combination? 我在查看XMLSrializable + Factory组合吗? Is there any good elegant way to achieve this? 有什么好的优雅的方法可以做到这一点吗?

Why not implement the IDeserializationCallback interface so upon deserialization you can bring your object back to life via a call to your factory. 为什么不实现IDeserializationCallback接口,以便在反序列化后,可以通过调用工厂使对象恢复活力。

I don't really understand the purpose of your classes, but I will give an example as best I can: 我不太了解您的课程目的,但是我将尽我所能举一个例子:

public class MyDataClass : IDeserializationCallback
{

   public int SimpleKeyProperty { get; set; }
   public string TextProperty{ get; set; }

   public void OnDeserialization(object sender)
   {
      //upon deserialization use the int key to get the latest value from
      //factory (this is make believe...)
      TextProperty = SomeFactory.GetCurrentValue( this.SimpleKeyProperty) ;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM