Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DP-396: Publish messages in the same transaction as other database operations #733

Merged
merged 11 commits into from
Oct 14, 2024

Conversation

jakzal
Copy link
Member

@jakzal jakzal commented Oct 9, 2024

I missed it in my previous PRs.

In this PR, I added an InTransaction extension method on DbContext:

await context.InTransaction(async context =>
{
    // Everything in here will be wrapped in a transaction
});

This extension method can then be used in repositories where we have access to the DbContext:

public async Task SaveAsync(Organisation organisation, Func<Organisation, Task> onSave) =>
    await context.InTransaction(async context =>
    {
            Save(organisation);
            // Assumption here, is whatever `onSave` does, it uses the same `DbContext`.
            await onSave(organisation);
            await context.SaveChangesAsync();
    });

For this purpose, I extended SaveAsync with an additional callback parameter. The first argument is saved, then the callback is called, all in the same transaction:

await organisationRepository.SaveAsync(
    organisation,
    async o => await publisher.Publish(mapper.Map<OrganisationUpdated>(o))
);

@jakzal jakzal marked this pull request as ready for review October 11, 2024 15:27
@jakzal jakzal merged commit d635bce into main Oct 14, 2024
8 checks passed
@jakzal jakzal deleted the feature/DP-396-transaction branch October 14, 2024 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants