I'm starting a new site and some of the feature they want are making me consider using an xml data structure. The first thing I considered when I though of this is making my data object serializible and simple seralizing and deserializing my objects as the methos of handling the data.
I don't see many downsides to this. I may have to write a hefty search function and a method to sort the data, but as I see it right now the fact that I only have to inherit from my base class serial and overide it's DataRoot property to make a data object save, update and delete/export looks good to me.
What do you guys think?
Code:[System.Serializable]
public abstract class Serial
{
/// <summary>A class Implamenting Serial should overide this property to provide it's root data directory.</summary>
protected abstract string DataRoot{get{return "";}}
private System.Guid _key;
/// <summary></summary>
public System.Guid Key{get{return _key;}}
/// <summary>Used internally by the inherating class to load saved data.</summary>
protected object Load(System.Guid key)
{
System.IO.FileStream fs = System.IO.File.Open(DataRoot + key.ToString(), System.IO.FileMode.Open);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
object o = bf.Deserialize(fs);
fs.Close();
return o;
}
/// <summary>Saves object data to it's DataRoot.</summary>
public void Save()
{
if(_key.ToString() == null || _key.ToString() == ""){_key = UniqueKey();}
System.IO.FileStream fs = System.IO.File.Create(DataRoot _key.ToString());
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf.Serialize(fs, this);
fs.Close();
}
/// <summary>Get's a unique key for the DataRoot.</summary>
protected System.Guid UniqueKey()
{
//this is not fool proof another item in another folder could have the key
//but nothing in here would be affected so does it matter?
System.Guid g = System.Guid.NewGuid();
if(!System.IO.File.Exists(DataRoot + g.ToString())){return g;}
return UniqueKey();
}
}
public class Class1 : Serial
{
public Class1(){}
public Class1(System.Guid key)
{
Class1 c = (Class1)Load(key);
//populate data properties
}
protected override string DataRoot
{
get
{
return "folder data files are in";
}
}
}
