Пример #1
0
 public void Agregar(string key, object valor)
 {
     Condicion.ValidarParametro(key).IsNotNullOrWhiteSpace("La llave no puede ser nula o vacia.");
     Condicion.ValidarParametro(valor).IsNotNull("El valor no puede ser nulo.");
     var politica = new CacheItemPolicy();
     _objectCache.Add(key, valor, politica);
 }
Пример #2
0
 public void Agregar(string key, object valor, TimeSpan timeout)
 {
     Condicion.ValidarParametro(key).IsNotNullOrWhiteSpace("La llave no puede ser nula o vacia.");
     Condicion.ValidarParametro(valor).IsNotNull("El valor no puede ser nulo.");
     
     _objectCache.Add(key, valor, DateTime.Now.AddMinutes(timeout.Seconds));
 }
Пример #3
0
        protected void ValidarClave(string key)
        {
            int longitudClave = LongitudClave();

            Condicion.ValidarParametro(key)
            .IsNotNullOrWhiteSpace(Condicion.MensajeValorNulo)
            .Evaluate(x => x.Length >= longitudClave, string.Format("La longitud de la clave es incorrecta. Debe ser una clave de {0} digitos.", longitudClave));
        }
Пример #4
0
        public bool Remover(string key)
        {
            Condicion.ValidarParametro(key).IsNotNullOrWhiteSpace("La llave no puede ser nula o vacia.");

            if (_objectCache.Contains(key))
            {
                _objectCache.Remove(key);
                return true;
            }
            return false;
        }
Пример #5
0
 public object this[string key]
 {
     get
     {
         Condicion.ValidarParametro(key).IsNotNullOrWhiteSpace("La llave no puede ser nula o vacia.");
         return _objectCache[key];
     }
     set
     {
         Condicion.ValidarParametro(key).IsNotNullOrWhiteSpace("La llave no puede ser nula o vacia.");
         _objectCache[key] = value;
     }
 }
Пример #6
0
        /// <summary>
        /// Genera el valor hash de un texto
        /// </summary>
        /// <param name="password">texto</param>
        /// <param name="encryptionPrivateKey">llave privada de tamaño 16</param>
        /// <returns>Valor hash</returns>
        public static string GenerarHash(string password, string encryptionPrivateKey)
        {
            Condicion.ValidarParametro(encryptionPrivateKey)
            .IsNotNullOrWhiteSpace(Condicion.MensajeValorNulo)
            .Evaluate(x => x.Length >= 16, "La longitud de la clave es incorrecta. Debe ser una calve de 16 digitos.");

            byte[] secretBytes = Encoding.ASCII.GetBytes(encryptionPrivateKey);
            using (var hmac = new HMACSHA256(secretBytes))
            {
                byte[] dataBytes    = Encoding.ASCII.GetBytes(password);
                byte[] computedHash = hmac.ComputeHash(dataBytes);

                return(Convert.ToBase64String(computedHash));
            }
        }
Пример #7
0
 public object Obtener(string key)
 {
     Condicion.ValidarParametro(key).IsNotNullOrWhiteSpace("La llave no puede ser nula o vacia.");
     return _objectCache.Get(key);
 }
Пример #8
0
        public string GetDataErrorValidacion(int value)
        {
            Condicion.ValidarParametro(value).IsGreaterThan(10000);

            return(value.ToString());
        }
