By dsandor at July 17, 2009 00:12
Filed Under:
At my wits end with some seriously lame code I stumbled upon today. Believe it or not, I actually saw a developer use a dead open source .ini reader/writer to read and write ONE setting in a config file. The best part is the open source project they used was not originally intended to be used that way. It was originally used to read/write settings in a .INI file. Once again… Carpenter.. Hammer.. used on everything. My sincerest apologies to the carpenters.
Problem: I want to read, write and store data on disk. .NET since 1.0 has had facilities built in to serialize classes to XML (and other medium). This works out well if you for example have an ‘order’ class that contains a customer’s order. That order class would have properties containing order data and possibly a List<> of OrderedItems which in turn would have it’s own set of properties. So you want to persist this Order to disk? Serialize it and write it.
Solution: Serialize the data to XML and write it to disk. I will provide clear cut examples of this in part 2 of this post. For now, I wanted to post a base class I use that I wrote long ago that lets you Load, Save and Enqueue an object to Disk or to an MSMQ Message Queue. I find it very useful in storing configuration settings and for sending MSMQ Messages.
First is the interface: ILoadableSavableQueueable
using System;
namespace ConfigHelper
{
interface ILoadableSaveableQueueable
{
void Enqueue(string msmqDestinationPath);
void SaveToXML(string filename);
string ToString();
}
}
Second is the class implementation: LoadableSavableQueueable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Reflection;
using System.Xml.Serialization;
using System.IO;
namespace ConfigHelper
{
public class LoadableSaveableQueueable<T> : ILoadableSaveableQueueable
{
public static T LoadFromXML(string filePath)
{
XmlSerializer xer = new XmlSerializer(typeof(T));
StreamReader sr = File.OpenText(filePath);
T config = (T)xer.Deserialize(sr);
sr.Close();
sr.Dispose();
return config;
}
public void SaveToXML(string filename)
{
if (!Directory.Exists(Path.GetDirectoryName(filename)))
Directory.CreateDirectory(Path.GetDirectoryName(filename));
XmlSerializer xer = new XmlSerializer(typeof(T));
if (File.Exists(filename))
File.Delete(filename);
using (FileStream fs = File.Create(filename))
{
xer.Serialize(fs, this);
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("--] {0}", this.GetType().Name);
foreach (PropertyInfo pi in this.GetType().GetProperties())
{
sb.AppendFormat("{0}: '{1}'\r\n", pi.Name, pi.GetValue(this, null));
}
return sb.ToString();
}
public void Enqueue(string msmqDestinationPath)
{
using (MessageQueue q = new MessageQueue(msmqDestinationPath))
{
Message msg = new Message(this);
msg.Recoverable = true;
q.Send(msg);
msg.Dispose();
}
}
}
}
Simple example:
You have a class that you want to read and write to XML to disk. That class is called ConfigSettings and contains a property called ProgramName.
[Serializable]
public class ConfigSettings : LoadableSavableQueueable<ConfigSettings>
{
public ConfigSettings() {}
private string _ProgramName;
public string ProgramName
{
get { return _ProgramName; }
set { _ProgramName = value; }
}
}
Now you can use the LoadFromXML and SaveToXML methods to read and write this class to XML file on disk.
Again, a better example is to follow.
f81e98e0-a040-4278-8adc-4dd7d7c186a1|0|.0
Tags: