MandalaConsulting.Objects.API 0.0.3

dotnet add package MandalaConsulting.Objects.API --version 0.0.3
                    
NuGet\Install-Package MandalaConsulting.Objects.API -Version 0.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MandalaConsulting.Objects.API" Version="0.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MandalaConsulting.Objects.API" Version="0.0.3" />
                    
Directory.Packages.props
<PackageReference Include="MandalaConsulting.Objects.API" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MandalaConsulting.Objects.API --version 0.0.3
                    
#r "nuget: MandalaConsulting.Objects.API, 0.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=MandalaConsulting.Objects.API&version=0.0.3
                    
Install MandalaConsulting.Objects.API as a Cake Addin
#tool nuget:?package=MandalaConsulting.Objects.API&version=0.0.3
                    
Install MandalaConsulting.Objects.API as a Cake Tool

MandalaConsulting.Objects.API

Overview

The MandalaConsulting.Objects.API library extends the functionality of MandalaConsulting.Objects by providing additional object models and utilities tailored for ASP.NET Core applications. It is designed to standardize and simplify the management of HTTP responses and interactions within ASP.NET Core APIs.


Features

  • ASP.NET Core Integration: Optimized for use with ASP.NET Core's HTTP request/response pipeline.
  • Standardized API Responses: Includes enhanced models like ApiResponse for consistent communication.
  • Flexible Data Structures: Easily adaptable to a wide variety of API use cases.
  • Lightweight and Reusable: Minimal overhead, designed for plug-and-play integration.

Installation

To use MandalaConsulting.Objects.API, include it in your .NET project and ensure the following packages are available:

  • Microsoft.AspNetCore.Http
  • MandalaConsulting.Objects

Usage

Import the Namespace

using MandalaConsulting.Objects.API;

Core Classes

ApiResponse

The ApiResponse class provides a structured format for HTTP API responses, encapsulating data, metadata, and error information.

Properties
  • StatusCode: The HTTP status code associated with the response.
  • Message: A descriptive message about the response.
  • Data: The main payload of the response.
  • Error: Details about any errors encountered.
Constructors
  1. Default Constructor

    ApiResponse response = new ApiResponse();
    
  2. Parameterized Constructor

    ApiResponse response = new ApiResponse(200, "Success", new { Id = 1, Name = "John Doe" }, null);
    
Example Usage
var response = new ApiResponse
{
    StatusCode = StatusCodes.Status200OK,
    Message = "Data retrieved successfully",
    Data = new { Id = 1, Name = "John Doe" },
    Error = null
};

// Serialize to JSON for API output
string jsonResponse = JsonSerializer.Serialize(response);

ErrorResponse

The ErrorResponse class is designed to encapsulate detailed error information in API responses.

Properties
  • StatusCode: The HTTP status code indicating the type of error.
  • Error: A descriptive error message or object.
  • Details: Additional details about the error, such as stack trace or validation errors.
Example Usage
var errorResponse = new ErrorResponse
{
    StatusCode = StatusCodes.Status400BadRequest,
    Error = "Invalid input data",
    Details = new { MissingFields = new[] { "Name", "Email" } }
};

// Serialize to JSON for API output
string errorJson = JsonSerializer.Serialize(errorResponse);

Middleware Integration

You can use MandalaConsulting.Objects.API to standardize error handling and responses in your ASP.NET Core middleware.

Example: Global Exception Handling
app.Use(async (context, next) =>
{
    try
    {
        await next();
    }
    catch (Exception ex)
    {
        var errorResponse = new ErrorResponse
        {
            StatusCode = StatusCodes.Status500InternalServerError,
            Error = "An unexpected error occurred",
            Details = ex.Message
        };

        context.Response.StatusCode = StatusCodes.Status500InternalServerError;
        context.Response.ContentType = "application/json";
        await context.Response.WriteAsync(JsonSerializer.Serialize(errorResponse));
    }
});

Requirements

  • .NET Core 3.1 or later
  • ASP.NET Core
  • Dependencies:
    • Microsoft.AspNetCore.Http
    • System.Text.Json (or any JSON serialization library)

License

This project is copyrighted © 2023 Mandala Consulting, LLC.
All Rights Reserved.


Author

Created by Alexander Fields
For inquiries, please contact Mandala Consulting.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.3 445 10/15/2024
0.0.1 145 10/12/2024