使用ServiceStack webservice进行批量更新

时间:2014-02-16 00:01:38

标签: c# servicestack

如果我们有多个已更改的属性,您将如何通过REST服务实现批量更新?

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

是否有必要为每种更改类型进行多次服务调用,或者有可能我现在看不到?

更新:
不仅要将一个或多个记录更新为给定值,还要将不同更新方案的组合更新。

  • Updateids [1,5,8]locationId=4
  • 的计算机
  • Delete计算机ids [7,9]
  • Create新计算机id [10]locationId=7

如果客户端程序管理一堆记录(此处为计算机)并点击“保存”,则这种情况并不罕见。

4 个答案:

答案 0 :(得分:3)

如果您想考虑该场景,您需要编写一个服务,在请求中接受Ids数组,然后处理'批处理',如下所示。我写了两个路由,这些路由接受单个更新或一批更新。

// 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发布到/ Location / 1

{ "ComputerId": 10 }

但要进行批量更新,我会将此JSON发布到/ Location / 1 / Multiple

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

希望有所帮助。

答案 1 :(得分: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
    }
}

没有创造,但应该清楚如何做到这一点......

答案 2 :(得分:1)

Javascript代码:

        // 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#代码工作得很好。 我可以获得请求检查关键字请求的价值,它是从javascript。

答案 3 :(得分: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 = "..." };
        }

此代码效果很好。