gpt4 book ai didi

servicestack - 如何使用 JsonServiceClient 过滤对象

转载 作者:行者123 更新时间:2023-12-01 03:58:02 28 4
gpt4 key购买 nike

//DTO
public class SampleDto : IReturn<SampleDto>
{
public int Id { get; set; }
public string Description { get; set; }
}
public class ListSampleDto : IReturn<List<SampleDto>>
{
}
//Service
public class SampleService : Service
{
public object Get(ListSampleDto request)
{
List<SampleDto> res = new List<SampleDto>();
res.Add(new SampleDto() { Id = 1, Description = "first" });
res.Add(new SampleDto() { Id = 2, Description = "second" });
res.Add(new SampleDto() { Id = 3, Description = "third" });
return res;
}
}
//Client
string ListeningOn = ServiceStack.Configuration.ConfigUtils.GetAppSetting("ListeningOn");
JsonServiceClient jsc = new JsonServiceClient(ListeningOn);
// How to tell the service only to deliver the objects where Description inludes the letter "i"
List<SampleDto> ks = jsc.Get(new ListSampleDto());

我不知道如何将过滤条件(例如,仅获取描述包含字母“i”的对象)从 JsonServiceClient 发送到服务。

最佳答案

在这种情况下,您通常会使用您评估服务器端的属性来扩展您的输入 Dto(在本例中为 ListSampleDto )以提供正确的响应:

// Request Dto:
public class ListSampleDto
{
public string Filter { get; set; }
}
...
// Service implementation:
public object Get(ListSampleDto request)
{
List<SampleDto> res = new List<SampleDto>();
res.Add(new SampleDto() { Id = 1, Description = "first" });
res.Add(new SampleDto() { Id = 2, Description = "second" });
res.Add(new SampleDto() { Id = 3, Description = "third" });
if (!string.IsNullOrEmpty(request.Filter)) {
res = res.Where(r => r.Description.StartsWith(request.Filter)).ToList()
}
return res;
}
...
// Client call:
List<SampleDto> ks = jsc.Get(new ListSampleDto { Filter = "i" });

关于servicestack - 如何使用 JsonServiceClient 过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15619894/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com