Пример #9
0
        static void Main(string[] args)
        {
            //1. Referenciar el ensamblado Framework
            //2. Referenciar el ensamblado CuttingEdge.Conditions
            //3. Importar el namespace Framework.Error e Framework.Validacion
            //4. Importar el namespace CuttingEdge.Conditions

            //VALIDACION DE PARAMETROS
            //Ante cualquier validacion que no cumpla con lo requerido
            //se lanzara una excepcion del tipo ExcepcionValidacion

            //Validacion simple
            //El parametro no debe ser nulo
            string valor = "Hola";

            Condicion.ValidarParametro(valor)
            .IsNotNullOrEmpty("El param no puede ser nulo.");

            //Validacion compuesta
            //El parametro no debe ser nulo
            //EL parametro debe tener una longitud de 8 caracteres
            //El parametro debe empezar con la letra H
            //El parametro debe contener las letras "ol"
            Condicion.ValidarParametro(valor)
            .IsNotNullOrEmpty("El param no puede ser nulo.")
            .HasLength(4, "La longitud del parametro no es correcta.")
            .StartsWith("H", "El valor del parametro debe empezar con la letra H.")
            .Contains("ol", "EL valor del parametro debe contener la cadena ol.");

            //Validacion personalizada
            Condicion.ValidarParametro(valor)
            .Evaluate(x =>
                      x.ToUpper().Equals("HOLA"), "El valor del parametro no es correcto.");

            //Arroja una excepcion del ExcepcionValidacion
            try
            {
                //Se valida que el parametro debe ser null
                Condicion.ValidarParametro(valor)
                .IsNullOrEmpty("El param no puede ser nulo.");
            }
            catch (ExcepcionValidacion ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine();
            Console.WriteLine();



            Cliente c = null;

            var param = new object[] { 33, "", null, new Cliente(), c, 3 };

            foreach (var o in param)
            {
                Condicion.ValidarNotacion(o);

                if (o != null)
                {
                    if (o.GetType().IsClass)
                    {
                        Console.WriteLine(o.GetType());

                        try
                        {
                            Condicion.ValidarNotacion(o);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }


            var cliente = new Cliente();

            cliente.Email = "ddd";
            try
            {
                //Validador de entidades que tengan propiedades
                //marcadas con atributos DataAnnotation
                Condicion.ValidarNotacion(cliente);
            }
            catch (ExcepcionValidacion ex)
            {
                Console.WriteLine(ex.Message);
            }

            //VALIDACION DE REGLA DE NEGOCIO
            //Aplica la misma funcionalidad expuesta en los metodos de arriba
            //solo cambia la cantidad de parametros de entrada

            try
            {
                valor = null;
                Condicion.ValidarRegla(valor, "RN001").IsNotNullOrEmpty(ReglasNegocio.RN00001);
            }
            catch (ExcepcionReglaNegocio ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }
Пример #10
0
        public string Export <T>(List <T> lista, List <Formato> listaFormato) where T : class, new()
        {
            Condicion.ValidarParametro(lista).IsNotNull("El parametro lista no puede ser null.").IsNotEmpty("El parametro lista debe contener elementos.");
            Condicion.ValidarParametro(listaFormato).IsNotNull("El parametro listaFormato no puede ser null").IsNotEmpty("El parametro listaFormato debe contener elementos");

            DataTable pDataTable = DataTableConvert <T> .ToDataTable(lista);

            pDataTable = ConfigurarDatos(pDataTable, listaFormato);
            var    excelDoc      = new StringBuilder();
            string startExcelXml = string.Format(FormatoExcelXml, ExcelXmlStyle(pDataTable, listaFormato));

            int rowCount   = 0;
            int sheetCount = 1;

            excelDoc.AppendLine(startExcelXml);
            excelDoc.AppendLine("<Worksheet ss:Name=\"Lista" + sheetCount + "\">");
            excelDoc.AppendLine("<Table>");

            for (int x = 0; x < pDataTable.Columns.Count; x++)
            {
                int lWidth = listaFormato[x].Ancho > 0 ? listaFormato[x].Ancho : 100;
                excelDoc.AppendLine("<Column ss:AutoFitWidth=\"0\"  ss:Width=\"" + lWidth + "\"/>");
            }
            excelDoc.AppendLine("<Row>");
            for (int x = 0; x < pDataTable.Columns.Count; x++)
            {
                excelDoc.Append("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");
                excelDoc.Append(pDataTable.Columns[x].Caption);
                excelDoc.AppendLine("</Data></Cell>");
            }
            excelDoc.AppendLine("</Row>");
            foreach (DataRow x in pDataTable.Rows)
            {
                rowCount++;
                //if the number of rows is > 64000 create a new page to continue output
                if (rowCount == 64000)
                {
                    rowCount = 0;
                    sheetCount++;
                    excelDoc.AppendLine("</Table>");
                    excelDoc.AppendLine("</Worksheet>");
                    excelDoc.AppendLine("<Worksheet ss:Name=\"Lista" + sheetCount + "\">");
                    excelDoc.AppendLine("<Table>");
                }
                excelDoc.AppendLine("<Row>"); //ID=" + rowCount + "
                for (int y = 0; y < pDataTable.Columns.Count; y++)
                {
                    Type rowType = x[y].GetType();
                    excelDoc.Append("<Cell ss:StyleID=\"s" + y + "0\">");
                    switch (rowType.ToString())
                    {
                    case "System.String":
                        string xmlString = x[y].ToString();
                        xmlString = xmlString.Trim();
                        xmlString = xmlString.Replace("&", "&");
                        xmlString = xmlString.Replace(">", ">");
                        xmlString = xmlString.Replace("<", "<");
                        xmlString = ReplaceCadena(xmlString);
                        excelDoc.Append("<Data ss:Type=\"String\">");
                        excelDoc.Append(xmlString);
                        break;

                    case "System.DateTime":
                        //Excel has a specific Date Format of YYYY-MM-DD followed by
                        //the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000
                        //The Following Code puts the date stored in XMLDate
                        //to the format above
                        DateTime xmlDate         = (DateTime)x[y];
                        string   xmlDatetoString = xmlDate.Year +
                                                   "-" +
                                                   (xmlDate.Month < 10 ? "0" +
                                                    xmlDate.Month : xmlDate.Month.ToString()) +
                                                   "-" +
                                                   (xmlDate.Day < 10 ? "0" +
                                                    xmlDate.Day : xmlDate.Day.ToString()) +
                                                   "T" +
                                                   (xmlDate.Hour < 10 ? "0" +
                                                    xmlDate.Hour : xmlDate.Hour.ToString()) +
                                                   ":" +
                                                   (xmlDate.Minute < 10 ? "0" +
                                                    xmlDate.Minute : xmlDate.Minute.ToString()) +
                                                   ":" +
                                                   (xmlDate.Second < 10 ? "0" +
                                                    xmlDate.Second : xmlDate.Second.ToString()) +
                                                   ".000";
                        excelDoc.Append("<Data ss:Type=\"DateTime\">");
                        excelDoc.Append(xmlDatetoString);
                        break;

                    case "System.Boolean":
                        excelDoc.Append("<Data ss:Type=\"String\">");
                        excelDoc.Append(x[y]);
                        break;

                    case "System.Int16":
                    case "System.Int32":
                    case "System.Int64":
                    case "System.Byte":
                        excelDoc.Append("<Data ss:Type=\"Number\">");
                        excelDoc.Append(x[y]);
                        break;

                    case "System.Decimal":
                    case "System.Double":
                        excelDoc.Append("<Data ss:Type=\"Number\">");
                        excelDoc.Append(x[y]);
                        break;

                    case "System.DBNull":
                        excelDoc.Append("<Data ss:Type=\"String\">");
                        excelDoc.Append("");
                        break;

                    default:
                        throw (new Exception(rowType + " not handled."));
                    }
                    excelDoc.Append("</Data>");

                    excelDoc.AppendLine("</Cell>");
                }
                excelDoc.AppendLine("</Row>");
            }
            excelDoc.AppendLine("</Table>");
            excelDoc.AppendLine(" </Worksheet>");
            excelDoc.AppendLine(EndExcelXml);

            return(excelDoc.ToString());
        }