Autoquery CRUD for batch operations

Viewed 109

Is there any practice using ServiceStack AutoQuery Crud to post multiple CreateDb<T> requests to insert in single transaction multiple rows (Auto Batched Requests?)?

UPDATE: I have tried to use @mythz solution but custom service for processing batching requests of type ICreateDb results in exception ResolutionException:

"Required dependency of type aproject.core.ServiceInterface.Services.ProjectContractsService could not be resolved."

Funq.ResolutionException: Required dependency of type aproject.core.ServiceInterface.Services.ProjectContractsService could not be resolved. at ServiceStack.Host.ContainerResolveCache.CreateInstance(IResolver resolver, Type type, Boolean tryResolve) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\ContainerResolveCache.cs:line 60 at ServiceStack.Host.ContainerResolveCache.CreateInstance(IResolver resolver, Type type) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\ContainerResolveCache.cs:line 34 at ServiceStack.Host.ServiceController.<>c__DisplayClass41_0.<RegisterServiceExecutor>g__HandlerFn|0(IRequest req, Object dto) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\ServiceController.cs:line 437 at ServiceStack.Host.ServiceController.ExecuteAsync(Object requestDto, IRequest req) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\ServiceController.cs:line 674 at ServiceStack.Host.Handlers.GenericHandler.ProcessRequestAsync(IRequest httpReq, IResponse httpRes, String operationName) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\Handlers\GenericHandler.cs:line 62

The Autoquery is registered correctlly but I have found that my ICreateDb DTO for crating the entries is registered multiple times:

DEBUG: Registering OneWay service 'ContractsService' with request 'CreateContractEstimate[]' DEBUG: Registering Reply service '__AutoQueryServices' with request 'CreateContractEstimate'

1 Answers

I've just implemented support for auto implementing batch implementations for all CRUD operations which will by default execute all AutoQuery CRUD Requests within a DB transaction.

By default it will generate AutoBatch implementations for all CRUD operations and can be changed to only generate implementations for specific CRUD operations by changing:

Plugins.Add(new AutoQueryFeature {
    GenerateAutoBatchImplementationsFor = new() { AutoCrudOperation.Create }
});

It also wont generate implementations for custom AutoBatch implementations, e.g. you can add a custom implementation that does what the generated implementation would've done and execute using the same DB Connection and Transaction with:

public class CustomAutoQueryServices : Service
{
    public IAutoQueryDb AutoQuery { get; set; }

    public object Any(CreateItem[] requests)
    {
        using var db = AutoQuery.GetDb<Item>(Request);
        using var dbTrans = db.OpenTransaction();

        var results = new List<object>();
        foreach (var request in requests)
        {
            var response = await AutoQuery.CreateAsync(request, Request, db);
            results.Add(response);
        }

        dbTrans.Commit();
        return results;            
    }
}

and as AutoQuery Services are just normal ServiceStack Services you can re-use the existing Service Client support for Auto Batched Requests, e.g:

var client = new JsonServiceClient(BaseUrl);
var response = client.SendAll(new CreateItem[] { ... });

This feature is available from v5.11.1 that's now available on MyGet.

Related