Skip to content

Instantly share code, notes, and snippets.

@explorer14
Created November 21, 2021 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save explorer14/858d3f2a22443dbb9bdd856f3d5af8f6 to your computer and use it in GitHub Desktop.
Save explorer14/858d3f2a22443dbb9bdd856f3d5af8f6 to your computer and use it in GitHub Desktop.
public async Task ProcessMessages()
{
var message = default(AcknowledgeableMessage)
try
{
var message = await GetMessage();
// process the message here
message.Acknowledge(); //a.k.a. transaction.Commit()
}
catch(Exception ex)
{
message.Abandon();
}
}
public class AcknowledgeableMessage
{
private IDbTransaction _transaction;
public string Id {get;}
//...other properties
public AcknowledgeableMessage(IDbTransaction transaction, string messageId,...)
{
_transaction = transaction;
Id = messageId;
// map other properties
}
public void Acknowledge()
{
_transaction?.Commit();
}
public void Abandon()
{
_transaction?.Rollback();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment