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 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); }
//as a general rule of thumb, consider the parallel library as being faster on //recordsets of greater then 100 rows, don't use this for small datasets. public static List <T> GetInParallel <T>(this IOrmModel me) where T : IOrmModel, new() { string ProcessName = ReflectionHelper.GetProcessName(me); string _message = string.Format("Process {0} failed;", ProcessName); string _cacheName = ProcessName; ConcurrentQueue <T> retval = new ConcurrentQueue <T>(); object sync = new object(); if (!me.CacheStatus(_cacheName)) { try { using (SqlConnection cn = new SqlConnection(Connstring())) { cn.Open(); SqlCommand cmd = new SqlCommand(me.OrmContext.Read, cn); cmd.CommandType = CommandType.StoredProcedure; SqlDataReader r = cmd.ExecuteReader(); Parallel.ForEach(r.Cast <System.Data.Common.DbDataRecord>(), row => { IOrmModel item = (IOrmModel) new T(); item.AutoMapFromDictionary(row.ToDataRow()); lock (sync) { retval.Enqueue((T)item); } }); while (r.NextResult()) { ; //get Error from SQL... Foobar.. } cn.Close(); } if (retval.Count > 0) { OrmCaching.SetCache(_cacheName, retval.ToList <T>()); } } catch (SqlException ex) { // err.WriteError("SQL Exception: " + _message, GetProcessName(me), ex); throw; } catch (Exception ex) { // err.WriteError(_message, GetProcessName(me), ex); } return(retval.ToList <T>()); } else { return((List <T>)OrmCaching.GetCache(_cacheName)); } }
public static List <T> GetCollection <T>(this IOrmModel me) where T : IOrmModel, new() { string _message = string.Format("Process {0} failed;", ReflectionHelper.GetProcessName(me)); string _cacheName = ReflectionHelper.GetProcessName(me); List <T> retval = new List <T>(); object sync = new object(); if (!me.CacheStatus(_cacheName)) { try { using (SqlConnection cn = new SqlConnection(Connstring())) { cn.Open(); SqlCommand cmd = new SqlCommand(me.OrmContext.Read, cn); cmd.CommandType = CommandType.StoredProcedure; 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(); } if (retval.Count > 0) { OrmCaching.SetCache(_cacheName, me); } } catch (SqlException ex) { // err.WriteError("SQL Exception: " + _message, GetProcessName(me), ex); throw; } catch (Exception ex) { // err.WriteError(_message, GetProcessName(me), ex); } return(retval); } else { return((List <T>)OrmCaching.GetCache(_cacheName)); } }
public static T Get <T> (this IOrmModel me, int id) where T : IOrmModel, new() { KeyValuePair <string, int?> _PrimaryKey = ReflectionHelper.PrimaryKey(me); string ProcessName = ReflectionHelper.GetProcessName(me); string _message = string.Format("Process {0} failed;", ProcessName); _message = string.Format("Process {0} failed; {1}={2}", ProcessName, _PrimaryKey.Key, _PrimaryKey.Value); string _cacheName = string.Format("{0}/{1}", ProcessName, _PrimaryKey.Value); if (!me.CacheStatus(_cacheName)) { try { using (SqlConnection cn = new SqlConnection(Connstring())) { cn.Open(); SqlCommand cmd = new SqlCommand(me.OrmContext.Read, cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(_PrimaryKey.Key, id); SqlDataReader r = cmd.ExecuteReader(); while (r.Read()) { Dictionary <string, object> row = r.ToDataRow(); IOrmModel item = (IOrmModel) new T(); item.AutoMapFromDictionary(row); OrmCaching.SetCache(_cacheName, item); return((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((T)me); } else { return((T)OrmCaching.GetCache(_cacheName)); } }