-
Notifications
You must be signed in to change notification settings - Fork 38
Generics
fonlow edited this page Aug 26, 2018
·
1 revision
Please note that not every languages support generics.
WebApiClientGen in earlier releases has supported some BCL generic types like Dictionary<>, IDictionary<>, List<> and IList<> etc. Starting from v2.5, WebApiClientGen supports custom generic classes.
Web API
[DataContract(Namespace = Constants.DataNamespace)]
public class MimsResult<T>
{
[DataMember]
public T Result { get; set; }
[DataMember]
public DateTime GeneratedAt { get; set; }
[DataMember]
public bool Success { get; set; } = true;
[DataMember]
public string Message { get; set; }
}
[DataContract(Namespace = Constants.DataNamespace)]
public class MimsPackage
{
[DataMember]
public MimsResult<Decimal> Result { get; set; }
[DataMember]
public string Tag { get; set; }
}
[DataContract(Namespace = Constants.DataNamespace)]
public class MyGeneric<T, K, U>
{
[DataMember]
public T MyT { get; set; }
[DataMember]
public K MyK { get; set; }
[DataMember]
public U MyU { get; set; }
[DataMember]
public string Status { get; set; }
}
[HttpPost]
[Route("Mims")]
public MimsResult<string> GetMims([FromBody] MimsPackage p)
...
[HttpPost]
[Route("MyGeneric")]
public MyGeneric<string, decimal, double> GetMyGeneric([FromBody] MyGeneric<string, decimal, double> s)
...
[HttpPost]
[Route("MyGenericPerson")]
public MyGeneric<string, decimal, Person> GetMyGenericPerson([FromBody] MyGeneric<string, decimal, Person> s)
...
Client API in TypeScript for Angular 2+:
export interface MimsResult<T> {
result?: T;
generatedAt?: Date;
success?: boolean;
message?: string;
}
export interface MimsPackage {
result?: DemoWebApi_DemoData_Client.MimsResult<number>;
tag?: string;
}
export interface MyGeneric<T, K, U> {
myT?: T;
myK?: K;
myU?: U;
status?: string;
}
getMims(p: DemoWebApi_DemoData_Client.MimsPackage): Observable<DemoWebApi_DemoData_Client.MimsResult<string>> {
return this.http.post<DemoWebApi_DemoData_Client.MimsResult<string>>(this.baseUri + 'api/Entities/Mims', JSON.stringify(p), { headers: { 'Content-Type': 'application/json;charset=UTF-8' } });
}
getMyGeneric(s: DemoWebApi_DemoData_Client.MyGeneric<string, number, number>): Observable<DemoWebApi_DemoData_Client.MyGeneric<string, number, number>> {
return this.http.post<DemoWebApi_DemoData_Client.MyGeneric<string, number, number>>(this.baseUri + 'api/Entities/MyGeneric', JSON.stringify(s), { headers: { 'Content-Type': 'application/json;charset=UTF-8' } });
}
getMyGenericPerson(s: DemoWebApi_DemoData_Client.MyGeneric<string, number, DemoWebApi_DemoData_Client.Person>): Observable<DemoWebApi_DemoData_Client.MyGeneric<string, number, DemoWebApi_DemoData_Client.Person>> {
return this.http.post<DemoWebApi_DemoData_Client.MyGeneric<string, number, DemoWebApi_DemoData_Client.Person>>(this.baseUri + 'api/Entities/MyGenericPerson', JSON.stringify(s), { headers: { 'Content-Type': 'application/json;charset=UTF-8' } });
}
Client API in C#:
public class MimsResult<T> : object
{
private T _Result;
...
public T Result
{
get
{
return _Result;
}
set
{
_Result = value;
}
}
...
public async Task<DemoWebApi.DemoData.Client.MimsResult<string>> GetMimsAsync(DemoWebApi.DemoData.Client.MimsPackage p)
{
...
public async Task<DemoWebApi.DemoData.Client.MyGeneric<string, decimal, double>> GetMyGenericAsync(DemoWebApi.DemoData.Client.MyGeneric<string, decimal, double> s)
{
...