Ejemplo n.º 1
0
        private void GetData(object criteria)
        {
            var    key   = Getkey(criteria);
            ICache cache = ServiceLocator.Current.GetInstance <ICache>();

            if (cache.Contains(key))
            {
                var objCache = (CacheListBase <TCr>)cache.GetData(key);
                _totalRegistros = objCache.TotalRegistros;
                foreach (var cr in objCache.Data)
                {
                    Items.Add(cr);
                }
            }
            else
            {
                //crear la conexion a la base
                if (Db == null)
                {
                    Db = DatabaseFactory.CreateDatabase();
                }
                Comando = Db.CreateSPCommand(NombreProcedimiento);

                //setear los parametros
                MethodInfo methodInfo = GetType()
                                        .GetMethod(NombreMetodo, BindingFlags.NonPublic | BindingFlags.Instance,
                                                   Type.DefaultBinder, new[] { criteria.GetType() }, null);
                if (methodInfo != null)
                {
                    methodInfo.Invoke(this, new [] { criteria });
                }

                else
                {
                    if (criteria is int)
                    {
                        Db.AddParameterWithValue(Comando, "en_id", DbType.Int32, criteria);
                    }
                    else if (criteria is string)
                    {
                        Db.AddParameterWithValue(Comando, "ec_codigo", DbType.String, criteria);
                    }
                    else
                    {
                        throw new JusException(
                                  String.Format("No se implemento el metodo {0}, Ej: 'private void {0}({1} criteria)'",
                                                NombreMetodo, criteria.GetType()));
                    }
                }
                Db.AddParameter(Comando, "sn_total_registros", DbType.Int32, ParameterDirection.Output);
                Db.AddParameter(Comando, "sq_resultado", DbType.Object, ParameterDirection.Output);
                using (IDataReader dr = Db.ExecuteDataReader(Comando))
                {
                    List <TCr> listaInfo = new List <TCr>();
                    Int32.TryParse(Db.GetOutputParameterValue(Comando, "sn_total_registros").ToString(), out _totalRegistros);
                    while (dr.Read() && listaInfo.Count < 500)
                    {
                        listaInfo.Add(JusReadOnlyBase <TCr> .Get(dr));
                    }
                    foreach (var info in OrdenarList(listaInfo))
                    {
                        Add(info);
                    }
                }
                if (Items.Count > 1)
                {
                    var grupo = RootClass.Aggregate(string.Empty, (current, type) => current + type + ',');
                    cache.AddItem(key, new CacheListBase <TCr> {
                        Data = Items, TotalRegistros = _totalRegistros
                    }, grupo.Trim(','));
                }
            }
        }