# mquangcao/minichatapp architecture

> Architecture diagram of the GitHub repository mquangcao/minichatapp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mquangcao/minichatapp
- Repository: https://github.com/mquangcao/minichatapp

## Overview

##### 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)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        ClientProgram["Program.cs"]:::project
        ClientWorker["Worker.cs"]:::project
    end

    %% Protocol Layer
    subgraph "Protocol Layer"
        ChatRoom["ChatRoom.cs"]:::project
        Message["Message.cs"]:::project
        IChatRoomRepository["IChatRoomRepository"]:::interface
        IChatRoomManager["IChatRoomManager"]:::interface
        DefaultChatRoomManager["DefaultChatRoomManager.cs"]:::project
        IMessageReader["IMessageReader"]:::interface
        ProtocolUtil["Protocol.cs"]:::project
    end

    %% Application Layer
    subgraph "Application Layer"
        ServerProgram["Program.cs"]:::project
        ServerWorker["Worker.cs"]:::project
        DefaultMessageReader["DefaultMessageReader.cs"]:::interface
        HeaderParser["HeaderParser.cs"]:::interface
        CommandParser["CommandParser.cs"]:::interface
        ChatCommandBuilder["ChatCommandBuilder.cs"]:::factory
        MessageBuilder["MessageBuilder.cs"]:::factory
        ChatCommandFactory["ChatCommandFactory.cs"]:::factory
        ReadMessageFactory["ReadMessageFactory.cs"]:::factory
        ChatRoomManagerSrv["ChatRoomManager"]:::project
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        InMemoryRepo["InMemoryChatRoomRepository.cs"]:::project
        MongoOptions["MongoDbChatRoomOptions.cs"]:::project
        MongoRepo["MongoDbChatRoomRepository.cs"]:::project
    end

    %% Data Storage
    subgraph "Data Storage"
        MongoDB["MongoDB"]:::database
    end

    %% Tests
    subgraph "Tests"
        InMemoryTests["InMemoryChatRoomRepositoryTests.cs"]:::project
        RepoTests["ChatRoomRepositoryTests.cs"]:::project
    end

    %% Connections
    ClientWorker -->|"uses Protocol"| ProtocolUtil
    ClientWorker -->|"serialize/deserialize"| ChatRoom
    ClientWorker -->|"serialize/deserialize"| Message
    ClientWorker -->|"send over TCP"| ServerProgram
    ServerWorker -->|"read bytes"| DefaultMessageReader
    DefaultMessageReader -->|"split header/body"| HeaderParser
    HeaderParser -->|"parse command"| CommandParser
    CommandParser -->|"build message"| MessageBuilder
    CommandParser -->|"build command"| ChatCommandBuilder
    ChatCommandBuilder --> ChatCommandFactory
    MessageBuilder --> ReadMessageFactory
    ChatCommandFactory -->|"produce command"| ChatRoomManagerSrv
    ChatRoomManagerSrv -->|"calls"| IChatRoomRepository
    IChatRoomRepository --> InMemoryRepo
    IChatRoomRepository --> MongoRepo
    MongoRepo -->|"connects"| MongoDB

    InMemoryTests --> InMemoryRepo
    RepoTests --> IChatRoomRepository
    RepoTests --> MongoRepo

    %% Click Events
    click ClientProgram "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Client/Program.cs"
    click ClientWorker "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Client/Worker.cs"
    click ChatRoom "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.SDK/Entities/ChatRoom.cs"
    click Message "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.SDK/Entities/Message.cs"
    click IChatRoomRepository "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.SDK/Repositories/IChatRoomRepository.cs"
    click IChatRoomManager "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.SDK/Services/ChatRoomManager/IChatRoomManager.cs"
    click DefaultChatRoomManager "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.SDK/Services/ChatRoomManager/DefaultChatRoomManager.cs"
    click IMessageReader "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.SDK/Services/MessageReader/IMessageReader.cs"
    click ProtocolUtil "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.SDK/Utils/Protocol.cs"
    click InMemoryRepo "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Infrastructure.InMemory/InMemoryChatRoomRepository.cs"
    click MongoOptions "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Infrastructure.MongoDb/MongoDbChatRoomOptions.cs"
    click MongoRepo "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Infrastructure.MongoDb/MongoDbChatRoomRepository.cs"
    click ServerProgram "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Server/Program.cs"
    click ServerWorker "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Server/Worker.cs"
    click DefaultMessageReader "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Server/MessageReader/DefaultMessageReader.cs"
    click HeaderParser "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Server/Utils/HeaderParser.cs"
    click CommandParser "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Server/Utils/CommandParser.cs"
    click ChatCommandBuilder "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Server/Builder/ChatCommandBuilder.cs"
    click MessageBuilder "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Server/Builder/MessageBuilder.cs"
    click ChatCommandFactory "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Server/Factory/ChatCommandFactory.cs"
    click ReadMessageFactory "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Server/Factory/ReadMessageFactory.cs"
    click InMemoryTests "https://github.com/mquangcao/minichatapp/blob/master/MiniChatApp.Infrastructure.InMemoryTests/InMemoryChatRoomRepositoryTests.cs"
    click RepoTests "https://github.com/mquangcao/minichatapp/blob/master/RepositoryTests/ChatRoomRepositoryTests.cs"

    %% Styles
    classDef project fill:#ADD8E6,stroke:#000;
    classDef interface fill:#90EE90,stroke:#000;
    classDef factory fill:#FFFF99,stroke:#000;
    classDef database fill:#FFA500,stroke:#000;
```
