I don't know how many times I wanted my cache objects to be flexible. Often you end up writing tons of cache objects, 9 out of 10 times model after the database layout <grin> and when it's time to use them, it's tedious to iterate over them. Now since I am looking into System.Xml; these days I stumbled over the IXPathNavigable interface which has 1 method that returns the XPathNavigator object. With the XPathNavigator object I can use XPath expressions. So what if my cacheobject had the ability to serialize it self to memory passing it on the the XPathDocument, thereby loading my cacheobject as valid xml and then call CreateNavigator. This would actually give me the opportunity to use xpath on my cache object. hmm
some code to illustrate this:
[XmlRootAttribute(ElementName = "vstudio",IsNullable = false)] public class xmldoc : IXPathNavigable {
[System.Xml.Serialization.XmlArrayAttribute(ElementName = "namespaces", IsNullable = false)] [System.Xml.Serialization.XmlArrayItemAttribute("class", Type = typeof(System.String), IsNullable = false)] public ArrayList framework; [System.Xml.Serialization.XmlElementAttribute("msdn")] public string msdn; [System.Xml.Serialization.XmlElementAttribute("date")] public string date; [System.Xml.Serialization.XmlElementAttribute("price")] public int price;
public XPathNavigator CreateNavigator() { MemoryStream memStream = new MemoryStream(); XmlSerializer serializer = new XmlSerializer(typeof(xmldoc)); TextWriter writer = new StreamWriter(memStream); this.framework = new ArrayList(); for(int i = 0; i < 3; i++) this.framework.Add("IXPathNavigable" + i.ToString()); this.msdn="msdn"; this.date="16-04-2003"; this.price=2999; serializer.Serialize(writer, this); memStream.Seek(0,0); XPathDocument doc = new XPathDocument(memStream); writer.Close(); memStream.Close(); return doc.CreateNavigator(); } }
now on the client side you do this:
xmldoc x = new xmldoc(); XPathNavigator nav = x.CreateNavigator(); XPathNodeIterator itr = nav.Select("/vstudio/namespaces/class"); while(itr.MoveNext()) { System.Diagnostics.Trace.WriteLine(itr.Current.Name + ": " + itr.Current.Value); }
I know it's far from perfect, but there are some cool potentials here...
"Java is high performance. By high performance we mean adequate. By adequate we mean slow." - Mr. Bunny
wow I am tired, Goodnight.
12:33:13 AM
|