protected ORMTypeMapping FetchTypeMap(IRelatable relatee) { //Logger.Write("Testing"); //LogWriter.gger.Writer.SystemTrace.MethodStart( MethodBase.GetCurrentMethod() ); Type t = relatee.GetType(); Assembly sourceAssembly = t.Assembly; string resourceName = t.Namespace + ".ORM.xml"; XmlDocument xDoc = new XmlDocument(); StreamReader xmlRaw = new StreamReader(sourceAssembly.GetManifestResourceStream(resourceName)); // StreamReader xmlRaw = new StreamReader((esourceName); xDoc.Load(xmlRaw); string query = "//type[@fullname='" + t.FullName + "']"; XmlNode typeMapNode = xDoc.DocumentElement.SelectSingleNode(query); if (typeMapNode != null) { ORMTypeMapping typeMap = new ORMTypeMapping(typeMapNode); return(typeMap); } else { // SystemTrace.TraceError("Failed to load type map for {0}", t.FullName); // ExceptionPolicy.HandleException( new NullReferenceException("Unable to fetch type map for " + t.FullName), "Exception" ); string debug = "Unable to fetch type map for " + t.FullName; } return(null); }
private SqlCommand BuildCommandFromTypeMap(IRelatable relatee, ORMTypeMapping typeMap, string relationKey) { // Logger.Write( MethodBase.GetCurrentMethod() ); ORMCommandMap ocm = typeMap.GetMapByName(relationKey); if (ocm != null) { SqlCommand cmd = new SqlCommand(ocm.StoredProcedure, conn); cmd.CommandType = CommandType.StoredProcedure; foreach (object ob in ocm.PropertyMaps) { ORMPropertyMap propMap = (ORMPropertyMap)ob; SqlParameter newParam = CreateParameterFromPropertyMap(propMap); if ((newParam.Direction == ParameterDirection.Input) || (newParam.Direction == ParameterDirection.InputOutput)) { SetParameterValue(newParam, relatee, propMap.MemberName); } cmd.Parameters.Add(newParam); } return(cmd); } else { //ExceptionPolicy.HandleException( new NullReferenceException("No such command mapping: " + typeMap.FullName + ":" + relationKey), "Exception" ); } return(null); }
private void AssignOutputValuesToObject(SqlCommand cmd, IRelatable relatee, ORMTypeMapping typeMap, string relationKey) { // Logger.Write( MethodBase.GetCurrentMethod() ); ORMCommandMap ocm = typeMap.GetMapByName(relationKey); foreach (object ob in ocm.PropertyMaps) { ORMPropertyMap propMap = (ORMPropertyMap)ob; if ((propMap.DataDirection == ParameterDirection.Output) || (propMap.DataDirection == ParameterDirection.InputOutput)) { PropertyInfo prop; Type t = relatee.GetType(); prop = t.GetProperty(propMap.MemberName); if (prop != null) { if (cmd.Parameters[propMap.Parameter].Value != DBNull.Value) { prop.SetValue(relatee, cmd.Parameters[propMap.Parameter].Value, null); } } else { // ExceptionPolicy.HandleException( new NullReferenceException(("Missing member " + t.FullName + "." + propMap.MemberName)), "Exception" ); } } } }
public override void Relate(IRelatable relatee, string relationKey) { //Logger.Write( MethodBase.GetCurrentMethod() ); ORMTypeMapping typeMap = FetchTypeMap(relatee); ORMCommandMap cmdMap = typeMap.GetMapByName(relationKey); SqlCommand cmd = BuildCommandFromTypeMap(relatee, typeMap, relationKey); //Logger.Write(cmd.ToString()); try { conn.Open(); if (cmdMap.ReturnsMultiple) { SqlDataAdapter da = new SqlDataAdapter(cmd); IRelatableSet relateSet = (IRelatableSet)relatee; da.Fill(relateSet.ResultSet); } else { // Logger.Write(cmd.CommandText); // Logger.Write(cmd.Parameters.ToString()); cmd.ExecuteNonQuery(); } AssignOutputValuesToObject(cmd, relatee, typeMap, relationKey); //conn.Close(); } catch (Exception e) { string b = e.ToString(); MessageBox.Show(relationKey + "\\n" + e.ToString()); } finally { conn.Close(); } }