How mquangcao/minichatapp works
Project Type and Purpose
• This is a lightweight, modular chat‐room application composed of a Client, a Server, a reusable SDK, interchangeable persistence layers (In-Memory and MongoDB), and unit tests.
• Its goal is to demonstrate a clean separation of concerns (UI/CLI client, protocol, domain, persistence, command handling) and to allow swapping persistence implementations at will.
Main Components
a. Client (MiniChatApp.Client)
• A console/worker application that reads user input, constructs protocol‐compliant messages (via SDK), sends them to the chat server, and displays responses.
b. SDK (MiniChatApp.SDK)
• Defines Entities (ChatRoom, Message), Repositories interfaces (IChatRoomRepository), Services interfaces and defaults (IChatRoomManager, IMessageReader), and the communication Protocol.
• Acts as the domain layer and protocol contract for both Client and Server.
c. Infrastructure (MiniChatApp.Infrastructure.InMemory & .MongoDb)
• Concrete implementations of IChatRoomRepository:
• InMemoryChatRoomRepository (for testing or ephemeral usage)
• MongoDbChatRoomRepository (for production storage in MongoDB)
d. Server (MiniChatApp.Server)
• Listens for network connections, uses DefaultMessageReader to parse raw bytes, CommandParser & HeaderParser to interpret input, Builders (ChatCommandBuilder, MessageBuilder) + Factories (ChatCommandFactory, ReadMessageFactory) to produce command objects.
• Commands (CreateChatRoomCommand, SendMessageCommand) invoke the ChatRoomManager (from SDK) which in turn uses the injected IChatRoomRepository to persist or retrieve chat data.
e. Database / External Service
• MongoDB (optional, based on configuration in MongoDbChatRoomOptions)
f. Tests
• RepositoryTests and InMemoryInfrastructureTests validate repository behavior.
Component Relationships & Data Flow
1. User types a command in the Client →
2. Client.Worker uses SDK.Protocol to serialize into a message →
3. Over TCP (or configured transport) → Server.DefaultMessageReader receives bytes →
4. HeaderParser splits header/body → CommandParser delegates to ChatCommandFactory →
5. Command object (e.g., SendMessageCommand) is built, injected with IChatRoomManager →
6. ChatRoomManager calls the configured IChatRoomRepository (InMemory or MongoDb) →
7. Repository performs persistence →
8. Server constructs a response via MessageBuilder → back to Client →
9. Client deserializes and displays output.
Architectural Patterns & Design Principles
• Repository Pattern (IChatRoomRepository + implementations)
• Factory & Builder Patterns (for commands/messages)
• Dependency Injection (to swap persistence layers)
• Single Responsibility / Modularization (separate projects per concern)
• Protocol encapsulation in SDK (ensures both Client & Server agree on wire format)