private DarkTable GetClassAttribute() { DarkTable tableDefinifiton = (DarkTable)Attribute.GetCustomAttribute(typeof(T), typeof(DarkTable)); if (tableDefinifiton == null) { throw new Exceptions.DarkExceptionSystem(string.Format("The attribute was not found in the class '{0}'.", typeof(T).Name)); } return(tableDefinifiton); }
public bool Delete() { DarkTable tableDefinifiton = GetClassAttribute(); if (tableDefinifiton.IsStoreProcedure) { return(ActionsObject(DbManagerTypes.Delete)); } else { return(ActionsObjectCode(DbManagerTypes.Delete, tableDefinifiton)); } }
private string GetRealNameClass() { string Nombre = ""; DarkTable tableDefinifiton = GetClassAttribute(); if (tableDefinifiton.IsMappedByLabels) { Nombre = tableDefinifiton.Name; } else { Nombre = typeof(T).Name; } return(Nombre); }
private List <T> DataReader(string SqlStatements) { DarkTable tableDefinifiton = GetClassAttribute(); System.Data.SqlClient.SqlDataReader Data = dBConnection.GetDataReader(SqlStatements); List <T> Response = new List <T>(); while (Data.Read()) { object exFormAsObj = Activator.CreateInstance(typeof(T)); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = exFormAsObj.GetType().GetProperty(prop.Name); DarkColumn hiddenAttribute = (DarkColumn)propertyInfo.GetCustomAttribute(typeof(DarkColumn)); if (hiddenAttribute == null) { throw new Exceptions.DarkExceptionSystem(string.Format("The attribute was not found in the attribute '{0}', if you don´t want to use mapTable, please set IsMappedByLabels = false", prop.Name)); } if (hiddenAttribute.IsMapped) { string NombrePropiedad = ""; if (tableDefinifiton.IsMappedByLabels) { NombrePropiedad = hiddenAttribute.Name.Trim(); } else { NombrePropiedad = prop.Name; } try { if (prop.PropertyType.Equals(typeof(DateTime))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? DateTime.Now : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.DateTime), null); } if (prop.PropertyType.Equals(typeof(DateTime?))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? null : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, value, null); } if (prop.PropertyType.Equals(typeof(TimeSpan))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? null : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, propertyInfo.PropertyType), null); } if (prop.PropertyType.Equals(typeof(double))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? 0 : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.Double), null); } if (prop.PropertyType.Equals(typeof(float))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? 0 : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ToSingle(value), null); } if (prop.PropertyType.Equals(typeof(Decimal))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? 0 : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.Double), null); } if (prop.PropertyType.Equals(typeof(string))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? "" : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.String), null); } if (prop.PropertyType.Equals(typeof(bool))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? false : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, TypeCode.Boolean), null); } if (prop.PropertyType.Equals(typeof(int))) { var value = Data.GetValue(Data.GetOrdinal(NombrePropiedad)) is System.DBNull ? 0 : Data.GetValue(Data.GetOrdinal(NombrePropiedad)); propertyInfo.SetValue(exFormAsObj, Convert.ChangeType(value, propertyInfo.PropertyType), null); } } catch (Exception ex) { throw new Exceptions.DarkExceptionSystem(string.Format("The attribute'{0}' has an error, {1}", prop.Name, ex.Message)); } } } Response.Add((T)exFormAsObj); } Data.Close(); return(Response); }
private bool ActionsObjectCode(DbManagerTypes dbManagerTypes, DarkTable tableDefinifiton) { //DarkTable tableDefinifiton = GetClassAttribute(); bool result = false; //mapeo de tabla con los nombres de los campos ya existentes string sentencia = ""; string sentenciaVariables = ""; foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = Element.GetType().GetProperty(prop.Name); DarkColumn hiddenAttribute = (DarkColumn)propertyInfo.GetCustomAttribute(typeof(DarkColumn)); if (hiddenAttribute == null) { throw new Exceptions.DarkExceptionSystem(string.Format("The attribute was not found in the attribute '{0}', if you don´t want to use mapTable, please set IsMappedByLabels = false", prop.Name)); } if (tableDefinifiton.IsMappedByLabels) { if (string.IsNullOrEmpty(hiddenAttribute.Name)) { throw new Exceptions.DarkExceptionSystem(string.Format("The attribute {0} was setting like mapping column, the name is missing", prop.Name)); } } else { hiddenAttribute.Name = prop.Name; } if (dbManagerTypes == DbManagerTypes.Add) { if (!hiddenAttribute.IsKey && hiddenAttribute.IsMapped) { sentencia += hiddenAttribute.Name + ","; sentenciaVariables += "@" + hiddenAttribute.Name + ","; } } else if (dbManagerTypes == DbManagerTypes.Update) { if (!hiddenAttribute.IsKey && hiddenAttribute.IsMapped) { sentencia += hiddenAttribute.Name + " = @" + hiddenAttribute.Name + ","; } else if (hiddenAttribute.IsKey && hiddenAttribute.IsMapped) { sentenciaVariables = hiddenAttribute.Name + " = @" + hiddenAttribute.Name + ""; } else { } } else if (dbManagerTypes == DbManagerTypes.Delete) { if (hiddenAttribute.IsKey) { sentenciaVariables = hiddenAttribute.Name + " = @" + hiddenAttribute.Name + ""; } } else { throw new Exceptions.DarkExceptionSystem(string.Format("Delete action is not active")); } } if (dbManagerTypes == DbManagerTypes.Add) { string Statement = string.Format("INSERT INTO {0}({1}) VALUES({2})", Nametable, sentencia.Substring(0, sentencia.Length - 1), sentenciaVariables.Substring(0, sentenciaVariables.Length - 1)); List <ProcedureModel> procedureModels = new List <ProcedureModel>(); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = Element.GetType().GetProperty(prop.Name); DarkColumn hiddenAttribute = (DarkColumn)propertyInfo.GetCustomAttribute(typeof(DarkColumn)); if (!hiddenAttribute.IsKey && hiddenAttribute.IsMapped) { procedureModels.Add(new ProcedureModel { Namefield = tableDefinifiton.IsMappedByLabels ? hiddenAttribute.Name : prop.Name, value = propertyInfo.GetValue(Element) }); } } dBConnection.StartInsert(Statement, procedureModels); result = true; } else if (dbManagerTypes == DbManagerTypes.Update) { string Statement = string.Format("UPDATE {0} SET {1} WHERE {2} ", Nametable, sentencia.Substring(0, sentencia.Length - 1), sentenciaVariables); List <ProcedureModel> procedureModels = new List <ProcedureModel>(); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = Element.GetType().GetProperty(prop.Name); DarkColumn hiddenAttribute = (DarkColumn)propertyInfo.GetCustomAttribute(typeof(DarkColumn)); if (hiddenAttribute.IsMapped) { procedureModels.Add(new ProcedureModel { Namefield = tableDefinifiton.IsMappedByLabels ? hiddenAttribute.Name : prop.Name, value = propertyInfo.GetValue(Element) }); } } dBConnection.StartUpdate(Statement, procedureModels); result = true; } else if (dbManagerTypes == DbManagerTypes.Delete) { string Statement = string.Format("DELETE FROM {0} WHERE {1} ", Nametable, sentenciaVariables); List <ProcedureModel> procedureModels = new List <ProcedureModel>(); foreach (var prop in typeof(T).GetProperties()) { PropertyInfo propertyInfo = Element.GetType().GetProperty(prop.Name); DarkColumn hiddenAttribute = (DarkColumn)propertyInfo.GetCustomAttribute(typeof(DarkColumn)); if (hiddenAttribute.IsMapped && hiddenAttribute.IsKey) { procedureModels.Add(new ProcedureModel { Namefield = tableDefinifiton.IsMappedByLabels ? hiddenAttribute.Name : prop.Name, value = propertyInfo.GetValue(Element) }); } } dBConnection.StartDelete(Statement, procedureModels); result = true; } else { throw new Exceptions.DarkExceptionSystem(string.Format("Delete action is not active")); } return(result); }