类是否应该能够通过 XML 保存和加载自己? [英] Should classes be able to save and load themselves via XML?

查看:59
本文介绍了类是否应该能够通过 XML 保存和加载自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我主要是在这里寻找最佳实践的答案.我有一个系统,它基本上是一组规则、一些数据和一个确保数据遵循规则的经理.很简单.我试图找出处理保存和加载规则的最佳方式.第一次尝试时,我有一个名为 IRule 的接口,所有规则都继承了该接口.

So mainly I'm looking for a best practices sort of answer here. I have a system which is basically a set of rules, some data, and a manager to make sure the data follows the rules. Pretty simple. I'm trying to figure out the best way of handling saving and loading of the rules. For a first attempt, I have an interface called IRule which all the rules inherit.

interface IRule
{
  /* all rules inherit this */
  string RuleName {get;}
}
interface IRuleTypeA : IRule
{
  /* Type A Rules */
  double[] Data;
}

interface IRuleTypeB : IRule
{
  /* Type B Rules */
  double[,] Data;
}

public static class IRuleExtensions
{
  XElement AsXml(this IRuleTypeA ruleA)
  {
    //format my data my way and return an XElement representation
    return new XElement(...);
  }
  XElement AsXml(this IRuleTypeB ruleB)
  {
    //format my different data in a different way and return an XElement
    return new XElement(...);
  }
  XElement AsXml(this IRule rule)
  {
    object definition = null;
    if(rule is IRuleTypeA)
      definition = (rule as IRuleTypeA).AsXml();
    else if(rule is IRuleTypeB)
      definition = (rule as IRuleTypeB).AsXml();
    return new XElement("RuleDefinition", new XAttribute("name", rule.RuleName),
                         definition);
  }
}

所以在这里,我将保存功能放入正在保存的类中.我即将实现一个类似的加载机制,它将获取每个元素并构建一个规则.在我这样做之前,我想看看是否有这方面的最佳实践.我应该创建一个单独的系统来接受规则,对其进行询问,然后将其保存到 XML 并使用工厂类型系统来使用 Xml 来构造对象?

So here, I'm putting the saving functionality into the class being saved. I was about to implement a similar loading mechanism that will take each element and construct a rule. Before I do that, I wanted to see if there were best practices about this. Should I instead create a separate system which would take a rule, interrogate it, then save that to XML and use a factory-type system to consume the Xml to construct the objects back?

interface RuleToXmlAdaptor
{
  XElement AsXml(IRule rule);
  IRule FromXml(XElement element);
}
class RuleAToXmlAdaptor : RuleToXmlAdaptor
{
  XElement AsXml(IRule rule) { return AsXml(rule as IRuleTypeA); }
  XElement AsXml(IRuleTypeA rule);
}

我在这些方法之间来回走动,不确定何时使用一种或另一种方法,或者是否还有更好的解决方案.

I go back and forth between these methods not really sure when to use one or the other or if there's a better solution still.

我不想使用序列化,因为文件类型可能会不断变化,我可以通过这种方式简单地管理兼容性.

I do not want to use Serialization because the file type is likely to change continuously and I can manage the compatibility fairly simply this way.

推荐答案

我不认为类本身应该知道如何序列化它.这违反了单一责任原则.想想明天会出现一种新的令人敬畏的序列化格式的情况,例如 SoupML.如果您让对象之外的人来处理序列化,那么让您的系统与 SoupML 一起工作应该很容易,您甚至不必更改现有的规则模型.

I don't think the class itself should know anything about how it can be serialized. That violates the single-responsibility principle. Think about the case where tomorrow a new awesome serialization format comes out, say SoupML. If you let someone outside the object take care of the serialization, then it should be really easy to make your system work well with SoupML and you won't even have to change the existing rule models.

理想情况下,只有那些明确使用 XML 的单元才应该知道 XML,这样您的系统就可以由不关心格式而只关心接口的单元组成.稍后他们将被告知使用哪个序列化器/反序列化器(控制反转).拥有这种方式也支持开闭原则.您不必为了修改而撬开东西,但它可以扩展.

Ideally only those units that explicitly consume XML should be aware of XML, this way your system can be made out of units that wouldn't care about the format but only about the interfaces. Somewhere later they will be told which serializer/deserializaer to use (Inversion of Control). Having it this way also supports open-close principle. You won't have to pry open stuff for modification, but it will be open for extension.

这篇关于类是否应该能够通过 XML 保存和加载自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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