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)
-
0
You can return the object type to dynamically return dto such as:
Task<object> GetPeople();
-
0
Perfict! It works as expected.
Thanks,
-
0
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 toTask<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,
-
0
Are you talking about nswag?
-
0
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.
-
0
@fguo you can add JsonProperty attribute to your fields on your Dto.
-
0
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,
-
0
@fguo
GraphQL will be ready probably for v6.8. It will contain users, roles and organizationUnits queries by default.
-
0
Wait for GraphQL implementation https://github.com/aspnetzero/aspnet-zero-core/issues/1175
-
0
"You can return the object type to dynamically return dto such as: Task<object> GetPeople();"
But Client Side return null. (Angular TypeScript page)