Base solution for your next web application
Open Closed

How to return a dynamic type from AppService method? #6511


User avatar
0
fguo created

I am using Core + Angular version. Currently, all of AppService methods return a specific type, such as: ListResultDto<PersonListDto> GetPeople(GetPeopleInput input){...}

In my application, the type PersonListDto has many properties. Now, my users want to get partial of these peroperties (e.g. only FirstName and Address). I surely can make another type and method for this request, such as: ListResultDto<NameAddresssListDto> GetPeople(GetPeopleInput input){...}

However, users want to freely choose properties from PersonListDto type, and get a return only includes the properties accordingly. So, I need to make a method to return a generic type dynamically. I need a method, such as: ListResultDto<T> GetPeople(GetPeopleInput input){...}

For example, if the GetPeopleInput contains "fields = 'FirstName', 'Address'", the <T> should be a type only contains two peroperties, FirstName and Address.

Is it possible? If so, can you provide an sample code?

Thanks,


10 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    You can return the object type to dynamically return dto such as: Task<object> GetPeople();

  • User Avatar
    0
    fguo created

    Perfict! It works as expected.

    Thanks,

  • User Avatar
    0
    fguo created

    The only negative thing is that, after I use <object>, I have to keep the same Pascal Case or Camel Case for field names on both server side (C#) and client side (TypeScript, Angular).

    For example, the “Person” type has a property "Name". When I use the specific type such as Task<Person> GetPeople(); It automatically changed to "name" on client side and back to "Name" on server side. After I changed to Task<object> GetPeople(); I have to keep "Name" on client side. Otherwise, it throw an exception such as "'Person' does not contain a definition of 'name'"

    Is there an easy way to keep the behavior before (Pascal Case on server side code and camel Case on client side code)?

    Thanks,

  • User Avatar
    0
    maliming created
    Support Team

    Are you talking about nswag?

  • User Avatar
    0
    fguo created

    I am not too sure. While I use the swag to test, it works fine (i.e. the property "Name" shows as "name" on screen). However, after I refresh service on Angualr (Angular/nswag/refresh), I need to change "name" to "Name" in my Angular side code.

  • User Avatar
    0
    ismcagdas created
    Support Team

    @fguo you can add JsonProperty attribute to your fields on your Dto.

  • User Avatar
    0
    fguo created

    It sounds not too easy in my case.

    Never mind. It's not a big deal to me for now. It is just an alternative of GraphQL implement for an endpoint of mine.

    I noticed "GraphQL endpoint" is on the top 2nd on the road map. Can I expect it to release in a couple of months?

    Thanks,

  • User Avatar
    0
    ismcagdas created
    Support Team

    @fguo

    GraphQL will be ready probably for v6.8. It will contain users, roles and organizationUnits queries by default.

  • User Avatar
    0
    alper created
    Support Team

    Wait for GraphQL implementation https://github.com/aspnetzero/aspnet-zero-core/issues/1175

  • User Avatar
    0
    velu created

    "You can return the object type to dynamically return dto such as: Task<object> GetPeople();"

    But Client Side return null. (Angular TypeScript page)