Click here to Skip to main content
15,891,688 members
Articles / Programming Languages / C#

Scheme of Persistence

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
19 Nov 2007CDDL3 min read 23.1K   20  
Mapping business objects and Store Procedures
using System;
using System.Collections;
using System.Reflection;
using System.Xml;

namespace SQLSPMapping
{
	
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field)]
	public class Persistent:Attribute
	{
		static Hashtable persistentClass=new Hashtable();
		private readonly Class cl=new Class();
		public Class Class{get{return cl;}}
		public Persistent(Type type)
		{
			if(persistentClass.Contains(type.GUID))
			{
				cl=(Class)persistentClass[type.GUID];
			}
			else
			{
				Load(cl,type);	
				persistentClass[type.GUID]=cl;
			}
		}

		
		private void Load(Class cl,Type tipo)
		{	
			cl.Name=tipo.FullName.Substring(tipo.FullName.LastIndexOf('.')+1);
			string lugAssembly=tipo.Assembly.Location;
			string trayClass=tipo.FullName.Substring(0,tipo.FullName.Length-cl.Name.Length-1).Replace(".","/");
			string nomArc=lugAssembly.Substring(0,lugAssembly.LastIndexOf('.'))+".pers.xml";
			string trayCnTipo="Mapping/"+trayClass+"/Class[@name='"+cl.Name+"']";
			string traySPTipo="Mapping/"+trayClass+"/Class[@name='"+cl.Name+"']/Procedure";
			string traySelTipo="Mapping/"+trayClass+"/Class[@name='"+cl.Name+"']/Select";
			string nomProc="";
			string nomSP="";
			string assemblyConnection="";
			string assemblyConnClass="";
			string assemblyConnString="";
			string typeAsmConnection="";
			string typeConnClass="";
			string typeConnString="";
			string procedureAsmConnection="";
			string procedureConnClass="";
			string procedureConnString="";
			Procedure proc;
			XmlDocument doc=new XmlDocument();
			doc.Load (nomArc);
			XmlNode nodoSel=doc.SelectSingleNode("Mapping");
			if(nodoSel!=null)
			{
				GetValuesConnection(ref assemblyConnection,ref assemblyConnClass,ref assemblyConnString,nodoSel);
			}
			nodoSel=doc.SelectSingleNode(trayCnTipo);
			if(nodoSel!=null)
			{
				GetValuesConnection(ref typeAsmConnection,ref typeConnClass,ref typeConnString,nodoSel);
			}

			nodoSel=doc.SelectSingleNode(traySelTipo);
			if(nodoSel!=null)
			{
				XmlNode nodoSel1=nodoSel.SelectSingleNode("Query");
				if(nodoSel!=null)
				{
					cl.QuerySelect=nodoSel1.InnerText;
				}
				else
				{
					throw new ApplicationException("Statement 'Select' without query for class"+cl.Name);
				}
				LoadParameter(nodoSel,cl.SelectParameters,"Parameters/parameter");
			}


			XmlNodeList nodos=doc.SelectNodes(traySPTipo);
			foreach(XmlNode nodo in nodos)
			{
				procedureAsmConnection="";
				procedureConnClass="";
				procedureConnString="";
				GetValuesConnection(ref procedureAsmConnection,ref procedureConnClass,ref procedureConnString,nodo);
				nomProc=nodo.Attributes["Method"].Value;
				nomSP=nodo.Attributes["StoreProcedure"].Value;
				proc=new Procedure(nomSP,nomProc);
				proc.Connection=nodo.Attributes["Connection"]==null ? "" : nodo.Attributes["Connection"].Value;
				LoadParameter(nodo,proc.InputParameters,"InputParameters/parameter");
				LoadParameter(nodo,proc.OutputParamters,"OutputParameters/parameter");
				proc.AssemblyConnection=procedureAsmConnection.Length!=0?procedureAsmConnection:typeAsmConnection.Length!=0?typeAsmConnection:assemblyConnection;
				proc.ClsConnection=procedureConnClass.Length!=0?procedureConnClass:typeConnClass.Length!=0?typeConnClass:assemblyConnClass;
				proc.Connection=procedureConnString.Length!=0?procedureConnString:typeConnString.Length!=0?typeConnString:assemblyConnString;
				cl.Methods.Add(nomProc,proc);
			}
		}
		
		private void LoadParameter(XmlNode nodo,ArrayList al,string query)
		{
			XmlNodeList parameters=nodo.SelectNodes(query);
			try
			{
				Parameter p;
				string nameProp;
				string nameSP;
				foreach(XmlNode parameter in parameters)
				{
					nameProp=parameter.Attributes["Property"].Value;
					nameSP=parameter.Attributes["ParameterBD"].Value;
					p=new Parameter(nameSP,nameProp,0);
					al.Add(p);
				}
			}
			catch(Exception)
			{
				throw new ApplicationException("No estan bien definidos los parametros");
			}
		}
		

		private void GetValuesConnection(ref string assm,ref string className,ref string connectionString,XmlNode nodo)
		{
			if(nodo.Attributes["Assembly"]!=null)
			{
				assm=nodo.Attributes["Assembly"].Value;
			}
			if(nodo.Attributes["ConnectionClass"]!=null)
			{
				className=nodo.Attributes["ConnectionClass"].Value;
			}
			if(nodo.Attributes["Connection"]!=null)
			{
				connectionString=nodo.Attributes["Connection"].Value;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Web Developer
Mexico Mexico
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions