RA.Utilities.Core.Constants
A centralized and consistent set of core constants for the RA Utilities ecosystem. This package helps streamline development, improve code readability, and reduce "magic strings" and "magic numbers" by providing a single source of truth for common values.
π― Purposeβ
The primary goal of this package is to ensure consistency across different projects and services. By using these predefined constants, you can:
- Avoid Typos: Prevent hard-to-find bugs caused by typos in strings or numbers.
- Improve Readability: Make your code more self-documenting (e.g.,
HttpStatusCodes.NotFoundis clearer than404). - Simplify Maintenance: Update a constant in one place, and the change propagates everywhere it's used.
π οΈ Installationβ
You can install the package via the .NET CLI:
dotnet add package RA.Utilities.Core.Constants
Or through the NuGet Package Manager console:
Install-Package RA.Utilities.Core.Constants
Or through the NuGet Package Manager in Visual Studio.
β¨ Available Constantsβ
π Usage Examplesβ
Hereβs how you can use these constants within an ASP.NET Core controller to create clean and consistent API endpoints.
using Microsoft.AspNetCore.Mvc;
using RA.Utilities.Core.Constants; // Import the constants namespace
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet("{id}")]
public IActionResult GetProduct(int id)
{
var product = _productService.GetById(id);
if (product == null)
{
// Use constants for both the status code and the response message
return NotFound(ResponseMessages.NotFound);
}
return Ok(product);
}
[HttpPost]
public IActionResult CreateProduct([FromBody] Product newProduct)
{
if (!ModelState.IsValid)
{
// Use constants for a bad request
return BadRequest(ResponseMessages.BadRequest);
}
var createdProduct = _productService.Create(newProduct);
// Use constants for a 'Created' response
return StatusCode(HttpStatusCodes.Created, createdProduct);
}
}
ποΈ BaseResponseCode
The BaseResponseCode class is a static class within the RA.Utilities.Core.Constants package.
ποΈ BaseResponseMessages
The BaseResponseMessages class is a static class that provides a collection of predefined, constant string messages for common API responses.
ποΈ HeaderParameters
The HeaderParameters class is a static class that acts as a central repository for common HTTP header names used throughout your services.
ποΈ ResponseType
The ResponseType enum is a core component of the RA.Utilities ecosystem, designed to provide a standardized, machine-readable vocabulary for the outcomes of API operations.