servicestack,Routing,servicestack" /> servicestack,Routing,servicestack" />

Routing 处理多个get操作

Routing 处理多个get操作,routing,servicestack,Routing,servicestack,我是ServiceStack的新手,我正在尝试找出处理同一请求上的多个get操作的最佳实践。以下是我的请求对象: [Route("/Entity", Verbs = "GET", Notes = "Returns all the entities.")] [Route("/Entity/{Id}", Verbs = "GET", Notes = "Returns a specific entity.")] [Route("/Entity/{Id}/Child", Verbs = "GET", N

我是ServiceStack的新手,我正在尝试找出处理同一请求上的多个get操作的最佳实践。以下是我的请求对象:

[Route("/Entity", Verbs = "GET", Notes = "Returns all the entities.")]
[Route("/Entity/{Id}", Verbs = "GET", Notes = "Returns a specific entity.")]
[Route("/Entity/{Id}/Child", Verbs = "GET", Notes = "Returns children of a specific entity.")]    
public class EntityRequest {
    public int Id { get; set; }
    public int ChildId { get; set; }
}
以下是我的服务:

public object Get(EntityRequest request) {
        if (request.Id > 0) {
            //returns a given entity
            return _applicationService.getEntities(request.Id);
        }

        //How do I handle this? Check for the word "/Child" in the URL?
        //returns children for a given entity
        //return _applicationService.getChildren(request.Id);

        //returns all the entities
        return _applicationService.getEntities();
    }
}
如您所见,我正在处理来自服务端的前两条路由“/Entity”和“/Entity/{Id}”。如何最好地处理“/Entity/{Id}/Child”路由?在当前状态下,第三个URL将返回所有实体。如果有任何帮助,我们将不胜感激


谢谢

看看下面这些现有答案,这些答案介绍了使用ServiceStack设计服务的推荐方法:

如果响应不同,建议使用不同的服务。您的服务也应该是自我描述的,而不是依赖注释中的文档来描述每项服务(很少有人阅读),因此我会将您的服务重新设计为:

[Route("/entities")]
public class GetAllEntities : IReturn<GetAllEntitiesResponse> {}

public class GetAllEntitiesResponse
{
    public List<Entity> Results { get; set; }
}
我个人的偏好是对每个服务使用显式响应DTO,因为它将来会验证服务,并允许您以后改进服务以在不破坏现有服务客户端的情况下返回其他结果,但如果您愿意,您可以直接返回结果,而无需显式响应DTO包装器,例如:

[Route("/entities")]
public class GetAllEntities : IReturn<List<Entity>> {}

[Route("/entities/{Id}")]
public class GetEntity : IReturn<Entity>
{
    public int Id { get; set; }
}

[Route("/entities/{Id}/children")]
public class GetEntityChildren : IReturn<List<EntityChild>>
{
    public int Id { get; set; }
}
[路由(“/实体”)]
公共类getAllenties:IReturn{}
[路由(“/entities/{Id}”)]
公共类GetEntity:IReturn
{
公共int Id{get;set;}
}
[路由(“/entities/{Id}/children”)]
公共类GetEntityChildren:IReturn
{
公共int Id{get;set;}
}
[Route("/entities/{Id}")]
public class GetEntity : IReturn<GetEntityResponse>
{
    public int Id { get; set; }
}

public class GetEntityResponse
{
    public Entity Result { get; set; }
}
[Route("/entities/{Id}/children")]
public class GetEntityChildren : IReturn<GetEntityChildrenResponse>
{
    public int Id { get; set; }
}

public class GetEntityChildrenResponse
{
    public List<EntityChild> Results { get; set; }
}
var response = client.Get(new GetAllEntities());
var response = client.Get(new GetEntity { Id = 1 });
var response = client.Get(new GetEntityChildren { Id = 1 });
[Route("/entities")]
public class GetAllEntities : IReturn<List<Entity>> {}

[Route("/entities/{Id}")]
public class GetEntity : IReturn<Entity>
{
    public int Id { get; set; }
}

[Route("/entities/{Id}/children")]
public class GetEntityChildren : IReturn<List<EntityChild>>
{
    public int Id { get; set; }
}