Sure,
First, create an interface of type MessageAction. Give it a method of
GetXSLT(param1,param2...)
Derive a series of classes from MessageAction, each one implementing one of
the different sections in your GetXSLT() routine below.
Create a factory method in MessageController that returns an object of type
"MessageAction" when provided with a string describing the action.
In the MessageAction classes, have them look to a config file to get the
name of the XSLT to use for each message action. Do this in the
constructor. This gives you more flexibility to create a new MessageAction.
MessageAction ma = this.CreateMessageAction(mstrAction);
return ma.GetXSLT(GetMetaDataSet().GetXml().ToString());
--- Nick
P.S. I'm not sure what CreateEntity does, but it doesn't fit the pattern of
the rest. Is there something structurally different with this one?
You may have a design defect if you need to do something structurally
different in a class like this.
"chrisybhoy" <chris.millar@voyage.co.uk> wrote in message
news:%23KNN9Pj8DHA.2064@TK2MSFTNGP11.phx.gbl...[color=blue]
> The class below uses the switch clause in the GetXSLT method. I want it[/color]
to code it with a more OO approach does anyone have any suggestions, or
change to make.[color=blue]
>
> cheers
>
> using System;
> using System.Collections;
> using System.Configuration;
> using System.Data;
> using System.IO;
> using System.Xml;
> using System.Xml.XPath;
> using System.Xml.Xsl;
> using System.Diagnostics;
>
> namespace MetaData
> {
> /// <summary>
> /// MessageController Class
> /// This class is used to parse the ORE XML Messages, and identify which[/color]
set of XSLT transforms to use to[color=blue]
> /// process the XML to fill the ORE Message with meta data. Depending on[/color]
the ORE XML Message an instance[color=blue]
> /// of the DAOFacade class is instantiated and the retrieve method is[/color]
called to access the DB Meta Repository[color=blue]
> /// In order to start the process of reading debug strings you should do[/color]
the following:[color=blue]
> /// 1) Set the string XMLMessage property to the ORE XML Message.
> /// 2) execute the MessageParser instance method, which instances GetXSLT[/color]
method;[color=blue]
> /// 3) register at least one event handler for the DebugData event;
> /// 4) Depending on the Action value, Transform mehtod is executed and/or[/color]
GetMetaData/ UpdateMetaData methods.[color=blue]
> /// </summary>
> public class MessageController
> {
> #region Constructor
>
> public MessageController()
> {
> mstrXSLTDir = ConfigurationSettings.AppSettings["XSLTDir"];
> mstrMetaConn = ConfigurationSettings.AppSettings["MetaConnString"];
> }
> #endregion
>
> #region Variable Declarations
> public static Hashtable xmlEntityCache = new Hashtable();
> public static Hashtable xsltCache = new Hashtable();
>
> private string mstrXmlMessage;
> private string mstrDACMessage;
> private string mstrEntityName;
> private string mstrXSLTDir;
> private string mstrMetaConn;
> private string mstrAction;
>
> #endregion
>
> #region Message Controller Properties
> public string xmlAction
> {
> get
> {
> return mstrAction;
> }
> set
> {
> mstrAction = value;
> }
> }
> public string xmlEntityName
> {
> get
> {
> return mstrEntityName;
> }
> set
> {
> mstrEntityName = value;
> }
> }
> public string xmlInMessage
> {
> get
> {
> return mstrXmlMessage;
> }
> set
> {
> mstrXmlMessage = value ;
> }
> }
>
> public string DACOutMessage
> {
> get
> {
> return mstrDACMessage;
> }
> set
> {
> mstrDACMessage = value ;
> }
> }
> #endregion
>
> #region Message Parser Method
> /// <summary>
> /// Message Parser Method retrieves the values of the Entity and Action[/color]
attributes[color=blue]
> /// and uses property mstrXMLMessage to find the Action and Entity value.
> /// </summary>
> /// <returns>XML message response as string</returns>
> public string MessageParser()
> {
> XmlDataDocument xmlDoc = new XmlDataDocument();
>
> xmlDoc.LoadXml(mstrXmlMessage);
>
> XPathNavigator Nav = xmlDoc.CreateNavigator();
>
> System.Xml.XPath.XPathNodeIterator ActionIterator =[/color]
Nav.Select("//message/@action");[color=blue]
> System.Xml.XPath.XPathNodeIterator EntityIterator =[/color]
Nav.Select("//message/Entity/@Name");[color=blue]
>
> ActionIterator.MoveNext();
> EntityIterator.MoveNext();
>
> mstrAction = ActionIterator.Current.Value.ToString();
> //TODO: if Action is Retrieve then check cache for schemas, iterate[/color]
through entities.[color=blue]
>
> mstrEntityName = EntityIterator.Current.Value.ToString();
>
> return GetXSLT();
> }
> #endregion
>
> #region Private Methods and Constructors
>
> /// <summary>
> /// Parses XML strings to Hashtable
> /// </summary>
> /// <param name="entityName"></param>
> /// <returns>Values of elements in Hashtable</returns>
> private bool ParseToHashtable(string entityName)
> {
> if(xmlEntityCache.ContainsKey(entityName))
> {
> //return cached schema
> return false;
> }
> else
> {
> return true;
> }
> }
> /// <summary>
> /// Entity Processor performs the main XSLT's and queries database to[/color]
return schemas as[color=blue]
> /// output to DAC or ORE
> /// </summary>
> /// <returns>XML String for DAC (Schema, DataSet)</returns>
> /// <paramref name="No Parameters"/>
> public string GetXSLT()
> {
> string result;
> switch (mstrAction)
> {
> case "CreateEntity":
>
> result = MessageController.Transform(mstrXSLTDir + "\\" +[/color]
"createEntity_dac.xslt", mstrXmlMessage);[color=blue]
>
> return result;
>
> case "Create":
>
> //result = MessageController.Transform(mstrXSLTDir + "\\" +[/color]
"DataSetFromDAC.xslt", GetMetaDataSet().GetXml().ToString());[color=blue]
> result = MessageController.Transform(mstrXSLTDir + "\\" +[/color]
"metamessage.xslt", GetMetaDataSet().GetXml().ToString());[color=blue]
> return result;
>
> case "Retrieve":
>
> //result = MessageController.Transform(mstrXSLTDir + "\\" +[/color]
"DataSetFromDAC.xslt",GetMetaDataSet().GetXml().To String());[color=blue]
> result = MessageController.Transform(mstrXSLTDir + "\\" +[/color]
"metamessage.xslt", GetMetaDataSet().GetXml().ToString());[color=blue]
>
> MessageCache.SaveToFileCache(result, mstrEntityName);
>
> return result;
>
> case "Update":
>
> //result = MessageController.Transform(mstrXSLTDir + "\\" +[/color]
"DataSetFromDAC.xslt",GetMetaDataSet().GetXml().To String());[color=blue]
> result = MessageController.Transform(mstrXSLTDir + "\\" +[/color]
"metamessage.xslt",GetMetaDataSet().GetXml().ToStr ing());[color=blue]
>
> MessageCache.SaveToFileCache(result, mstrEntityName);
>
> return result;
>
> case "RetrieveSchema":
>
> if (ParseToHashtable(mstrEntityName) == false)
> {
> return xmlEntityCache[mstrEntityName].ToString();
> }
> else
> {
> //DataSet dacresult meta query to get entity
> //DataSet dacresult1 = new DataSet();
> //dacresult1.ReadXml(mstrXSLTDir + "\\" + "oregetschemadac.xml");
>
> result = MessageController.Transform(mstrXSLTDir + "\\" +[/color]
"OreGetSchema.xslt", GetMetaDataSet().GetXml().ToString());[color=blue]
>
> xmlEntityCache.Add(mstrEntityName, result);
> return result;
> }
> }
> return "error";
> }
>
> /// <summary>
> /// Returns the xml node as a string of all the connection details.
> /// </summary>
> /// <param name="MetaConnString"></param>
> /// <param name="DLLFileName"></param>
> private void GetConnection(out string MetaConnString, out string[/color]
DLLFileName)[color=blue]
> {
> XmlDocument doc = new XmlDocument();
> doc.Load(mstrXSLTDir + "\\" + "Provider_Types1.xml");
>
> DLLFileName = doc.SelectSingleNode("NewDataSet/DataSource[IsMaster[/color]
= -1]/FileName").InnerXml;[color=blue]
> MetaConnString = doc.SelectSingleNode("NewDataSet/DataSource[IsMaster[/color]
= -1]/ConnectionString").InnerXml;[color=blue]
>
> }
>
> private DataSet GetMetaDataSet()
> {
> try
> {
> //Transform entity dataset
> string tmpEntityMetaData = MessageController.Transform(mstrXSLTDir + "\\"[/color]
+ "EntityDataSetnew.xslt", mstrXmlMessage);[color=blue]
>
> string tmpEntityMetaDataMessage = MessageController.Transform(mstrXSLTDir[/color]
+ "\\" + "EntityDataSetMeta.xslt", mstrXmlMessage);[color=blue]
>
> //Get entity dataset from DAC giving tmpEntityMetaData
> DAOFacade daoentity = new DAOFacade();
> DataSet EntityData = new DataSet();
>
> EntityData = daoentity.RetrieveData(tmpEntityMetaData,[/color]
tmpEntityMetaDataMessage);[color=blue]
>
> //EntityData.ReadXml(mstrXSLTDir + "\\" + "EntityDataSet.xml");
>
> //Transform entity dataset
> string tmpAttributeMetaData = MessageController.Transform(mstrXSLTDir +[/color]
"\\" + "AttributeDataSetnew.xslt", EntityData.GetXml().ToString());[color=blue]
>
> string tmpAttributeMetaDataMessage =[/color]
MessageController.Transform(mstrXSLTDir + "\\" +
"AttributeDataSetMeta.xslt", EntityData.GetXml().ToString());[color=blue]
>
> //Get Attribute dataset from DAC
> DataSet AttribData = new DataSet();
> DAOFacade daoattribute = new DAOFacade();
> AttribData = daoattribute.RetrieveData(tmpAttributeMetaData,[/color]
tmpAttributeMetaDataMessage);[color=blue]
>
> //AttribData.ReadXml(mstrXSLTDir + "\\" + "AttributeDataSet.xml");
>
> //Merge Datasets (Entity and Attribute)
> EntityData.Merge(AttribData);
>
> EntityData.Dispose();
> AttribData.Dispose();
> return EntityData;
>
> }
> catch(Exception ex)
> {
> throw(ex);
> }
> }
> /// <summary>
> /// Inserts entries into MetaData Store.
> /// </summary>
> /// <param name="oreMessage"></param>
> /// <returns></returns>
> public DataSet UpdateMetaData(string oreMessage)
> {
>
> try
> {
> //Transform entity dataset
>
> // Transform ORE orignal message to create Meta DataSet to update[/color]
MetaStore DB.[color=blue]
> string tmpEntityMetaData = MessageController.Transform(mstrXSLTDir + "\\"[/color]
+ "CreateEntity.xslt", oreMessage);[color=blue]
>
> //Create new instance of a dataset that will hold the returned[/color]
Entity/Attribute datatables.[color=blue]
> DataSet EntityData = new DataSet();
> //EntityData.ReadXmlSchema("C:\\DACXML\\EntityAttrib ute.xsd");
>
> //read the transformed xml in a StringReader
> StringReader sr = new StringReader(tmpEntityMetaData);
>
>[/color]
//sr.Read((tmpEntityMetaData.ToCharArray()),0,tmpEnt ityMetaData.ToCharArray(
).Length);[color=blue]
> EntityData.ReadXmlSchema("C:\\DACXML\\EntityAttrib ute.xsd");
> //Fill the dataset with the stream contents.
> EntityData.ReadXml(sr);
>
> sr.Close();
>
> //Create a new instance of the DAC facade
>
> //Method call to update MetaStore.
> string MetaConn;
> string MetaDLLFile;
> GetConnection(out MetaConn, out MetaDLLFile);
>
> DAOFacade DAOIns = new DAOFacade();
> DAOIns.UpdateDataSet(MetaDLLFile, MetaConn, EntityData);
>
> EntityData.Dispose();
>
> return EntityData;
> }
> catch(Exception ex)
> {
> EventLog.WriteEntry("UpdateMetaData", ex.Message);
>
> throw(ex);
> }
> }
>
> #endregion
>
> #region XSLT Transform Method
> /// <summary>
> /// Main XSLT Transform Method static method.
> /// </summary>
> /// <param name="xsltName">Name of xslt file to use in the[/color]
transform</param>[color=blue]
> /// <param name="xmlMessage">xmlMessage to transfom</param>
> /// <returns>XML transform string</returns>
> public static string Transform(string xsltName, string xmlMessage)
> {
> try
> {
> //get the xml doc.
> XmlDocument xmlDoc = new XmlDocument();
> xmlDoc.LoadXml(xmlMessage);
>
> XslTransform xslDoc = new XslTransform();
> xslDoc.Load(xsltName);
>
> XsltArgumentList args=new XsltArgumentList();
>
> MemoryStream ms = new MemoryStream();
>
> xslDoc.Transform(xmlDoc,args,ms,new XmlUrlResolver());
>
> ms.Flush();
> ms.Position =0;
> StreamReader sr = new StreamReader(ms);
> string result = sr.ReadToEnd();
> sr.Close();
> ms.Close();
>
> return result;
> }
> catch(Exception ex)
> {
> throw(ex);
> }
> finally
> {
> //ms.Close();
> }
> }
>
> #endregion
>
> }
> }
>
> ************************************************** ********************
> Sent via Fuzzy Software @
http://www.fuzzysoftware.com/
> Comprehensive, categorised, searchable collection of links to ASP &[/color]
ASP.NET resources...