The Butterfly Calculator is a simple Web API for performing basic maths operations like addition, subtraction, multiplication, and division. This project demonstrates modern software development approaches and technologies while keeping things clear and maintainable.
This project is being used by Butterfly.Healthcare to evaluate the technical skills of candidates during the recruitment process.
- Supports core maths operations (
add
,subtract
,multiply
,divide
). - Clean code principles with separation of responsibilities.
- Structured logging using Serilog.
- Input validation with helpful error messages.
- Robust exception handling for a smoother user experience.
- Follows RESTful API design principles.
- Includes unit tests for core business logic.
-
ASP.NET Core 9/3.1
- Lightweight, cross-platform framework for web APIs.
- API endpoint defined using
ControllerBase
.
-
Entity Mapping:
- AutoMapper for clean and efficient model mapping.
-
Logging:
- Serilog for structured logging and flexible output.
-
Dependency Injection:
- Built-in DI for adding services like
ICalculationService
andILogger
.
- Built-in DI for adding services like
-
Exception Handling:
- Handles errors gracefully using try-catch blocks.
-
Validation:
- Checks input parameters and provides user-friendly feedback.
-
Unit Testing:
- XUnit is used to test key functionality of the application.
-
Clear Responsibilities:
- Validation logic is separate from business logic (
ValidateParameters
method). - The controller is responsible for routing and calling services.
- Validation logic is separate from business logic (
-
Structured Logging:
- Log messages include details like method names, inputs, and errors for easy debugging.
-
Error Handling:
- Provides meaningful error messages for invalid inputs or unexpected issues.
-
Async Programming:
- Uses
async/await
to handle requests efficiently.
- Uses
-
Test-Driven Development:
- Core logic is covered with unit tests to ensure reliability and reduce regression.
Follow these steps to set up and run the project on your local machine.
- .NET 9 SDK
- Visual Studio Code or your favourite code editor.
- Command-line interface (CLI) for running the app.
-
Clone the Repository:
git clone https://github.com/yourusername/Butterfly.Calculator.git cd Butterfly.Calculator
-
Restore Dependencies:
dotnet restore
-
Build the Project:
dotnet build
-
Run the Application:
dotnet run --project Butterfly.Calculator.WebApi
-
Access the API:: Open your browser or a tool like Postman and use:
https://localhost:5001/calculation/calculate?addend1=10&addend2=5&operation=add
The project includes unit tests to ensure the accuracy and reliability of its core functionality. These tests validate:
- Correct results for all mathematical operations (add, subtract, multiply, divide).
- Validation errors for invalid inputs (e.g., division by zero, invalid operations).
- Edge cases to confirm robustness.
XUnit: A popular testing framework for .NET applications.
Navigate to the Test Directory:
cd Butterfly.Calculator.Tests
Run the unit tests:
dotnet test
Test results will be displayed in the terminal, showing whether all test cases passed.