IEvangelist / azure-cosmos-dotnet-repository

Wraps the .NET SDK for Azure Cosmos DB abstracting away the complexity, exposing a simple CRUD-based repository pattern
https://ievangelist.github.io/azure-cosmos-dotnet-repository
MIT License
310 stars 92 forks source link

Support for TransactionalBatch #151

Open catsburg opened 2 years ago

catsburg commented 2 years ago

The repositories currently don't offer support for transactions (using TransactionalBatch), for use cases where a bunch of CRUD actions have to all succeed/fail together. If transactions are required, the only solution now is to implement a custom Repository, which kind of defeats the purpose of using this library.

It would be cool to be able to do something along the lines of: await _repository.Batch(partitionKey).Create(item1).Replace(item2.Id, item2).ExecuteAsync();

mumby0168 commented 2 years ago

Hi @catsburg, this is certainly a part of our plans myself & @IEvangelist where just chatting about it the other day. It might be required that we inject something other than IRepository<T> to deal with batches as a batch can span multiple IItems and current the IRepository<T> is scoped to one item in this libraries case, but I like the look of your API it certainly could be done and could ease some of the error handling when a batch fails. Like the idea 😃

mumby0168 commented 2 years ago

Having a look at this and found this example, the question here is how can we make this API they already offer easier to use, just an open question, see here.

https://github.com/Azure/azure-cosmos-dotnet-v3/tree/master/Microsoft.Azure.Cosmos.Samples/Usage/TransactionalBatch

catsburg commented 2 years ago

@mumby0168 Glad to hear that it's part of your plans!

Just to share some thoughts on this:

Hope this is useful! Pretty cool project you guys have, but unfortunately since we require transactions, we're not going to use it for now, and create our own Repositories instead .

philip-reed commented 4 months ago
  • For the missing use case (multiple-types-per-container AND you want to do a transaction across multiple types): How relevant is this use case? Wouldn't there be something to say for IF you find yourself wanting to do this, you probably modeled your application poorly? Like for instance, you should denormalize further?

A transactional batch that supports multiple item types might allow this library to be used to follow the Transactional Outbox pattern described here, though it might be a little difficult modelling this with the current IRepository<T>:

https://learn.microsoft.com/en-us/azure/architecture/databases/guide/transactional-outbox-cosmos