Exemplo n.º 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));
            }
        }
Exemplo n.º 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));
            }
        }
Exemplo n.º 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));
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        //public static List<T> Query<T>(this IOrmModel me, params IEnumerable<dynamic> Constraints) where T : IOrmModel, new() {
        public static List <T> Query <T>(this IOrmModel me, params Parameter[] Constraints) 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 (Parameter Constraint in Constraints)
                    {
                        cmd.Parameters.AddWithValue(Constraint.Name, Constraint.Value);
                    }
                    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);
        }