Code Monkey home page Code Monkey logo

be-lib-masstransit-postgres-outbox's Introduction

1. Pandatech.MassTransit.PostgresOutbox

Welcome to the Pandatech MassTransit PostgreSQL Outbox Extension repository. This library is designed to enhance MassTransit's capabilities by introducing robust support for the Outbox and Inbox patterns with a particular focus on PostgreSQL, alongside seamless integration with multiple DbContexts in Entity Framework Core. This extension is ideal for developers seeking to ensure reliable message delivery and processing in distributed, microservice-oriented architectures.

1.1. Features

  • Multiple DbContext Support: Operate within complex systems using multiple data contexts without hassle.
  • Outbox Pattern Implementation: Reliably handle message sending operations, ensuring no messages are lost in transit, even in the event of system failures.
  • Inbox Pattern Support: Process incoming messages effectively, preventing duplicate processing and ensuring message consistency.
  • PostgreSQL ForUpdate Concurrency Handling: Utilize PostgreSQL's ForUpdate feature for enhanced concurrency control, making your message handling processes more robust.
  • Seamless Integration: Designed to fit effortlessly into existing MassTransit and EF Core based projects.

1.2. Getting Started

To get started with the Pandatech MassTransit PostgreSQL Outbox Extension, ensure you have the following prerequisites:

  • .NET Core 8 or later
  • An existing MassTransit project
  • PostgreSQL database

1.3. Installation

The library can be installed via NuGet Package Manager. Use the following command:

Install-Package Pandatech.MassTransit.PostgresOutbox

1.4. Configuration

Before diving into the usage, it's essential to configure the Pandatech MassTransit PostgreSQL Outbox Extension in your application. This involves setting up your DbContexts, configuring MassTransit to use the extension, and initializing the Outbox and Inbox features.

Stay tuned for the next sections where we'll cover the usage details, showcasing how you can leverage this powerful extension to enhance your distributed systems.

1.5. Usage

Take into account that examples below are given for configuring both inbox and outbox patterns. If you need only one of those , consider using appropriate methods available(eg. instead of AddOutboxInboxServices use AddInboxServices and etc).

1.5.1. Configuration

Entity Configuration: Ensure your DbContext implements the IOutboxDbContext and IInboxDbContext interfaces. Configure your entities and generate migrations. Call ConfigureInboxOutboxEntities on your ModelBuilder to configure the necessary tables for inbox and outbox patterns.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ConfigureInboxOutboxEntities();
}

And you need to call UseQueryLocks() inside AddDbContext or AddDbContextPool , this needs for enabling ForUpdate feature.

builder.Services.AddDbContextPool<PostgresContext>(options =>
         options.UseNpgsql(connectionString)
                .UseQueryLocks());

Service Registration: Register essential services on startup, specifying the DbContext type. You can optionally override settings(its optional parameter).

services.AddOutboxInboxServices<PostgresContext>();

1.5.2. Publishing Messages (Outbox Pattern)

To publish a message using the outbox pattern, call the AddToOutbox method on your DbContext, specifying your message. Remember to call SaveChanges() to persist the message to the database.

dbContext.Orders.Add(new Order
{
    Amount = 555,
    CreatedAt = DateTime.UtcNow,
});

// Add message to the outbox
dbContext.AddToOutbox(new OrderCreatedEvent());

// Save changes to the database
dbContext.SaveChanges();

1.5.3. Consuming Messages (Inbox Pattern)

To consume messages using the inbox pattern, create a consumer that inherits from InboxConsumer<TMessage, TDbContext> class, specifying the message type and DbContext type as generic arguments.

public class YourConsumer : InboxConsumer<YourMessage, PostgresContext>
{
    private readonly PostgresContext _context;

    public YourConsumer(PostgresContext dbContext, IServiceScopeFactory serviceScopeFactory)
        : base(serviceScopeFactory)
    {
        _context = dbContext;
    }

    public override async Task Consume(YourMessage message)
    {
        // Implement your message processing logic here
    }
}

1.6. License

Pandatech.MassTransit.PostgresOutbox is licensed under the MIT License.

be-lib-masstransit-postgres-outbox's People

Contributors

haikasatryan avatar mnacmargaryan avatar

Stargazers

 avatar  avatar  avatar

be-lib-masstransit-postgres-outbox's Issues

Please add support for a single consumer class that consumes multiple message types

A MassTransit consumer class may consume multiple message types by implementing the IConsumer with various message type arguments, for example

public class CustomerEventsConsumers :
    IConsumer<CustomerCreated>,
    IConsumer<CustomerUpdated>
{
    public Task Consume(ConsumeContext<CustomerCreated> context)
    {
        ...
    }

    public Task Consume(ConsumeContext<CustomerUpdated> context)
    {
        ...
    }
}

With the current design of this package, inbox consumer classes have to extend the InboxConsumer base class, which limit the ability for an inbox consumer to consumer more than one message type (C# does not allow multiple inheritance). It would be nice to have a built-in support for this feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.