How jgmelon22/externalapiconsumer works
Project Type and Purpose
• This is an ASP.NET Core Web API project built as a weekend experiment, whose main aim is to consume an external API (built in Java/Spring and hosted on Render).
• The project uses Refit to make type-safe HTTP calls, along with MediatR for handling commands/queries and Swagger for API documentation.
• It is structured using a layered (or onion/Clean Architecture-like) approach, separating API, Application, Core (Domain), and Infrastructure concerns.
Key Layers and Components
a. API Layer (Presentation):
• Located in “src/ExternalApiConsumer.API”.
• Contains Controllers (e.g., PeopleController.cs) and Program.cs (the application startup).
• Exposes endpoints which the client (or Swagger UI) interacts with.
b. Application Layer (Business Logic):
• Located in “src/ExternalApiConsumer.Application”.
• Contains Commands (e.g., CreatePersonCommand, DeletePersonCommand, SeedDataCommand) and Queries (e.g., GetPeopleQuery, GetPersonByIdQuery) along with their respective Handlers.
• Implements the Mediator pattern (via MediatR) to decouple handling logic.
c. Core/Domain Layer:
• Located in “src/ExternalApiConsumer.Core”.
• Defines the domain entities (e.g., Person), Data Transfer Objects (DTOs) for requests/responses, and mapping extensions.
• Encapsulates essential business rules and common/shared services.
d. Infrastructure Layer:
• Located in “src/ExternalApiConsumer.Infrastructure”.
• Contains interfaces and concrete service implementations for interacting with external systems – notably with the external API through the IExternalPersonApi interface (using Refit) and the IManagePersonService from which ManagePersonService is implemented.
• Acts as the bridge between the internal domain and external services.
e. Testing:
• The “tests” folder contains distinct test projects for the API, Application, and Infrastructure layers ensuring each component is validated.
Relationships and Interactions
• The API Layer (controller endpoints) receives client HTTP requests and delegates work to the Application Layer by sending commands or queries (typically via MediatR).
• The Application Layer uses Domain objects (from the Core layer) for business logic and validation.
• When external data is needed, the Application Layer calls into the Infrastructure Layer (via defined interfaces) where services like ManagePersonService use Refit to consume the external API.
• Data returned from the Infrastructure is then mapped back (using Mapping Extensions in Core) to DTOs which the API layer returns as HTTP responses.
• Incoming HTTP requests into the API Controller.
• The control flow from API → Application (commands/queries via MediatR).
• Application layer invoking Domain logic (business rules, validations).
• Application/Domain calling into Infrastructure to interact with external services.
• Data flow back through these layers before returning a response to the client.
Architectural Patterns & Design Principles
• Layered Architecture: separation into API, Application, Domain/Core, and Infrastructure.
• Command and Query Separation via MediatR, ensuring that business logic is encapsulated in handlers.
• Dependency Inversion: the Application and Domain layers rely on abstractions (interfaces defined in Infrastructure), and the concrete implementation is provided in the Infrastructure project.
• External API Consumption via Refit, which abstracts REST API calls in a type-safe way.
• Extensive Use of Dependency Injection (as standard in ASP.NET Core) to wire up these layers.