patterncsharpMinor
.NET REST API JSON Filtering
Viewed 0 times
restnetjsonapifiltering
Problem
My question, REST API and lazy loading objects, didn't receive any answers (or many views), so here is my attempt at the solution.
Problem overview:
My mobile app requests a list of objects through a HTTP REST API but only required a small subset of each item to display items in a list. Other larger properties should not be returned unless requested i.e. in an an object detailed view.
My attempt:
API will act as normal unless a querystring is appended with the fields that are required.
I know that I need to fix the
My other concern is that this code is redundant due to my use of Automapper that may include functionality similar to this.
Also, could it be more efficient?
Problem overview:
My mobile app requests a list of objects through a HTTP REST API but only required a small subset of each item to display items in a list. Other larger properties should not be returned unless requested i.e. in an an object detailed view.
My attempt:
API will act as normal unless a querystring is appended with the fields that are required.
I know that I need to fix the
requiredFields.Contains(property.Name) call to ignore case during comparison.My other concern is that this code is redundant due to my use of Automapper that may include functionality similar to this.
Also, could it be more efficient?
public static Object FilterFields(Object target, Uri requestUri)
{
NameValueCollection mapQuery = UriExtensions.ParseQueryString(requestUri);
IEnumerable requiredFields = null;
if (mapQuery != null && mapQuery.Get("fields") != null)
requiredFields = mapQuery.Get("fields").Split(new char[] { ',' });
if (requiredFields != null)
{
foreach (PropertyInfo property in target.GetType().GetProperties())
{
if (!requiredFields.Contains(property.Name))
{
property.SetValue(target, null);
}
}
}
return target;
}Solution
Style
Be consistent in your style. If you use braces
Using guard conditions help you to save horizontal space.
otherwise your code looks good.
Be consistent in your style. If you use braces
{} for single if statements then you should use them always. Using guard conditions help you to save horizontal space.
NameValueCollection mapQuery = UriExtensions.ParseQueryString(requestUri);
if (mapQuery == null) { return target; }
string fields = mapQuery.Get("fields");
if (String.IsNullOrEmpty(fields)) { return target; }
IEnumerable requiredFields = fields.Split(new char[] { ',' });
if (requiredFields == null) { return target; }
your loop hereotherwise your code looks good.
Code Snippets
NameValueCollection mapQuery = UriExtensions.ParseQueryString(requestUri);
if (mapQuery == null) { return target; }
string fields = mapQuery.Get("fields");
if (String.IsNullOrEmpty(fields)) { return target; }
IEnumerable<string> requiredFields = fields.Split(new char[] { ',' });
if (requiredFields == null) { return target; }
your loop hereContext
StackExchange Code Review Q#69602, answer score: 5
Revisions (0)
No revisions yet.