2014-02-16 77 views
1

如果我们有多个更改的属性,您将如何实现对REST服务的批量更新?使用ServiceStack webservice进行批量更新

假设我们有一位管理员在他的软件中管理100台客户端计算机。一些电脑改变了他们的位置,一些被删除了。其他人得到了一个新的描述,等等。所以它不只是set location = 1 for ids {1,5,8}

是否有必要针对每种更改类型进行多个服务调用或者是否有可能我现在没有看到?

更新:
这不仅仅是将一个或多个记录更新到给定的值,而且是不同更新场景的组合。

  • Update电脑与ids [1,5,8]locationId=4
  • Delete计算机ids [7,9]
  • Create一个新的计算机id [10]locationId=7

这并不少见,如果客户端程序管理一串记录(这里的电脑)和点击“保存”例如。

回答

3

我会做这样的:

[Route("/computer/{ComputerId}", "POST")] 
public class UpdateComputerRequest : IReturnVoid 
{ 
    public int LocationId { get; set; } 
    public int ComputerId { get; set; } 
} 

[Route("/computer/{ComputerId}", "DELETE")] 
public class DeleteComputerRequest : IReturnVoid 
{ 
    public int ComputerId { get; set; } 
} 

[Route("/computers", "POST")] 
public class BatchRequest : IReturnVoid 
{ 
    public List<UpdateComputerRequest> UpdateRequests { get; set; } 
    public List<DeleteComputerRequest> DeleteRequests { get; set; } 
} 

public class ComputerLocationService : Service 
{ 
    public void Post(UpdateComputerRequest request) 
    { 
     PostImpl(request); 
    } 

    public void Post(DeleteComputerRequest request) 
    { 
     DeleteImpl(request); 
    } 

    public void Post(BatchRequest request) 
    { 
     request.UpdateRequests.ForEach(PostImpl); 
     request.DeleteRequests.ForEach(DeleteImpl); 
    } 

    private void PostImpl(UpdateComputerRequest request) 
    { 
     // do stuff... 
    } 

    private void DeleteImpl(DeleteComputerRequest deleteComputerRequest) 
    { 
     // delete 
    } 
} 

就没有创造,但应该清楚该怎么办呢?

+0

这完全有道理!谢谢! – TheBert

3

如果您想解释这种情况,您需要编写一个服务,在请求中接受一个I​​D数组,然后处理'批',如下所示。我写了两条路线,并且这些路线接受单个更新或一批更新。

// Update a location with single computer 
[Route("/Location/{LocationId}", "POST")] 
public class UpdateLocationRequest : IReturnVoid 
{ 
    public int LocationId { get; set; } 
    public int ComputerId { get; set; } 
} 

// Update a location with multiple computers 
[Route("/Location/{LocationId}/Multiple", "POST")] 
public class UpdateMultipleLocationsRequest : IReturnVoid 
{ 
    public int LocationId { get; set; } 
    public int[] ComputerIds { get; set; } 
}  

public class ComputerLocationService : Service 
{ 
    public void Post(UpdateLocationRequest request) 
    { 
     UpdateLocation(request.LocationId, request.ComputerId); 
    } 

    public void Post(UpdateMultipleLocationsRequest request) 
    { 
     // Multiple computers updated by calling the handler many times. 
     foreach(int computerId in request.ComputerIds) 
      UpdateLocation(request.LocationId, computerId); 
    } 

    // Method for handling updating one location 
    private void UpdateLocation(int locationId, int computerId) 
    { 
     // Logic to perform the update 
    } 
} 

因此,为了使单个更新我会张贴此JSON到/位置/ 1

{ "ComputerId": 10 } 

但是,为了使批量更新我会张贴此JSON到/位置/ 1 /多

{ "ComputerIds": [1,5,8] } 

希望有所帮助。

+0

不,这不是我错过的观点,我害怕。你会怎样做更新ID [1,5,8]到位置= 4并删除[7,9]并在一个请求中插入一个新项目{ID = 10,locationId = 7}。 – TheBert

+1

@TheBert这不是开箱即用的功能。您需要创建某种请求代理。 – Scott

+0

当然不是,我明白。但我仍然在寻找那种你称为“排序请求代理”的模式。 – TheBert

0
$.ajax({ 
     url: "/api/Keywords/Multiple", 
     data: JSON.stringify({ "ComputerIds": [1,5,8] }), 
     contentType: 'application/json; charset=utf-8', 
     type: "POST", 
     success: function (data) { 
      // 
     }, 
     error: function (XMLHttpRequest, textStatus, errorThrown) { 
      // 
     } 
    }) 

[Route("/api/Keywords/Multiple", "POST")] 
public class UpdateMultipleLocationsRequest : IReturn<Response> 
{ 
    public int[] ComputerIds { get; set; } 
} 

public Response Post(UpdateMultipleLocationsRequest request) 
     { 
      return new Response { Result = 1, Message = "..." }; 
     } 

此代码工作得很好。

1

Javascrit代码:

 // send AJAX request 
     var data = new Array(); 
     var checkedKeyword = { 
      "Keyword": "1", 
      "TotalNum": 100, 
      "State": "stop crawl", 
      "Updatetime": "2015-02-15 23:22:06", 
      "IsChecked": true 
     }; 
     data.push(checkedKeyword); 

     $.ajax({ 
      url: "/api/keywords/processchecked", 
      data: JSON.stringify({ "CheckedKeywords": data }), 
      contentType: 'application/json; charset=utf-8', 
      type: "POST", 
      success: function (data) { 
       // 
      }, 
      error: function (XMLHttpRequest, textStatus, errorThrown) { 
       // 
      } 
     }) 

    }); 

C#代码:

[Route("/api/keywords/processchecked", "POST")] 
public class RequestCheckedKeywords : IReturn<Response> 
{ 
    public List<CheckedKeyword> CheckedKeywords { get; set; } 
} 

public class CheckedKeyword 
{ 
    public string Keyword { get; set; } 
    public int TotalNum { get; set; } 
    public string State { get; set; } 
    public string Updatetime { get; set; } 
    public bool IsChecked { get; set; } 
} 

public class KeywordsServices : Service 
{ 
    public Response Post(RequestCheckedKeywords request) 
    { 
     return new Response { Result = 1, Message = "" }; 
    } 
} 

的JavaScript代码和C#代码工作得很好。 我可以得到RequestCheckedKeywords请求的值,它来自javascript。

+0

两个非常有用的链接: 结合复合物要求DTO https://groups.google.com/forum/#!topic/servicestack/77EXmIjkrBY 如何发送一个阵列使用JavaScript servicestack? https://groups.google.com/forum/#!topic/servicestack/OmFN4zeH48Q –