简体   繁体   English

如何序列化动态对象?

[英]How do I serialize a dynamic object?

I want to make a Configuration Data Manager. 我想制作一个配置数据管理器。 This would allow multiple services to store and access configuration data that is common to all of them. 这将允许多个服务存储和访问所有服务共有的配置数据。 For the purposes of the Manager, I've decided to create a configuration class object - basically what every configuration data entry would look like: Name, type, and value. 出于Manager的目的,我决定创建一个配置类对象-基本上每个配置数据条目的外观都是:名称,类型和值。

In the object these would all be strings that discribe the configuration data object itself. 在对象中,所有这些都是描述配置数据对象本身的字符串。 Once it has gotten this data from its database as strings, it would put it into this configuration object. 一旦从数据库以字符串形式获取了这些数据,便会将其放入此配置对象中。

Then, I want it to send it through WCF to its destination. 然后,我希望它通过WCF将其发送到其目的地。 BUT, I don't want to send a serialized version of the configuration object, but rather a serialized version of the object discribed by the configuration object. 但是,我不想送配置对象的序列化版本,而是由配置对象discribed对象的序列化版本。

The reason I'd like to do this is so that 我想这样做的原因是

  1. The Data Manager does not need to know anything about the configuration data. 数据管理器不需要了解任何有关配置数据的信息。
  2. So I can add configuration objects easily without changing the service. 因此,我可以轻松添加配置对象,而无需更改服务。 Of course, I should be able to do all of the CRUD operations, not just read. 当然,我应该能够执行所有CRUD操作,而不仅仅是阅读。

Summary: Input: string of name, type and value 摘要:输入:名称,类型和值的字符串

Output: Serialized output of the object; 输出:对象的序列化输出; the object itself is "type name = value" 对象本身是“类型名称=值”

Questions: 问题:

  1. Is this a good method for storing and accessing the data? 这是存储和访问数据的好方法吗?
  2. How can I/can I serialize in this manner? 我/如何以这种方式进行序列化?
  3. What would the function prototype of a getConfigurationData method look like? getConfigurationData方法的函数原型是什么样的?

I have decided to go in a different direction, thanks for the help. 感谢您的帮助,我决定朝另一个方向前进。

Is this a good method for storing and accessing the data? 这是存储和访问数据的好方法吗?

That is difficult to answer, the best I can give you is both a "yes" and a "No". 这很难回答,我能给你的最好的答案是“是”和“否”。 Yes, It's not a bad idea to isolate the serialization/rehydration of this data.... and No, I don't really care much for the way you describe doing it. 是的,隔离此数据的序列化/重新水化并不是一个坏主意。...而且不,我真的不太在乎您描述它的方式。 I'm not sure I would want it stored in text unless I plan on editing it by hand, and if I'm editing it by hand, I'm not sure I'd want it in a database. 除非计划手动编辑,否则我不确定是否希望将其存储在文本中;如果手动编辑,我不确定是否希望将其存储在数据库中。 It could be done; 可以做到的; just not sure you're really on the right track yet. 只是不确定您是否真的在正确的轨道上。

How can I/can I serialize in this manner? 我/如何以这种方式进行序列化?

Don't build your own, never that. 不要自己建立,永远不要那样。 Use a well-known format that already exists. 使用已经存在的众所周知的格式。 Either XML or JSON will serve for hand-editable, or there are several binary formats (BSON, protobuffers) if you do not need to be able to edit it. XML或JSON可用于手动编辑,或者,如果您不需要进行编辑,则可以使用几种二进制格式(BSON,原型缓冲区)。

What would the function prototype of a getConfigurationData method look like? getConfigurationData方法的函数原型是什么样的?

I would first break-down the 'general' aka common configuration into a seperate call from the service specific configuration. 我首先将“通用”(又称为通用)配置分解为来自服务特定配置的单独调用。 This enables getConfigurationData to simply return a rich type for common information. 这使getConfigurationData可以简单地返回丰富的类型以获取公共信息。 Then either add a extra param and property for service specific data, or add another method. 然后,为服务特定的数据添加额外的参数和属性,或添加其他方法。 As an example: 举个例子:

[DataContract]
public class ConfigurationInfo
{
    [DataMember]
    public string Foo;
    ...
    // This string is a json/xml blob specific to the 'svcType' parameter
    [DataMember]
    public string ServiceConfig;
}

[DataContract]
public interface IServiceHost
{
    ConfigurationInfo GetConfigurationData(string svcType);
}

Obviously you place a little burden on the caller to parse the 'ServiceConfig'; 显然,给调用者解析“ ServiceConfig”带来了一些负担。 however, your server can treat it as an opaque string value. 但是,您的服务器可以将其视为不透明的字符串值。 It's only job is to associate it with the appropriate svcType and store/fetch the correct value. 唯一的工作就是将其与适当的svcType关联,并存储/获取正确的值。

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

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