Save/Load Properties to file or database provider
I need to save and load properties of a Class dynamicly, but what is the best practis for this ?
I have for now, two classes that I need to save.
public abstract class BaseComponent {
protected int ComponentID { get; set; }
protected string ComponentName { get; set; }
protected Dictionary GetAllProperties{) { /* Reflection */ }
}
public class Article : BaseComponent {
protected string Title { get; set; }
protected string Content { get; set; }
}
Here I'm thinking to table:
Table: Component, ComponentID, Parrent -> and more Table: ComponentProperties: ComponentID, Key, V开发者_高级运维alue -> and more
I need to use as must of the dotNet framework, but still keep it simpel. Im think og use a Provider, I need the function og make different data provider:thatcan save to xml file, sql database or oracle database, you name it.
Do I use a provider, or somethnig else ?
The answer for your persistence will depend very much on the scale of what you're writing. Storing XML in files or a database certainly works, as does a metadata driven generic schema (as you described), but making them work for large scale data can be problematic. Salesforce.com did a recent presentation on how they handle dynamic entities in a multi-tenant architecture (see here), but I doubt you need anything of that scale.
My recommendation would be to store as XML to start. The key issues to solve with the generic schema are joins between logical entities and the trouble of pulling together data that belongs to a single entity when it's split out into key/value pairs in a table.
.Net Framework 2.0 or later has a great class that helps you to implement this task, called SettingsBase
. You can find it in System.Configuration
assembly. You have to create a class for instance BaseComponentSettings
and derive it from System.Configuration.SettingsBase
. SettingsBase
is provider model and you can support many data source as well.
精彩评论