Developing Application Frameworks in .Net

慕璞
2023-12-01

1. Caching Service

static variable and HashTable

XML
    ---- Element  .InnerText
    ---- attribute .attribute[key].Value

XmlDocument , XmlElement, XmlNode

XPath

/RootElement/ChildElement/Element[.='something']  [@attribute = 'somevalue'] [ChildElement = 'somevalue'] [postion() = 1] [... and/or ...] 

//Element  //ChildElement/element /Element//ChildElement

----- Configuration file:
<configuration>
    <configSections>
       <section name="Framework" type="SAF.Configuration.ConfigurationHandler,SAF.Configuration" />
        <section name="Application" type="SAF.Configuration.ConfigurationHandler,SAF.Configuration" />
    </configSections>
   
    <Framework type="SAF.Configuration.ConfigurationManager,SAF.Configuration">
        <SAF.Cache>
            <CacheStrategy type = "SAF.Cache.DefaultCacheStrategy,SAF.Cache"/>
        </SAF.Cache>
    </Framework>
</configuration>
----- Configuration Class
CacheConfig = new  CacheConfigration(cam.GetData("SAF.Cache"));
....

public class CacheConfigration
    ...{
        private XmlNode cacheXml;
        public CacheConfigration(XmlNode configData)...{
            cacheXml = configData;
        }

        public object GetCacheStrategy()...{
            string typeName = cacheXml.SelectSingleNode("CacheStrategy").Attributes["type"].Value;
            Type type = Type.GetType(typeName);
            return Activator.CreateInstance(type,null);
        }
    }
----- Cach class (singleton and stratage design pattern)
public class Cache
    ...{
        private  XmlElement objectXmlMap ;

        // Stragtegy class
        private static SAF.Cache.ICacheStrategy cs;
        private static Cache cache;
        private XmlDocument rootXml = new XmlDocument();


        /** protected  construtor, required for singleton design pattern.
        protected Cache()
        ...{
            //retrieve setting from configuration file
            ConfigurationManager cm = (ConfigurationManager)ConfigurationSettings.GetConfig("Framework");
            //load the cache strategy object
            cs = (ICacheStrategy)cm.CacheConfig.GetCacheStrategy();
            //create an Xml used as a map between  xml expression and object cached in the
            //physical storage.
            objectXmlMap = rootXml.CreateElement("Cache");
            //build the internal xml document.
            rootXml.AppendChild(objectXmlMap);   
        }

        /** Singlton method used to return the instance of Cache class
        public static Cache GetSAFCacheService()
        ...{
            if (cache == null)
            ...{
                cache = new Cache();
            }
            return cache;
        }

        /** Add the object to the underlying storage and Xml mapping document
        public virtual void AddObject(string xpath, object o)
        ...{
            //determin if group is already exist?, if not, create one.
            if (groupNode == null)
            ...{
                lock(this)
                ...{
                    //build the xml tree
                    groupNode = CreateNode(group);
                }
            }
            //get a unique key to identity of object, it is used to map
            //between xml and object key used in the cache strategy
            string objectId = System.Guid.NewGuid().ToString();
            //create an new element and new attribute for this perticular object
            XmlElement objectElement = objectXmlMap.OwnerDocument.CreateElement(element);
            XmlAttribute objectAttribute =objectXmlMap.OwnerDocument.CreateAttribute("objectId");
            objectAttribute.Value = objectId;
            objectElement.Attributes.Append(objectAttribute);
            //Add the object element to the Xml document
            groupNode.AppendChild(objectElement);

            //add the object to the underlying storage through cache strategy
            cs.AddObject(objectId,o);       
        }

        /** Retrieve the cached object using its hierarchical location
        public virtual object RetrieveObject(string xpath)
        ...{
            //....
            if ( node != null)
            ...{
                string objectId = node.Attributes["objectId"].Value;
                //retrieve the object through cache strategy
                o = cs.RetrieveObject(objectId);
            }
            return o;
           
        }

        /** Remove the object from the storage and clear the Xml assocated withthe object
        public virtual void RemoveObject(string xpath)
        ...{
            XmlNode result = objectXmlMap.SelectSingleNode(PrepareXpath(xpath));
            //check if the xpath refers to a group(container) or
            //actual element for cached object
            if (result.HasChildNodes)
            ...{
                //remove all the cached object in the hastable
                //and remove all the child nodes
                XmlNodeList objects = result.SelectNodes("*[@objectId]");
                string objectId ="";
                foreach (XmlNode node in objects)
                ...{
                    objectId = node.Attributes["objectId"].Value;
                    node.ParentNode.RemoveChild(node);
                    //use cache strategy to remove the objects from the
                    //underlying storage
                    cs.RemoveObject(objectId);
                }
            }
            else
            ...{
                //just remove the element node and the object associate with it
                string objectId = result.Attributes["objectId"].Value;
                result.ParentNode.RemoveChild(result);
                cs.RemoveObject(objectId);       
            }
        }


    /** the interface for cache strategy.
    /// each class that is pluggable to the SAF.Cache must
    /// implement this interface.
    public interface ICacheStrategy
    ...{
        void AddObject(string objId, object o);
        void RemoveObject(string objId);
        object RetrieveObject(string objId);
    }

----- Default strategy
public class DefaultCacheStrategy : ICacheStrategy
    ...{
        private Hashtable objectTable;
       
        public DefaultCacheStrategy()
        ...{
            objectTable = new Hashtable();
        }

        /** Add an object to the underlying storage
        public void AddObject(string objId, object o)
        ...{
            objectTable.Add(objId,o);
        }

        /** Remove an object from the underlying storage
        public void RemoveObject(string objId)
        ...{
            objectTable.Remove(objId);
        }

        /** Retrieve an object from the underlying storage
        public object RetrieveObject(string objId)
        ...{

            return objectTable[objId];
        }
    }

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/CrystalHareHus/archive/2007/01/10/1479252.aspx

 类似资料:

相关阅读

相关文章

相关问答