# francopachue/dleader-consul architecture

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

- Interactive diagram: https://gitdiagram.com/francopachue/dleader-consul
- Repository: https://github.com/francopachue/dleader-consul

## Overview

##### Main components
- DLeader.Consul Library:
- Abstractions: Interfaces like ILeaderElection and IMessageBroker which define contracts for leader election and messaging.
- Implementations: Concrete classes such as ConsulLeaderElection and ConsulMessageBroker that implement the required functionality.
- Configuration & Exceptions: Includes configuration options (ConsulOptions, ServiceRegistrationOptions) and custom exception handling.
- Extensions: Integration with dependency injection via ServiceCollectionExtensions to easily plug the library into .NET applications.
- Example Application (DLeader.Consul.Example):
- Controllers (HealthController) and background services (WorkerService, ScheduledTasksService, QueueProcessorService, DistributedCacheService) demonstrating how to use the library in an application context.
- Docker support with Dockerfile and docker-compose.yml to facilitate local testing.
- Test Project:
- Contains unit tests to validate the functions of the leader election, message brokering, and service registration behaviors.

##### External systems and integrations
- HashiCorp Consul: An external distributed system used for leader election, service discovery, and session renewal. This is critical to the library's core functionality.
- Dependency Injection and Logging: The design integrates with the typical .NET DI patterns and logging frameworks.
- CI/CD Infrastructure: The presence of a .github directory and workflow files (e.g., publish-nugget.yml) shows that automated testing and publishing are part of the pipeline.

##### Relationships and interactions
- The Example Application uses dependency injection to register the DLeader.Consul library services.
- The library abstracts the core logic via defined interfaces, and the implementations communicate with Consul.
- Configuration is externally defined (appsettings.json) and injected into the system under startup.
- The example’s background services (like MyService in the README sample) interact with the ILeaderElection abstraction to perform leader-specific tasks.
- The CI/CD pipeline validates code via the test project before integrating changes.

##### Architectural patterns & design principles
- Separation of concerns: Separate projects for library, example usage, and testing.
- Dependency Injection: Enhances testability and modularity, as seen in the ServiceCollectionExtensions.cs.
- Abstraction over external dependencies: Use of interfaces to abstract Consul interactions.
- Microservices principles: While not a full microservice implementation, the library’s design supports distributed systems and service discovery.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% CI/CD Pipeline and Test Project
    CI["CI/CD Pipeline\n(.github/workflows)"]:::ci
    TP["Test Project\n(DLeader.Consul.Tests)"]:::test

    CI -->|"triggers"| TP

    %% DLeader.Consul Library Subgraph
    subgraph "DLeader.Consul Library"
        LA["Abstractions\n(ILeaderElection, IMessageBroker)"]:::library
        LB["Implementations\n(ConsulLeaderElection, ConsulMessageBroker)"]:::library
        LC["Configuration\n(ConsulOptions, ServiceRegistrationOptions)"]:::library
        LD["Exceptions\n(ConsulException, LeadershipException, ServiceRegistrationException, SessionException)"]:::library
        LE["Extensions\n(ServiceCollectionExtensions)"]:::library
    end

    %% Example Application Subgraph
    subgraph "Example Application"
        EA["Controllers\n(HealthController)"]:::example
        EB["Example Services\n(WorkerService, ScheduledTasksService, QueueProcessorService, DistributedCacheService)"]:::example
        EC["Configuration\n(appsettings.json)"]:::example
    end

    %% External System
    CONSUL["HashiCorp Consul Server"]:::external

    %% Relationships
    EA -->|"uses"| LA
    EB -->|"uses"| LA
    EA -->|"integrates via"| LE
    EB -->|"integrates via"| LE
    EC -->|"configures"| LC
    LB -->|"communicates with"| CONSUL

    %% Test Project validations
    TP -->|"validates"| LA
    TP -->|"validates"| LB

    %% Click Events for Component Mapping
    click LA "https://github.com/francopachue/dleader-consul/tree/main/DLeader.Consul/Abstractions"
    click LB "https://github.com/francopachue/dleader-consul/tree/main/DLeader.Consul/Implementations"
    click LC "https://github.com/francopachue/dleader-consul/tree/main/DLeader.Consul/Configuration"
    click LD "https://github.com/francopachue/dleader-consul/tree/main/DLeader.Consul/Exceptions"
    click LE "https://github.com/francopachue/dleader-consul/blob/main/DLeader.Consul/Extensions/ServiceCollectionExtensions.cs"
    click EA "https://github.com/francopachue/dleader-consul/blob/main/DLeader.Consul.Example/Controllers/HealthController.cs"
    click EB "https://github.com/francopachue/dleader-consul/tree/main/DLeader.Consul.Example/ExampleServices"
    click EC "https://github.com/francopachue/dleader-consul/blob/main/DLeader.Consul.Example/appsettings.json"
    click TP "https://github.com/francopachue/dleader-consul/blob/main/DLeader.Consul.Tests"
    click CI "https://github.com/francopachue/dleader-consul/tree/main/.github/workflows"

    %% Class Styles
    classDef library fill:#f9c74f,stroke:#000,stroke-width:2px;
    classDef example fill:#90be6d,stroke:#000,stroke-width:2px;
    classDef external fill:#277da1,stroke:#000,stroke-width:2px,stroke-dasharray: 5,5;
    classDef test fill:#f3722c,stroke:#000,stroke-width:2px;
    classDef ci fill:#577590,stroke:#000,stroke-width:2px;
```
