skip to Main Content

I’m trying to return objects based on query strings.
For example, I would like api/users/{id}?fields=username,email,reputation to return an object of type User that contains only the three included properties (username, email, reputation).

On a side note, comma separated query strings are not possible by default in .NET Core.
Here’s a tutorial on making that work.

Following the guide above, I have a list of strings. How can I create an object that only includes the properties that match the names of those strings?
For only a few strings I can do this (thanks Ben Hall):

List<string> listOfStrings ...; // Strings from query
User user = GetUser(id); // User from db 

User newUser = new User();

if (listOfStrings.Contains("username"))
    newUser.username = user.username;
if (listOfStrings.Contains("email"))
    newUser.email = user.email;
if (listOfStrings.Contains("reputation"))
    newUser.reputation = user.reputation;

But for a long list of strings (my user class has 30+ properties) how would I go about doing this?

For reference, Facebook Graph API does this.

3

Answers


  1. Chosen as BEST ANSWER

    Turns out there is actually Middleware for this called Popcorn.

    Popcorn is a communication protocol on top of a RESTful API that allows requesting clients to identify individual fields of resources to include when retrieving the resource or resource collection.

    It allows for a recursive selection of fields, allowing multiple calls to be condensed into one.

    Features:

    • Selective inclusion from a RESTful API
    • Configurable response defaults
    • Sorting of responses
    • Selective authorization and permissioning of response values
    • Configurable response inspectors
    • Factory and advanced projection support
    • Set relevant contexts for your API
    • Blind expansion f response objects

  2. We could explore other ways like reflection but if should still serve you well enough. Looks like you’re making a simple mistake trying to use an If in an object initialiser. You will have to do that after instaniating the new User object e.g.

    User newUser = new User();
    if (listOfStrings.Contains("username"))
    {
           newUser.username = user.username;
    }
    
    Login or Signup to reply.
  3. List<string> listOfStrings ...; // Strings from query
    User user = GetUser(id); // User from db 
    
    User newUser = new User();
    //gettting object type
    var userType = user.GetType();
    

    This solution needs some knowledge about Reflections
    It uses the methods GetValue and SetValue
    (The code is tested in Visual Studio)
    Now foreach element in listOfStrings

    foreach(var propertyName in listOfString){
    // This line of code retrives the value of the propety in the user class
     var retrivedValue = userType.GetProperty(propertyName).GetValue(user);
    // This line of code sets the value retrived to the property in the newUser class
     userType.GetProperty(propertyName).SetValue(newUser, retrivedValue , null);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search