public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List<string> listOfFields) { var listOfFieldsToWorkWith = new List<string>(listOfFields); if (!listOfFieldsToWorkWith.Any()) return expenseGroup; //does it include any expense-related field? List<string> listOfExpenseFields = listOfFieldsToWorkWith.Where(f => f.Contains("expenses")).ToList(); //if one of those fields is "expenses", we need to ensure FULL expense is returned. If //it's only subfields, only those subfields have to be returned. bool returnPartialExpense = listOfExpenseFields.Any() && !listOfExpenseFields.Contains("expenses"); if (returnPartialExpense) { //remove all expense-related fields from the list of fields, //as we will use the CreateDataShapeObject function in the ExpenseFactory for that listOfFieldsToWorkWith.RemoveRange(listOfExpenseFields); listOfExpenseFields = listOfExpenseFields.Select(f => f.Substring(f.IndexOf(".") + 1)).ToList(); } else { //we shouldnt return a partial expense, but the consumer might still have //asked for a subfield together with the main field, ie., expense, expense.id. We //need to remove those subfields in that case. listOfExpenseFields.Remove("expenses"); listOfFieldsToWorkWith.RemoveRange(listOfExpenseFields); } //create a new ExpandoObject & dynamically create the properties for this object //if we have an expense ExpandoObject objectToReturn = new ExpandoObject(); foreach (var field in listOfFieldsToWorkWith) { //need to include public and instance, b/c specifying a binding flag overwrites the //already-existing binding flags. var fieldValue = expenseGroup.GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expenseGroup, null); ((IDictionary<string, object>)objectToReturn).Add(field, fieldValue); } if (returnPartialExpense) { //add a list of expenses and in that add all those expenses List<object> expenses = new List<object>(); foreach (var expense in expenseGroup.Expenses) { expenses.Add(expenseFactory.CreateDataShapedObject(expense, listOfExpenseFields)); } ((IDictionary<string, object>)objectToReturn).Add("expenses", expenses); } return objectToReturn; }
public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List <string> fieldList) { List <string> listOfFieldsToWorkWith = new List <string>(fieldList); if (!listOfFieldsToWorkWith.Any()) { return(expenseGroup); } else { var listOfExpenseFields = listOfFieldsToWorkWith.Where(f => f.Contains("expenses")).ToList(); var returnPartialExpenses = listOfExpenseFields.Any() && !listOfExpenseFields.Contains("expenses"); if (returnPartialExpenses) { listOfFieldsToWorkWith.RemoveRange(listOfExpenseFields); // removes expense-related fields from the original list of fields listOfExpenseFields = listOfExpenseFields.Select(f => f.Substring(f.IndexOf(".", StringComparison.Ordinal) + 1)) .ToList(); } else { listOfExpenseFields.Remove("expenses"); listOfFieldsToWorkWith.RemoveRange(listOfExpenseFields); } ExpandoObject objectToReturn = new ExpandoObject(); foreach (var field in listOfFieldsToWorkWith) { var fieldValue = expenseGroup.GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expenseGroup, null); ((IDictionary <string, object>)objectToReturn).Add(field, fieldValue); } if (returnPartialExpenses) { List <object> expenses = new List <object>(); foreach (var expense in expenseGroup.Expenses) { expenses.Add(expenseFactory.CreateDataShapedObject(expense, listOfExpenseFields)); } ((IDictionary <string, object>)objectToReturn).Add("expenses", expenses); } return(objectToReturn); } }
public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List <string> lstOfFields) { List <string> fieldsToWorkWithList = new List <string>(lstOfFields); if (!fieldsToWorkWithList.Any()) { return(expenseGroup); } else { var lstOfExpenseFields = fieldsToWorkWithList.Where(f => f.Contains("expenses")).ToList(); bool returnPartialExpense = lstOfExpenseFields.Any() && !lstOfExpenseFields.Contains("expenses"); if (returnPartialExpense) { fieldsToWorkWithList.RemoveRange(lstOfExpenseFields); lstOfExpenseFields = lstOfExpenseFields.Select(f => f.Substring(f.IndexOf(".") + 1)).ToList(); } else { lstOfExpenseFields.Remove("expenses"); fieldsToWorkWithList.RemoveRange(lstOfExpenseFields); } ExpandoObject objectToReturn = new ExpandoObject(); foreach (var field in fieldsToWorkWithList) { var fieldValue = expenseGroup.GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expenseGroup, null); ((IDictionary <string, object>)objectToReturn).Add(field, fieldValue); } if (returnPartialExpense) { List <object> expenses = new List <object>(); foreach (var expense in expenseGroup.Expenses) { expenses.Add(expenseFactory.CreateDataShapedObject(expense, lstOfExpenseFields)); } ((IDictionary <string, object>)objectToReturn).Add("expenses", expenses); } return(objectToReturn); } }
public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List <string> fieldList) { List <string> workingFieldList = new List <string>(fieldList); if (!workingFieldList.Any()) { return(expenseGroup); } else { var expenseFieldList = workingFieldList.Where(f => f.ToLower().Contains("expense")).ToList(); bool returnPartialExpense = expenseFieldList.Any() && !expenseFieldList.Contains("expenses"); if (returnPartialExpense) { workingFieldList.RemoveRange(expenseFieldList); expenseFieldList = expenseFieldList.Select(f => f.Substring(f.IndexOf(".") + 1)).ToList(); } else { expenseFieldList.Remove("expenses"); workingFieldList.RemoveRange(expenseFieldList); } ExpandoObject shapedExpenseGroup = new ExpandoObject(); foreach (var field in workingFieldList) { var fieldValue = expenseGroup .GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expenseGroup, null); ((IDictionary <String, Object>)shapedExpenseGroup).Add(field, fieldValue); } if (returnPartialExpense) { List <object> expenses = new List <object>(); foreach (var expense in expenseGroup.Expenses) { expenses.Add(expenseFactory.CreateDataShapedObject(expense, expenseFieldList)); } ((IDictionary <String, Object>)shapedExpenseGroup).Add("expenses", expenses); } return(shapedExpenseGroup); } }
public object CreateDataShapedObject(DTO.ExpenseGroup expenseGroup, List <string> lstOfFields) { // work with a new instance, as we'll manipulate this list in this method var lstOfFieldsToWorkWith = new List <string>(lstOfFields); if (!lstOfFieldsToWorkWith.Any()) { return(expenseGroup); } else { //does it include any expense-related field? var lstofExpenseFields = lstOfFieldsToWorkWith.Where(f => f.Contains("expenses")).ToList(); // if one of those fields is "expenses, we need to ensure the FULL expense is returned.. If it's only // subfields, only those subfields have to be returned. var returnPartialExpense = lstofExpenseFields.Any() && !lstofExpenseFields.Contains("expenses"); // if we don't want to return the full expense, we need to know which fields if (returnPartialExpense) { // remove all expense related fields from the list of fields, // as we will use the CreateDateShapedObject function in ExpenseFactory for that. lstOfFieldsToWorkWith.RemoveRange(lstofExpenseFields); lstofExpenseFields = lstofExpenseFields.Select(f => f.Substring(f.IndexOf(".") + 1)).ToList(); } else { // we shouldn't return a partial expense, but the consumer might still // have asked for a subfields together with the main field, ie: expense,expense.id. // need to remove those subfields in that case. lstofExpenseFields.Remove("expenses"); lstOfFieldsToWorkWith.RemoveRange(lstofExpenseFields); } // create a new ExpandoObject & dynamically create the properties for this object // if we have an expense var objectToReturn = new ExpandoObject(); foreach (var field in lstOfFieldsToWorkWith) { // need to include public and instance, b/c specifying a binding flag overwrites the already existing binding flags. var fieldValue = expenseGroup.GetType() .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) .GetValue(expenseGroup, null); //add the field to the ExpandoObject ((IDictionary <String, Object>)objectToReturn).Add(field, fieldValue); } if (returnPartialExpense) { // add a list of expenses, and in that, add all those expenses var expenses = new List <object>(); foreach (var expense in expenseGroup.Expenses) { expenses.Add(expenseFactory.CreateDataShapedObject(expense, lstofExpenseFields)); } ((IDictionary <String, Object>)objectToReturn).Add("expenses", expenses); } return(objectToReturn); } }