Beispiel #1
0
        public List <T> LeerTabla <T>()
        {
            List <T>      lista = new List <T>();
            String        query = "Select * from " + typeof(T).Name;
            SQLiteCommand cmd   = conexion.CreateCommand();

            cmd.CommandText = query;
            SQLiteDataReader lector = cmd.ExecuteReader();

            while (lector.Read())
            {
                if (lector.HasRows && lector.FieldCount > 0)
                {
                    IModeloBase   objeto      = ModeloFactory.darInstancia(typeof(T));
                    List <String> listaCampos = objeto.OrdenCampos();

                    Dictionary <String, String> excepciones = objeto.Excepciones();

                    foreach (String campo in listaCampos)
                    {
                        if (!campo.Trim().Equals(""))
                        {
                            PropertyInfo propiedad = _tipo.GetProperty(campo);
                            Utilidades.PasarValorCampo(excepciones, propiedad, objeto, lector[propiedad.Name].ToString());
                        }
                    }
                    lista.Add((T)objeto);
                }
            }
            return(lista);
        }
Beispiel #2
0
        //CRUD....
        //Create Crear
        //Retreive Obtener
        //Update Modificar
        //Delete Eliminar

        //Reflection ----> Ingenieria Inversa... Analizar o modificar la estructura de un objeto

        //Id: 1
        // Nombre: Fulanito de Tal
        // Estado: Habilitado
        // Insert Usuario(id,nombre,contrasena,estado)values(1,Fulanito de tal,******,Habilitado)


        public static Boolean Crear(IModeloBase fuente)
        {
            if (fuente != null)
            {
                return(ConexionFactory.DarConexion(fuente.GetType()).Crear(fuente));
            }
            return(false);
        }
Beispiel #3
0
 public bool Crear(IModeloBase fuente)
 {
     if (fuente != null)
     {
         List <IModeloBase> lista = LeerTabla();
         lista.Add(fuente);
         EscribirTabla(lista);
         return(true);
     }
     return(false);
 }
Beispiel #4
0
        //Se obtiene un valor de un campo de un objeto enviandole solamente el nombre
        // Si el objeto es Rol... y le queremos obtener el valor del campo nombre
        //ModeloBase.ObtenerCampoValor(rol,"nombre");
        public static Object ObtenerCampoValor(IModeloBase objeto, String campo)
        {
            //ToTitleCase ---> PascalCase
            TextInfo     textInfo  = (new CultureInfo("es-BO", false)).TextInfo;
            PropertyInfo propiedad = objeto.GetType().GetProperty(textInfo.ToTitleCase(campo));

            if (propiedad != null)
            {
                return(propiedad.GetValue(objeto));
            }
            return(null);
        }
Beispiel #5
0
        private void IngresarContenido(IModeloBase objeto, String[] camposValor)
        {
            List <String> listaCampos = objeto.OrdenCampos();
            int           indice      = 0;

            Dictionary <String, String> excepciones = objeto.Excepciones();

            foreach (String campo in listaCampos)
            {
                if (camposValor.Length > indice && !campo.Trim().Equals("") && !camposValor[indice].Trim().Equals(""))
                {
                    PropertyInfo propiedad = _tipo.GetProperty(campo);
                    Utilidades.PasarValorCampo(excepciones, propiedad, objeto, camposValor[indice]);
                }
                indice++;
            }
        }
Beispiel #6
0
 public bool Crear(IModeloBase fuente)
 {
     if (fuente != null)
     {
         PropertyInfo[] propiedades         = _tipo.GetProperties();
         Dictionary <String, String> campos = new Dictionary <string, string>();
         foreach (PropertyInfo propiedad in propiedades)
         {
             campos.Add(propiedad.Name, propiedad.GetValue(fuente).ToString());
         }
         String query = "insert into " + _tipo.Name + " (" + String.Join(",", campos.Keys) + ") values (" + String.Join(",", campos.Values.Select(p => "\"" + p + "\"")) + ");";
         if (ExecuteNonQuery(query))
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #7
0
        private List <T> ConvertirDeTexto <T>(string contenido)
        {
            List <T> lista = new List <T>();

            if (contenido != null && !contenido.Trim().Equals(""))
            {
                String[] lineas = contenido.Split("\n").Select(p => p.Trim()).ToArray();
                foreach (String linea in lineas)
                {
                    if (!linea.Trim().Equals(""))
                    {
                        String[]    camposValor = linea.Split("\t").Select(p => p.Trim()).ToArray();
                        IModeloBase objeto      = ModeloFactory.darInstancia(typeof(T));
                        IngresarContenido(objeto, camposValor);
                        lista.Add((T)objeto);
                    }
                }
            }

            return(lista);
        }
Beispiel #8
0
        //CRUD....
        //Create Crear
        //Retreive Obtener
        //Update Modificar
        //Delete Eliminar

        //Reflection ----> Ingenieria Inversa... Analizar o modificar la estructura de un objeto

        //Id: 1
        // Nombre: Fulanito de Tal
        // Estado: Habilitado
        // Insert Usuario(id,nombre,contrasena,estado)values(1,Fulanito de tal,******,Habilitado)


        public static Boolean Crear(IModeloBase fuente)
        {
            if (fuente != null)
            {
                IConexion conexion          = ConexionFactory.DarConexion(fuente.GetType());
                String    llave             = fuente.darLlave();
                Boolean   esAutoIncremental = fuente.llaveEsAutoIncremental();

                if (llave != null && !llave.Equals("") && esAutoIncremental)
                {
                    PropertyInfo propiedad = fuente.GetType().GetProperty(llave);
                    if (propiedad != null && (propiedad.GetValue(fuente) == null || (propiedad.GetValue(fuente) != null && propiedad.PropertyType.Name.Equals("Int32") && propiedad.GetValue(fuente).Equals(0))))
                    {
                        int max = Int32.Parse(conexion.LeerTabla().Max(p => propiedad.GetValue(p)).ToString());
                        propiedad.SetValue(fuente, max + 1);
                    }
                }

                return(conexion.Crear(fuente));
            }
            return(false);
        }
Beispiel #9
0
        public static void PasarValorCampo(Dictionary <String, String> excepciones, PropertyInfo propiedad, IModeloBase objeto, String valor)
        {
            if (propiedad != null && objeto != null)
            {
                Type _tipo = objeto.GetType();
                if (excepciones.ContainsKey(propiedad.Name))
                {
                    MethodInfo metodo = _tipo.GetMethod(excepciones[propiedad.Name]);
                    if (metodo != null)
                    {
                        switch (propiedad.PropertyType.Name)
                        {
                        case "Int32":
                            metodo.Invoke(objeto, new object[] { Int32.Parse(valor) });
                            break;

                        case "Boolean":
                            metodo.Invoke(objeto, new object[] { Boolean.Parse(valor) });
                            break;

                        default:
                            metodo.Invoke(objeto, new object[] { valor });
                            break;
                        }
                    }
                }
                else
                {
                    switch (propiedad.PropertyType.Name)
                    {
                    case "Int32":
                        propiedad.SetValue(objeto, Int32.Parse(valor));
                        break;

                    case "Boolean":
                        propiedad.SetValue(objeto, Boolean.Parse(valor));
                        break;

                    default:
                        propiedad.SetValue(objeto, valor);
                        break;
                    }
                }
            }
        }