Michael Josiah
Michael Josiah

Reputation: 148

servicestack request best approach to implement search

What would you guys recommend as being the best way to implement search in servicestack. For instance at the moment I have an advanced search form which in the backend simply builds up a linq query dynamically. If I wanted to expose the search feature using service stack what is the best way to go about it.

I have seen some people using the idea of creating a request property like [object].FirstnameStartsWith , [object].SurnameContains etc

Upvotes: 4

Views: 299

Answers (2)

mythz
mythz

Reputation: 143349

There's no best way to design APIs for services, but your goals should be to be as descriptive and user-friendly as possible so clients can determine exactly what the service does by just looking at the request DTO.

Generally services should be cohesive and relevant for the use-case of the clients call context consuming them, e.g adding a new field/feature for that request should have the potential to be useful for existing clients already consuming that service.

Searching and Filtering a result-set is good example of this where every field/feature added is filtering the target result-set.

Other concerns I have when designing services is cache-ability, i.e. I separate long-term cacheable results from short-term non-cachable info.

Upvotes: 0

Master Morality
Master Morality

Reputation: 5927

I went with something like this

[Route("/things", "GET")]
public class ThingList
{
    public string Term { get; set; }
    public int Take { get; set; }
    public int Skip { get; set; }
}

public partial class ThingService : Service
{
     public object Get(ThingList req)
     {
         var query = this.Things // from somewhere

         if(!string.IsNullOrEmpty(req.Term))
             query = query.Where(x => x.Name.Contains(req.Term));

         if(req.Skip > 0) query = query.Skip(req.Skip);
         if(req.Take > 0) query = query.Take(req.Take);

         return query.ToList();
     }

}

Upvotes: 1

Related Questions