public static IOrmModel Create(this IOrmModel me) { KeyValuePair <string, int?> _PrimaryKey = ReflectionHelper.PrimaryKey(me); /*Move donot use reflection unless error*/ //string _message = string.Format("Process {0} failed;", ReflectionHelper.GetProcessName(me)); //_message = string.Format("Process {0} failed; {1}={2}", ReflectionHelper.GetProcessName(me), _PrimaryKey.Key, _PrimaryKey.Value); try { using (SqlConnection cn = new SqlConnection(Connstring())) { cn.Open(); SqlCommand cmd = new SqlCommand(me.OrmContext.Create, cn); cmd.CommandType = CommandType.StoredProcedure; foreach (DataAnnotationsModelMetadata field in ModelMetadataProviders.Current.GetMetadataForProperties(me, me.GetType())) { if ( field.AdditionalValues.Contains(new KeyValuePair <string, object>("ORM", true)) & !field.AdditionalValues.Contains(new KeyValuePair <string, object>("ORM_Update", false)) ) { if (field.AdditionalValues.Contains(new KeyValuePair <string, object>("ORM_PrimaryKey", true))) { cmd.Parameters.Add(new SqlParameter(field.PropertyName, SqlDbType.Int)); cmd.Parameters[field.PropertyName].Direction = ParameterDirection.Output; } else { System.Reflection.PropertyInfo PropInfo = me.GetType().GetProperty(field.PropertyName); object PropVal = PropInfo.GetValue(me, null); cmd.Parameters.AddWithValue(field.PropertyName, PropVal); } } } cmd.ExecuteNonQuery(); ReflectionHelper.SetProperty( me , _PrimaryKey.Key , cmd.Parameters[_PrimaryKey.Key].Value ); cn.Close(); } OrmCaching.ClearCacheForProcessType(me.GetType().FullName); } catch (SqlException ex) { // err.WriteError("SQL Exception: " + _message, GetProcessName(me), ex); throw; } catch (Exception ex) { // err.WriteError(_message, GetProcessName(me), ex); } return(me); }
public static bool Delete(this IOrmModel me, int PrimaryKeyValue) { //Note, this returns true if no row is found matching the key... bubble exception maybe from SQL? KeyValuePair <string, int?> _PrimaryKey = ReflectionHelper.PrimaryKey(me); /*Move donot use reflection unless error*/ string _message = string.Format("Process {0} failed;", ReflectionHelper.GetProcessName(me)); _message = string.Format("Process {0} failed; {1}={2}", ReflectionHelper.GetProcessName(me), _PrimaryKey.Key, _PrimaryKey.Value); try { using (SqlConnection cn = new SqlConnection(Connstring())) { cn.Open(); SqlCommand cmd = new SqlCommand(me.OrmContext.Delete, cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter(_PrimaryKey.Key, SqlDbType.Int)); cmd.Parameters[_PrimaryKey.Key].Direction = ParameterDirection.InputOutput; cmd.Parameters[_PrimaryKey.Key].Value = PrimaryKeyValue; cmd.Parameters.AddWithValue("delete", true); cmd.ExecuteNonQuery(); cn.Close(); } OrmCaching.ClearCacheForProcessType(me.GetType().FullName); return(true); } catch (SqlException ex) { //err.WriteError("SQL Exception: " + _message, GetProcessName(me), ex); throw; } catch (Exception ex) { //err.WriteError(_message, GetProcessName(me), ex); } return(false); }
public static void SetProperty(this IOrmModel me, string Name, object Value) { string _message = string.Format("Set Property {0}={1} Dynamic Operation failed;", Name, Value); object[] SystemValue = { Value }; try { me.GetType().InvokeMember(Name, System.Reflection.BindingFlags.SetProperty, Type.DefaultBinder, me, SystemValue); } catch (Exception ex) { //err.WriteError(_message, GetProcessName(me), ex); } }
static public void AutoMapFromDictionary(this IOrmModel me, Dictionary <string, object> row) { string _message = string.Format("Deserialize of object {0} Failed", me.ToString()); try { foreach (KeyValuePair <string, object> Field in row) { _message = string.Format("Deserialize/Cast of Object '{0}' Failed at Property '{1}' with Value '{2}'", me.ToString(), Field.Key, Field.Value); System.Object[] intVal = { Field.Value }; me.GetType().InvokeMember(Field.Key, System.Reflection.BindingFlags.SetProperty, Type.DefaultBinder, me, intVal); } } catch (Exception ex) { //err.WriteError(_message, ProcessHelper.GetProcessName(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType), ex); } }
public static KeyValuePair <string, int?> PrimaryKey(this IOrmModel me) { string _primaryKeyFieldName = string.Empty; int? Id = 0; try { foreach (System.Web.Mvc.ModelMetadata field in System.Web.Mvc.ModelMetadataProviders.Current.GetMetadataForProperties(me, me.GetType())) { if (field.AdditionalValues.Contains(new KeyValuePair <string, object>("ORM_PrimaryKey", true))) { _primaryKeyFieldName = field.PropertyName; break; // TODO: might not be correct. Was : Exit For } } System.Reflection.PropertyInfo _PrimaryKey = me.GetType().GetProperty(_primaryKeyFieldName); Id = (int?)_PrimaryKey.GetValue(me, null); } catch (Exception ex) { } return(new KeyValuePair <string, int?>(_primaryKeyFieldName, Id)); }
public static object GetProperty(this IOrmModel me, string Name) { System.Reflection.PropertyInfo PropInfo = me.GetType().GetProperty(Name); return(PropInfo.GetValue(me, null)); }
public static string PrimaryKeyName(this IOrmModel me) { try { foreach (System.Web.Mvc.ModelMetadata field in System.Web.Mvc.ModelMetadataProviders.Current.GetMetadataForProperties(me, me.GetType())) { if (field.AdditionalValues.Contains(new KeyValuePair <string, object>("ORM_PrimaryKey", true))) { return(field.PropertyName); } } } catch (Exception ex) { } return(string.Empty); }
public static List <T> Query <T>(this IOrmModel me) where T : IOrmModel, new() { /*Move donot use reflection unless error*/ string _message = string.Format("Process {0} failed;", ReflectionHelper.GetProcessName(me)); List <T> retval = new List <T>(); try { using (SqlConnection cn = new SqlConnection(Connstring())) { cn.Open(); SqlCommand cmd = new SqlCommand(me.OrmContext.Read, cn); cmd.CommandType = CommandType.StoredProcedure; foreach (DataAnnotationsModelMetadata field in ModelMetadataProviders.Current.GetMetadataForProperties(me, me.GetType())) { if (field.AdditionalValues.Contains(new KeyValuePair <string, object>("ORM", true))) { object PropertyVal = ReflectionHelper.GetProperty(me, field.PropertyName); if ((PropertyVal != null)) { cmd.Parameters.AddWithValue(field.PropertyName, PropertyVal); } } } SqlDataReader r = cmd.ExecuteReader(); while (r.Read()) { Dictionary <string, object> row = r.ToDataRow(); IOrmModel item = (IOrmModel) new T(); item.AutoMapFromDictionary(row); retval.Add((T)item); } while (r.NextResult()) { ; //get Error... Foobar.. } cn.Close(); } } catch (SqlException ex) { //err.WriteError("SQL Exception: " + _message, GetProcessName(me), ex); throw; } catch (Exception ex) { //err.WriteError(_message, GetProcessName(me), ex); } return(retval); }