예제 #1
0
        //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));
            }
        }
예제 #2
0
        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));
            }
        }
예제 #3
0
        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));
            }
        }