2013-03-25 36 views
4
//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」的對象)到服務。如何使用JsonServiceClient過濾對象

回答

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" }); 
+0

謝謝,這是我錯過了(對於長時間的復活節延遲) – gerhard 2013-04-04 11:56:54