Skip to main content

RA.Utilities.Core.Constants

NuGet version Codecov GitHub license NuGet Downloads

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.NotFound is clearer than 404).
  • 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);
}
}