简体   繁体   中英

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.

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. 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.

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.

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?

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.

What would the function prototype of a getConfigurationData method look like?

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. 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'; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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