@@ -43,6 +43,74 @@ Stay tuned for the next sections where we'll cover the usage details, showcasing
4343extension to enhance your distributed systems.
4444
4545## Usage
46+ Take into account that examples below are given for configuring both inbox and outbox patterns.
47+ If you need only one of those , consider using appropriate methods available(eg. instead of AddOutboxInboxServices use AddInboxServices and etc).
48+
49+ Configuration
50+
51+ Entity Configuration: Ensure your DbContext implements the IOutboxDbContext and IInboxDbContext interfaces.
52+ Configure your entities and generate migrations.
53+ Call ConfigureInboxOutboxEntities on your ModelBuilder to configure the necessary tables for inbox and outbox patterns.
54+
55+ ``` csharp
56+
57+ protected override void OnModelCreating (ModelBuilder modelBuilder )
58+ {
59+ modelBuilder .ConfigureInboxOutboxEntities ();
60+ }
61+ ```
62+
63+ Service Registration: Register essential services on startup, specifying the DbContext type.
64+ You can optionally override settings(its optional parameter).
65+
66+ ``` csharp
67+
68+ services .AddOutboxInboxServices <PostgresContext >();
69+ ```
70+
71+ Publishing Messages (Outbox Pattern)
72+
73+ To publish a message using the outbox pattern, call the AddToOutbox method on your DbContext,
74+ specifying your message. Remember to call SaveChanges() to persist the message to the database.
75+
76+ ``` csharp
77+
78+ dbContext .Orders .Add (new Order
79+ {
80+ Amount = 555 ,
81+ CreatedAt = DateTime .UtcNow ,
82+ });
83+
84+ // Add message to the outbox
85+ dbContext .AddToOutbox (new OrderCreatedEvent ());
86+
87+ // Save changes to the database
88+ dbContext .SaveChanges ();
89+ ```
90+
91+ Consuming Messages (Inbox Pattern)
92+
93+ To consume messages using the inbox pattern, create a consumer that inherits from
94+ InboxConsumer<TMessage, TDbContext> class, specifying the message type and DbContext type as generic arguments.
95+
96+ ``` csharp
97+
98+ public class YourConsumer : InboxConsumer <YourMessage , PostgresContext >
99+ {
100+ private readonly PostgresContext _context ;
101+
102+ public YourConsumer (PostgresContext dbContext , IServiceScopeFactory serviceScopeFactory )
103+ : base (serviceScopeFactory )
104+ {
105+ _context = dbContext ;
106+ }
107+
108+ public override async Task Consume (YourMessage message )
109+ {
110+ // Implement your message processing logic here
111+ }
112+ }
113+ ```
46114
47115## License
48116
0 commit comments