private MySqlCommand PrepeareCommand(string procedureName) { Database db = new Database(); MySqlCommand command = null; using (MySqlConnection con = db.OpenConnection()) { command = new MySqlCommand(procedureName, con); command.CommandType = System.Data.CommandType.StoredProcedure; MySqlCommandBuilder.DeriveParameters(command); } Type objectType = typeof(T); PropertyInfo[] objectProps = objectType.GetProperties(); if (objectProps == null) { return(null); } foreach (PropertyInfo prop in objectProps) { object[] fieldAttribs = prop.GetCustomAttributes(typeof(ObjectPropertyAttribute), true); if (fieldAttribs == null || fieldAttribs.Length == 0) { continue; } ObjectPropertyAttribute fieldAttrib = (ObjectPropertyAttribute)fieldAttribs[0]; if (command.Parameters.Contains("@" + fieldAttrib.ParameterName)) { command.Parameters["@" + fieldAttrib.ParameterName].Value = prop.GetValue(this, null); } } return(command); }
public virtual void Save() { Type objectType = typeof(T); object[] objectBindingattribs = objectType.GetCustomAttributes(typeof(ObjectBindingAttribute), true); if (objectBindingattribs == null) { return; } ObjectBindingAttribute objectBindingAttrib = (ObjectBindingAttribute)objectBindingattribs[0]; try { if (this.State == ObjectState.Existing) { MySqlCommand updateCommand = PrepeareCommand(objectBindingAttrib.UpdateStoredProcedure); //if (updateCommand.Parameters.Contains("@pVrijemeModifikacije")) // updateCommand.Parameters["@pVrijemeModifikacije"].Value = DateTime.Now; updateCommand.Connection.Open(); updateCommand.ExecuteNonQuery(); if ((int)updateCommand.Parameters["@pError"].Value == -1) { throw new SaveFailedException(String.Format("Someone has changed data already.")); } if (updateCommand.Parameters.Contains("@pVrijemeModifikacije")) { this.VrijemeModificiranja = DateTime.Parse(updateCommand.Parameters["@pVrijemeModifikacije"].Value.ToString()); } updateCommand.Connection.Close(); } else if (this.State == ObjectState.New) { MySqlCommand insertCommand = PrepeareCommand(objectBindingAttrib.InsertStoredProcedure); insertCommand.Connection.Open(); insertCommand.ExecuteNonQuery(); insertCommand.Connection.Close(); PropertyInfo prop = objectType.GetProperty("ID"); object[] fieldAttribs = prop.GetCustomAttributes(typeof(ObjectPropertyAttribute), true); if (fieldAttribs == null || fieldAttribs.Length == 0) { throw new ApplicationException("Field binding attribute on " + prop.Name + " is missing."); } ObjectPropertyAttribute fieldAttrib = (ObjectPropertyAttribute)fieldAttribs[0]; _id = (int)insertCommand.Parameters["@" + fieldAttrib.ParameterName].Value; if (insertCommand.Parameters.Contains("@pVrijemeKreiranja")) { this.VrijemeKreiranja = DateTime.Parse(insertCommand.Parameters["@pVrijemeKreiranja"].Value.ToString()); } this.State = ObjectState.Existing; } } catch (System.Exception ex) { Logging.Log.Create("Error in the ObjectBase Save method.", Logging.LogEntryLevel.Critical, ex); throw; //throw new SaveFailedException(String.Format("Error while trying to save data for object:{0} of state:{1}", // objectType.Name, this.State.ToString()), ex); } }
public static T CreateObject <T>(MySql.Data.MySqlClient.MySqlDataReader dr) { T newObject = default(T); if (dr == null) { return(newObject); } try { newObject = Activator.CreateInstance <T>(); Type newObjectType = typeof(T); PropertyInfo[] newObjectProperties = newObjectType.GetProperties(); foreach (PropertyInfo property in newObjectProperties) { if (property.Name == "State") { property.SetValue(newObject, ObjectState.Existing, null); } object[] attribs = property.GetCustomAttributes(typeof(ObjectPropertyAttribute), true); if (attribs == null || attribs.Length != 1) { continue; } ObjectPropertyAttribute attrib = attribs[0] as ObjectPropertyAttribute; if (attrib == null) { continue; } try { if (property.CanWrite && !dr.IsDBNull(dr.GetOrdinal(attrib.FieldName))) { if (typeof(System.Char) == property.PropertyType) { Char ch = ' '; if (!String.IsNullOrEmpty(dr[attrib.FieldName].ToString())) { ch = Char.Parse(dr[attrib.FieldName].ToString().Substring(0, 1)); } property.SetValue(newObject, ch, null); } else { property.SetValue(newObject, dr[attrib.FieldName], null); } } } catch (System.Exception ex) { Logging.Log.Create(String.Format("Nemoguce je setovati {0} polje na objektu {1}", attrib.FieldName, newObjectType.Name), Logging.LogEntryLevel.Critical, ex); } } } catch (System.Exception ex) { Logging.Log.Create("Greska unutar Factory.CreateObject methode.", Logging.LogEntryLevel.Critical, ex); } return(newObject); }