This repository contains a small .NET8 solution structured to demonstrate clean layering, basic domain logic (Collatz sequence generation), and accompanying automated tests.
/README.md
/src
/Collatz.Core -> Core library containing domain abstractions & implementations
/Collatz.Api -> ASP.NET Core Web API exposing Collatz functionality
/Collatz.Core.UnitTests -> Unit tests for the core library
/Collatz.Api.IntegrationTests-> Integration tests for the API layer
-
Collatz.Core- Provides the domain model for generating Collatz sequences.
- Exposes a generic interface
ICollatzSequenceGenerator<T>with two concrete implementations: ULongCollatzSequenceGenerator(fast path for numbers that fit inulong).BigIntegerCollatzSequenceGenerator(arbitrary length integers viaBigInteger).- Includes DI extension methods (
AddCollatz,AddULongCollatz,AddBigIntegerCollatz).
-
Collatz.Api- ASP.NET Core minimal hosting model.
- Registers the generators plus an internal application service (
CollatzService) that: - Validates string input representing a positive integer.
- Chooses the appropriate generator (tries
ulong, falls back toBigInteger). CollatzControllerexposes a single GET endpoint:/Collatz/{value}returning the sequence as an array of strings.
-
Collatz.Core.UnitTests- Uses xUnit to validate correctness, edge cases, and error handling of the core generators.
-
Collatz.Api.IntegrationTests- Uses
Microsoft.AspNetCore.Mvc.Testingto spin up an in-memory test server and verify end?to?end API behavior.
- Uses
- .NET8 SDK (https://dotnet.microsoft.com/)
- (Optional) IDE such as Visual Studio2022, VS Code, or JetBrains Rider.
Verify installation:
dotnet --version
Should report 8.x.
From the repository root:
dotnet restore
dotnet build
Run all tests:
dotnet test
dotnet run --project src/Collatz.Api
The API will start (default Kestrel ports). In development, Swagger UI is enabled at:
https://localhost:xxxxx/swagger
GET https://localhost:xxxxx/Collatz/27
Response (truncated example):
["27","82","41", ... , "1"]
For very large values:
GET https://localhost:xxxxx/Collatz/18446744073709551616
The service automatically chooses the BigInteger path as needed.
- Separation of concerns: API contains only transport & orchestration; domain logic lives in
Collatz.Core. - Generic abstraction allows future numeric type extensions without changing API surface.
- Dependency Injection: explicit service registrations via
AddCollatz()extension for clarity and testability. - Validation: input accepted only as a digit string > 0; sequences always terminate at
1per conjecture definition.