//public static object GetListObjectByKey(FieldInfo fieldInfo, object primeryKey) //{ // StringBuilder cmdListObject = new StringBuilder(); // string tableName = string.Empty; // List<FieldInfo> regularMapedFields = new List<FieldInfo>(); // string whereClause = string.Empty; // string orderbyClause = string.Empty; // string fromClouse = string.Empty; // if (!fieldInfo.FieldType.IsGenericType || fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(IList)) // throw new Exception("This is not generic list"); // if (Attribute.IsDefined(fieldInfo, typeof(ListMapAttribute))) // { // ListMapAttribute listMapAttribute = (ListMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(ListMapAttribute)); // if (!string.IsNullOrEmpty(listMapAttribute.WhereClause)) // whereClause = string.Format("WHERE {0} ", listMapAttribute.WhereClause); // if (!string.IsNullOrEmpty(listMapAttribute.OrderBy)) // orderbyClause = string.Format("ORDER BY {0} ", listMapAttribute.OrderBy); // tableName = listMapAttribute.TableName; // } // else // throw new Exception("listmapatrribute not defined"); // Type typeElement = fieldInfo.FieldType.GetGenericArguments()[0]; // foreach (FieldInfo f in typeElement.GetFields()) // // { // if (Attribute.IsDefined(f, typeof(FieldMapAttribute))) //Check if the field is a table field if it is use it on the query // { // regularMapedFields.Add(f); // } // } // cmdListObject.AppendLine("SELECT "); // cmdListObject.AppendLine(CreateSelectList(regularMapedFields)); // //Add the from Clause // cmdListObject.AppendLine(string.Format(" FROM {0} ", tableName)); // //Add the where clause // if (!string.IsNullOrEmpty(whereClause)) // cmdListObject.AppendLine(whereClause); // if (!string.IsNullOrEmpty(orderbyClause)) // cmdListObject.Append(orderbyClause); // IList returnObject = (IList)Activator.CreateInstance(fieldInfo.FieldType); // using (DataManager.Current.OpenConnection()) // { // using (SqlCommand sqlCommand = DataManager.CreateCommand(cmdListObject.ToString())) // { // //sqlcommand parameters // sqlCommand.Parameters["@PrimeryKey"].Value = primeryKey; // using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader()) // { // while (sqlDataReader.Read()) // { // object currentItem = Activator.CreateInstance(typeElement); // foreach (FieldInfo f in typeElement.GetFields()) // { // if (Attribute.IsDefined(f, typeof(FieldMapAttribute))) // { // FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(f, typeof(FieldMapAttribute)); // object val = sqlDataReader[fieldMapAttribute.FieldName]; // if (val is DBNull) // { // f.SetValue(currentItem, null); // } // else // f.SetValue(currentItem, val); // } // } // returnObject.Add(currentItem); // } // } // } // } // return returnObject; //} public static IList GetListObject(FieldInfo fieldInfo, IDataReader sqlDataReader) { if (!fieldInfo.FieldType.IsGenericType || fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(IList)) { throw new Exception("This is not generic list"); } Type typeElement = fieldInfo.FieldType.GetGenericArguments()[0]; IList returnObject = (IList)Activator.CreateInstance(fieldInfo.GetType()); //Get the inner type while (sqlDataReader.Read()) { object currentItem = Activator.CreateInstance(typeElement); foreach (FieldInfo f in typeElement.GetFields()) { if (Attribute.IsDefined(f, typeof(FieldMapAttribute))) { FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(f, typeof(FieldMapAttribute)); object val = sqlDataReader[fieldMapAttribute.FieldName]; if (val is DBNull) { f.SetValue(currentItem, null); } else { f.SetValue(currentItem, val); } } } returnObject.Add(currentItem); } return(returnObject); }
public static object SaveOrRemoveSimpleObject <T>(string Command, CommandType commandType, SqlOperation sqlOperation, object objectToInsert, SqlConnection sqlConnection, SqlTransaction sqlTransaction) where T : class, new() { object rowsAfectedOrIdentity = 0; Type t = typeof(T); if (sqlConnection.State != ConnectionState.Open) { sqlConnection.Open(); } using (SqlCommand sqlCommand = DataManager.CreateCommand(Command, commandType)) { sqlCommand.Connection = sqlConnection; if (sqlTransaction != null) { sqlCommand.Transaction = sqlTransaction; } if (sqlCommand.Parameters.Contains("@Action")) { sqlCommand.Parameters["@Action"].Value = sqlOperation; } if (objectToInsert != null) { foreach (FieldInfo fieldInfo in objectToInsert.GetType().GetFields()) { if (Attribute.IsDefined(fieldInfo, typeof(FieldMapAttribute))) { FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(FieldMapAttribute)); if (sqlCommand.Parameters.Contains(string.Format("@{0}", fieldMapAttribute.FieldName))) { if (fieldInfo.GetValue(objectToInsert) != null) { sqlCommand.Parameters[string.Format("@{0}", fieldMapAttribute.FieldName)].Value = fieldInfo.GetValue(objectToInsert); } else { sqlCommand.Parameters[string.Format("@{0}", fieldMapAttribute.FieldName)].Value = DBNull.Value; } } } } } rowsAfectedOrIdentity = sqlCommand.ExecuteScalar(); } return(rowsAfectedOrIdentity); }
private static object CustomApply(FieldInfo info, IDataRecord reader) { SettingsCollection settings = null; FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(info, typeof(FieldMapAttribute)); try { if (reader != null) { settings = new SettingsCollection(reader[fieldMapAttribute.FieldName].ToString()); } } catch (Exception) { throw; } return(settings.ToDictionary()); }
/// <summary> /// Return SingleObject /// </summary> /// <typeparam name="ThingT"></typeparam> /// <param name="Key"></param> /// <param name="onApplyValue"></param> /// <returns></returns> public static ThingT CreateMainObject <ThingT>(IDataReader sqlDataReader, Func <FieldInfo, IDataRecord, object> onApplyValue) where ThingT : class, new() { Type t = typeof(ThingT); ThingT returnObject = (ThingT)Activator.CreateInstance(t); foreach (FieldInfo fieldInfo in t.GetFields()) { object val; if (Attribute.IsDefined(fieldInfo, typeof(FieldMapAttribute))) { FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(FieldMapAttribute)); if (fieldMapAttribute.Show) { if (sqlDataReader.FieldExists(fieldMapAttribute.FieldName)) { if (fieldMapAttribute.UseApplyFunction && onApplyValue != null) { val = onApplyValue(fieldInfo, sqlDataReader); } else { val = sqlDataReader[fieldMapAttribute.FieldName]; } if (val is DBNull) { fieldInfo.SetValue(returnObject, null); } else { fieldInfo.SetValue(returnObject, val); } } } } } return(returnObject); }
public static IList GetMenus <T>(object Key, bool recursive, Func <FieldInfo, IDataRecord, object> onApplyValue) where T : class, new() { StringBuilder cmdMainObject = new StringBuilder(); string tableName = string.Empty; string KeyFieldName = string.Empty; string KeyFieldType = string.Empty; Type requestedType = typeof(T); IList returnObject; Type typeElement; //init if (requestedType.IsGenericType) { returnObject = (IList)Activator.CreateInstance(requestedType); typeElement = returnObject.GetType().GetGenericArguments()[0]; if (Attribute.IsDefined(typeElement, typeof(TableMapAttribute))) { TableMapAttribute tableMapAttribute = (TableMapAttribute)Attribute.GetCustomAttribute(typeElement, typeof(TableMapAttribute)); tableName = tableMapAttribute.TableName; //Map the table name for the query } else { throw new InvalidOperationException("Class must have TableMap attribute defined"); } } else { throw new Exception("This is not generic type"); } //Create query //Table List <FieldInfo> regularMapedFields = new List <FieldInfo>(); foreach (FieldInfo fieldInfo in typeElement.GetFields()) { if (Attribute.IsDefined(fieldInfo, typeof(FieldMapAttribute))) //Check if the field is a table field if it is use it on the query { FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(FieldMapAttribute)); regularMapedFields.Add(fieldInfo); if (fieldMapAttribute.IsKey) { KeyFieldName = fieldMapAttribute.FieldName; KeyFieldType = Easynet.Edge.Core.Utilities.TypeConvertor.ToSqlDbType(fieldInfo.FieldType).ToString(); } } } cmdMainObject.AppendLine("SELECT "); cmdMainObject.Append(CreateSelectList(regularMapedFields)); cmdMainObject.AppendLine(string.Format(" FROM {0} ", tableName)); //Add the where clause if (Key == null) { cmdMainObject.AppendLine(string.Format(" WHERE {0} is null", KeyFieldName)); } else { cmdMainObject.AppendLine(string.Format(" WHERE {0}=@PrimeryKey:{1} ", KeyFieldName, KeyFieldType)); } using (DataManager.Current.OpenConnection()) { using (SqlCommand sqlCommand = DataManager.CreateCommand(cmdMainObject.ToString())) { if (Key != null) { sqlCommand.Parameters["@PrimeryKey"].Value = Key; } using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader()) { while (sqlDataReader.Read()) { object currentItem = Activator.CreateInstance(typeElement); foreach (FieldInfo fieldInfo in typeElement.GetFields()) { if (Attribute.IsDefined(fieldInfo, typeof(FieldMapAttribute))) { FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(FieldMapAttribute)); object val; if (fieldMapAttribute.UseApplyFunction && onApplyValue != null) { val = onApplyValue(fieldInfo, sqlDataReader); } else { val = sqlDataReader[fieldMapAttribute.FieldName]; } if (val is DBNull) { fieldInfo.SetValue(currentItem, null); } else { fieldInfo.SetValue(currentItem, val); } } } returnObject.Add(currentItem); } } } } if (recursive) { object recursiveLookupValue = null; for (int i = 0; i < returnObject.Count; i++) { object currentItem = Activator.CreateInstance(returnObject[i].GetType()); Type currentType = currentItem.GetType(); FieldInfo childsField = null; currentItem = returnObject[i]; foreach (FieldInfo fieldInfo in currentType.GetFields()) { if (Attribute.IsDefined(fieldInfo, typeof(FieldMapAttribute))) { FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(FieldMapAttribute)); if (fieldMapAttribute.RecursiveLookupField) { recursiveLookupValue = fieldInfo.GetValue(currentItem); } } if (Attribute.IsDefined(fieldInfo, typeof(HasChildOfTheSameAttribute))) { childsField = fieldInfo; } } if (recursiveLookupValue != null) { if (childsField != null) { childsField.SetValue(currentItem, GetMenus <T>(recursiveLookupValue, recursive, onApplyValue)); } } } } return(returnObject); }