Add customer projection service and event processor#13
Add customer projection service and event processor#13
Conversation
Introduces MathMax.Administration.Projections.Customer project for handling customer event projections. Adds background services for consuming NATS events and updating PostgreSQL, configuration settings, and registers the new project in the solution file.
There was a problem hiding this comment.
Pull Request Overview
Adds a new Customer projections service project to consume events from NATS and update a PostgreSQL database.
- Introduces
MathMax.Administration.Projections.Customerproject and registers it in the solution - Implements
CustomerProjectionServiceto pull events from JetStream and post to a bounded channel - Implements
CustomerEventProcessorto read from the channel and upsert records in PostgreSQL
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| MathMax.EventSourcing.sln | Registers new project and adds platform configurations |
| ProjectionSettings.cs | Defines configuration POCO for NATS and Postgres |
| Program.cs | Sets up DI, hard-coded settings, and background hosts |
| MathMax.Administration.Projections.Customer.csproj | References required packages and projects |
| CustomerProjectionService.cs | Background service that pulls and filters NATS events |
| CustomerEventProcessor.cs | Background service that upserts customer records |
Comments suppressed due to low confidence (3)
MathMax.Administration.Projections.Customer/CustomerProjectionService.cs:13
- [nitpick] Consider adding unit and integration tests for
CustomerProjectionServiceto verify deserialization, branching logic, and backpressure handling.
public class CustomerProjectionService : BackgroundService
MathMax.Administration.Projections.Customer/ProjectionSettings.cs:3
- [nitpick] Public settings class lacks XML documentation. Adding
<summary>comments on the class and its properties will improve discoverability when binding and usingProjectionSettings.
public class ProjectionSettings
MathMax.Administration.Projections.Customer/Program.cs:25
- The call to
Channel.CreateBounded<EventEnvelope>requiresusing System.Threading.Channels;and you also needusing MathMax.EventSourcing;forEventEnvelope. Add these usings at the top of Program.cs so the code compiles.
services.AddSingleton(Channel.CreateBounded<EventEnvelope>(100));
| services.AddSingleton<ProjectionSettings>(new ProjectionSettings | ||
| { | ||
| NatsUrl = "nats://localhost:4222", | ||
| StreamName = "EVENTS", | ||
| ConsumerName = "projection-consumer", | ||
| Subject = "events.>", | ||
| PostgresConnectionString = "Host=localhost;Username=postgres;Password=yourpassword;Database=yourdb" |
There was a problem hiding this comment.
Hard-coding the database connection string (including credentials) is insecure. Consider moving this into configuration files or environment variables and binding it via IConfiguration.
| services.AddSingleton<ProjectionSettings>(new ProjectionSettings | |
| { | |
| NatsUrl = "nats://localhost:4222", | |
| StreamName = "EVENTS", | |
| ConsumerName = "projection-consumer", | |
| Subject = "events.>", | |
| PostgresConnectionString = "Host=localhost;Username=postgres;Password=yourpassword;Database=yourdb" | |
| services.AddSingleton<ProjectionSettings>(provider => | |
| { | |
| var configuration = provider.GetRequiredService<IConfiguration>(); | |
| return new ProjectionSettings | |
| { | |
| NatsUrl = "nats://localhost:4222", | |
| StreamName = "EVENTS", | |
| ConsumerName = "projection-consumer", | |
| Subject = "events.>", | |
| PostgresConnectionString = configuration.GetConnectionString("PostgresConnection") | |
| }; |
| var envelope = JsonSerializer.Deserialize<EventEnvelope>(Encoding.UTF8.GetString(msg.Data)); | ||
| if (envelope == null) | ||
| { | ||
| Console.WriteLine("Failed to deserialize EventEnvelope"); |
There was a problem hiding this comment.
[nitpick] Using Console.WriteLine prevents structured logging and makes it hard to control output. Inject an ILogger<CustomerProjectionService> and use _logger.LogError(...) instead.
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine($"Error processing event in DB: {ex}"); |
There was a problem hiding this comment.
[nitpick] Replace Console.WriteLine with a structured ILogger<CustomerEventProcessor> for better diagnostics and control over log levels.
Introduces MathMax.Administration.Projections.Customer project for handling customer event projections. Adds background services for consuming NATS events and updating PostgreSQL, configuration settings, and registers the new project in the solution file.