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 ORMCommandMap GetMapByName(string relationKey) { foreach (object ob in commandMappings) { ORMCommandMap ocm = (ORMCommandMap)ob; if (ocm.RelationKey.ToUpper() == relationKey.ToUpper()) { return(ocm); } } return(null); }
public ORMTypeMapping(XmlNode typeMap) { commandMappings = new ArrayList(); fullName = typeMap.Attributes["fullname"].Value; // build command mappings XmlNodeList commandList = typeMap.SelectNodes("commandmap"); foreach (XmlNode commandMap in commandList) { ORMCommandMap cmdMap = new ORMCommandMap(commandMap); commandMappings.Add(cmdMap); } }
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(); } }