如何使用JsonServiceClient过滤对象

时间:2013-03-25 16:27:38

标签: servicestack

//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()); 

我不知道如何从JsonServiceClient向服务发送过滤条件(例如,只获取说明中包含字母“i”的对象)。

1 个答案:

答案 0 :(得分:2)

在这种情况下,您通常会扩展输入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" }); 
相关问题