-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d092ca3
commit c59a193
Showing
7 changed files
with
251 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace CourseLibrary.API.Helpers | ||
{ | ||
public static class IEnumerableExtensions | ||
{ | ||
public static IEnumerable<ExpandoObject> ShapeData<TSource>( this IEnumerable<TSource> source, string fields) | ||
{ | ||
if (source == null) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
// create a list to hold our ExpandoObjects | ||
var expandoObjectList = new List<ExpandoObject>(); | ||
|
||
// create a list with PropertyInfo objects on TSource. Reflection is | ||
// expensive, so rather than doing if for each object in the list, we do | ||
// it once and reuse the results. After all, part of the reflection is on the | ||
// type of the object (TSource), not on the instance | ||
var propertyInfoList = new List<PropertyInfo>(); | ||
|
||
if (string.IsNullOrWhiteSpace(fields)) | ||
{ | ||
// all public properties should be in the ExpandoObject | ||
var propertyInfos = typeof(TSource) | ||
.GetProperties(BindingFlags.Public | BindingFlags.Instance); | ||
|
||
propertyInfoList.AddRange(propertyInfos); | ||
} | ||
else | ||
{ | ||
// the field are separated by ",", so we split it. | ||
var fieldsAfterSplit = fields.Split(','); | ||
|
||
foreach (var field in fieldsAfterSplit) | ||
{ | ||
// trim each field, as it might contain leading | ||
// or trailing spaces. Can't trim the var in foreach, | ||
// so use another var. | ||
var propertyName = field.Trim(); | ||
|
||
// use reflection to get the property on the source object | ||
// we need to include public and instance, b/c specifying a binding | ||
// flag overwrites the already-existing binding flags. | ||
var propertyInfo = typeof(TSource) | ||
.GetProperty(propertyName, BindingFlags.IgnoreCase | | ||
BindingFlags.Public | BindingFlags.Instance); | ||
|
||
if (propertyInfo == null) | ||
{ | ||
throw new Exception($"Property {propertyName} wasn't found on" + | ||
$" {typeof(TSource)}"); | ||
} | ||
|
||
// add propertyInfo to list | ||
propertyInfoList.Add(propertyInfo); | ||
} | ||
} | ||
|
||
// run through the source objects | ||
foreach (TSource sourceObject in source) | ||
{ | ||
// create an ExpandoObject that will hold the | ||
// selected properties & values | ||
var dataShapedObject = new ExpandoObject(); | ||
|
||
// Get the value of each property we have to return. For that, | ||
// we run through the list | ||
foreach (var propertyInfo in propertyInfoList) | ||
{ | ||
// GetValue returns the value of the property on the source object | ||
var propertyValue = propertyInfo.GetValue(sourceObject); | ||
|
||
// add the field to the ExpandoObject | ||
((IDictionary<string, object>)dataShapedObject) | ||
.Add(propertyInfo.Name, propertyValue); | ||
} | ||
|
||
// add the ExpandoObject to the list | ||
expandoObjectList.Add(dataShapedObject); | ||
} | ||
|
||
// return the list | ||
return expandoObjectList; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace CourseLibrary.API.Helpers | ||
{ | ||
public static class ObjectExtensions | ||
{ | ||
public static ExpandoObject ShapeData<TSource>(this TSource source, | ||
string fields) | ||
{ | ||
if (source == null) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
|
||
var dataShapedObject = new ExpandoObject(); | ||
|
||
if (string.IsNullOrWhiteSpace(fields)) | ||
{ | ||
// all public properties should be in the ExpandoObject | ||
var propertyInfos = typeof(TSource) | ||
.GetProperties(BindingFlags.IgnoreCase | | ||
BindingFlags.Public | BindingFlags.Instance); | ||
|
||
foreach (var propertyInfo in propertyInfos) | ||
{ | ||
// get the value of the property on the source object | ||
var propertyValue = propertyInfo.GetValue(source); | ||
|
||
// add the field to the ExpandoObject | ||
((IDictionary<string, object>)dataShapedObject) | ||
.Add(propertyInfo.Name, propertyValue); | ||
} | ||
|
||
return dataShapedObject; | ||
} | ||
|
||
// the field are separated by ",", so we split it. | ||
var fieldsAfterSplit = fields.Split(','); | ||
|
||
foreach (var field in fieldsAfterSplit) | ||
{ | ||
// trim each field, as it might contain leading | ||
// or trailing spaces. Can't trim the var in foreach, | ||
// so use another var. | ||
var propertyName = field.Trim(); | ||
|
||
// use reflection to get the property on the source object | ||
// we need to include public and instance, b/c specifying a | ||
// binding flag overwrites the already-existing binding flags. | ||
var propertyInfo = typeof(TSource) | ||
.GetProperty(propertyName, | ||
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); | ||
|
||
if (propertyInfo == null) | ||
{ | ||
throw new Exception($"Property {propertyName} wasn't found " + | ||
$"on {typeof(TSource)}"); | ||
} | ||
|
||
// get the value of the property on the source object | ||
var propertyValue = propertyInfo.GetValue(source); | ||
|
||
// add the field to the ExpandoObject | ||
((IDictionary<string, object>)dataShapedObject) | ||
.Add(propertyInfo.Name, propertyValue); | ||
} | ||
|
||
// return the list | ||
return dataShapedObject; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace CourseLibrary.API.Services | ||
{ | ||
public interface IPropertyCheckerService | ||
{ | ||
bool TypeHasProperties<T>(string fields); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace CourseLibrary.API.Services | ||
{ | ||
public class PropertyCheckerService : IPropertyCheckerService | ||
{ | ||
public bool TypeHasProperties<T>(string fields) | ||
{ | ||
if (string.IsNullOrWhiteSpace(fields)) | ||
{ | ||
return true; | ||
} | ||
|
||
// the field are separated by ",", so we split it. | ||
var fieldsAfterSplit = fields.Split(','); | ||
|
||
// check if the requested fields exist on source | ||
foreach (var field in fieldsAfterSplit) | ||
{ | ||
// trim each field, as it might contain leading | ||
// or trailing spaces. Can't trim the var in foreach, | ||
// so use another var. | ||
var propertyName = field.Trim(); | ||
|
||
// use reflection to check if the property can be | ||
// found on T. | ||
var propertyInfo = typeof(T) | ||
.GetProperty(propertyName, | ||
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); | ||
|
||
// it can't be found, return false | ||
if (propertyInfo == null) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
// all checks out, return true | ||
return true; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters