MX.Api.Abstractions 2.0.185.1

dotnet add package MX.Api.Abstractions --version 2.0.185.1
                    
NuGet\Install-Package MX.Api.Abstractions -Version 2.0.185.1
                    
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="MX.Api.Abstractions" Version="2.0.185.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MX.Api.Abstractions" Version="2.0.185.1" />
                    
Directory.Packages.props
<PackageReference Include="MX.Api.Abstractions" />
                    
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 MX.Api.Abstractions --version 2.0.185.1
                    
#r "nuget: MX.Api.Abstractions, 2.0.185.1"
                    
#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.
#:package MX.Api.Abstractions@2.0.185.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MX.Api.Abstractions&version=2.0.185.1
                    
Install as a Cake Addin
#tool nuget:?package=MX.Api.Abstractions&version=2.0.185.1
                    
Install as a Cake Tool

MX.Api.Abstractions

Core abstractions library providing standardized models and interfaces for API handling in .NET applications. This package serves as the foundation for building consistent APIs and API clients that follow proven design patterns.

Installation

dotnet add package MX.Api.Abstractions

Key Features

  • Standardized API Response Models - Consistent response structures across all API operations
  • Collection Models - Uniform handling of result sets with pagination support
  • HTTP Response Wrappers - Separation of HTTP concerns from business logic
  • Error Handling Models - Structured error reporting with detailed information
  • Filtering Support - OData-like query parameters for flexible data retrieval

Core Models

ApiResponse

For operations that don't return data (DELETE, status checks, validation):

var response = new ApiResponse(); // Success
var errorResponse = new ApiResponse(new ApiError("VALIDATION_FAILED", "Invalid data provided"));

ApiResponse<T>

For operations that return data:

var user = new User { Id = 1, Name = "John Doe" };
var response = new ApiResponse<User>(user);

// With errors
var errorResponse = new ApiResponse<User>(
    new ApiError("USER_NOT_FOUND", "User not found"));

CollectionModel<T>

For paginated collections:

var collection = new CollectionModel<Product>
{
    Items = products,
    TotalCount = 100,
    FilteredCount = 10,
    Pagination = new ApiPagination
    {
        Page = 1,
        PageSize = 10,
        TotalPages = 10
    }
};

var response = new ApiResponse<CollectionModel<Product>>(collection);

ApiResult<T>

HTTP response wrapper (used by client libraries):

var apiResult = new ApiResult<User>(HttpStatusCode.OK, apiResponse);

// Check response status
if (apiResult.IsSuccess)
{
    var user = apiResult.Result?.Data;
}

if (apiResult.IsNotFound) 
{
    // Handle not found
}

Error Handling

ApiError Model

var error = new ApiError(
    code: "VALIDATION_ERROR",
    message: "The request data is invalid",
    detail: "Name field is required and cannot be empty");

Multiple Errors

var errors = new[]
{
    new ApiError("REQUIRED_FIELD", "Name is required"),
    new ApiError("INVALID_FORMAT", "Email format is invalid")
};

var response = new ApiResponse<User>(errors);

Filtering and Pagination

FilterOptions

var filters = new FilterOptions
{
    Filter = "category eq 'Electronics'",
    Select = "id,name,price",
    OrderBy = "name asc",
    Skip = 0,
    Take = 10
};

ApiPagination

var pagination = new ApiPagination
{
    Page = 2,
    PageSize = 25,
    TotalPages = 8
};

Integration with Other Libraries

This package is designed to work with:

  • MX.Api.Client - For building resilient API clients
  • MX.Api.Web.Extensions - For ASP.NET Core integration

Usage Patterns

In API Controllers

[HttpGet("{id}")]
public IActionResult GetUser(int id)
{
    var user = _userService.GetUser(id);
    
    if (user == null)
    {
        var errorResponse = new ApiResponse<User>(
            new ApiError("USER_NOT_FOUND", $"User {id} not found"));
        return NotFound(errorResponse);
    }
    
    var response = new ApiResponse<User>(user);
    return Ok(response);
}

In API Clients

public async Task<ApiResult<User>> GetUserAsync(int id)
{
    var httpResponse = await _httpClient.GetAsync($"users/{id}");
    var content = await httpResponse.Content.ReadAsStringAsync();
    var apiResponse = JsonConvert.DeserializeObject<ApiResponse<User>>(content);
    
    return new ApiResult<User>(httpResponse.StatusCode, apiResponse);
}

Documentation


**Use this when:**
- GET operations that return resources
- POST operations that return created resources  
- PUT/PATCH operations that return updated resources
- Any operation where a data payload is expected

**Note:** The HTTP status code is handled by the `ApiResult` at the transport layer, keeping the API response model focused on business data.

ApiError

The ApiError class provides a standardized format for API errors:

public class ApiError
{
    public string Code { get; set; } = string.Empty;
    public string Message { get; set; } = string.Empty;
    public string? Target { get; set; }
    public ApiError[]? Details { get; set; }
}

ApiPagination

The ApiPagination class provides standardized pagination information:

public class ApiPagination
{
    public int TotalCount { get; set; }
    public int FilteredCount { get; set; }
    public int Skip { get; set; }
    public int Top { get; set; }
    public bool HasMore { get; set; }
}

CollectionModel<T>

The CollectionModel<T> class provides a standardized container for collections of resources:

public class CollectionModel<T>
{
    public List<T> Items { get; set; } = new List<T>();
}

FilterOptions

The FilterOptions class provides standardized options for filtering API responses:

public class FilterOptions
{
    public string? FilterExpression { get; set; }
    public string[]? Select { get; set; }
    public string[]? Expand { get; set; }
    public string? OrderBy { get; set; }
    public int Skip { get; set; }
    public int Top { get; set; }
    public bool Count { get; set; }
}

ApiResult

The ApiResult class wraps API responses with HTTP-specific information, providing separation between transport concerns and business data. This class works specifically with non-generic ApiResponse objects:

public class ApiResult : IApiResult
{
    public ApiResponse? Result { get; set; }
    public HttpStatusCode StatusCode { get; set; }
    
    // Helper properties
    public bool IsSuccess { get; }
    public bool IsNotFound { get; }
    public bool IsConflict { get; }
}

Use this when:

  • Wrapping API responses that don't return typed data (e.g., DELETE operations, status checks)
  • Working with validation endpoints that only return success/failure status
  • Handling responses where no specific data payload type is expected

ApiResult<T>

The ApiResult<T> class wraps strongly-typed API responses with HTTP-specific information, providing separation between transport concerns and business data. This class works specifically with generic ApiResponse<T> objects:

public class ApiResult<T> : ApiResult, IApiResult<T>
{
    public new ApiResponse<T>? Result { get; set; }
    public HttpStatusCode StatusCode { get; set; }
    
    // Helper properties  
    public new bool IsSuccess { get; } // Requires both successful status code AND non-null Result
    public bool IsNotFound { get; }
    public bool IsConflict { get; }
}

Use this when:

  • Wrapping API responses that return typed data (e.g., GET, POST, PUT operations)
  • Working with endpoints that return specific resource types
  • Handling responses where a strongly-typed data payload is expected

Key differences:

  • ApiResult uses ApiResponse (non-generic) for responses without typed data
  • ApiResult<T> uses ApiResponse<T> (generic) for responses with typed data
  • Generic wrapper's IsSuccess property requires both successful HTTP status code AND non-null Result
  • Non-generic wrapper's IsSuccess property only depends on HTTP status code

Key benefits:

  • Separates HTTP transport concerns from business data models
  • Provides status code information at the transport layer
  • Offers convenience properties for common status checks
  • Enables clean API response models focused on data
  • Type-safe handling of different response scenarios

## Usage Examples

### Creating API Responses

```csharp
// Non-data response (e.g., DELETE operation)
var deleteResponse = new ApiResponse();

// Non-data error response
var errorResponse = new ApiResponse
{
    Errors = new[]
    {
        new ApiError("INVALID_REQUEST", "The request is invalid")
    }
};

// Success response with data
var successResponse = new ApiResponse<User>
{
    Data = user
};

// Error response with data context
var dataErrorResponse = new ApiResponse<User>
{
    Errors = new[]
    {
        new ApiError("INVALID_USERNAME", "Username is invalid", "username")
    }
};

// Collection response with pagination
var collectionResponse = new ApiResponse<CollectionModel<User>>
{
    Data = new CollectionModel<User>
    {
        Items = users
    },
    Pagination = new ApiPagination
    {
        TotalCount = 100,
        FilteredCount = 10,
        Skip = 0,
        Top = 10,
        HasMore = true
    }
};

// HTTP Response Wrapper for non-data response (e.g., DELETE operation)
var deleteHttpResponse = new ApiResult
{
    StatusCode = HttpStatusCode.NoContent,
    Result = new ApiResponse()
};

// HTTP Response Wrapper for typed data response  
var httpResponse = new ApiResult<User>
{
    StatusCode = HttpStatusCode.OK,
    Result = successResponse
};

// Alternative constructor usage
var createdResponse = new ApiResult<User>(
    HttpStatusCode.Created, 
    new ApiResponse<User> { Data = newUser }
);

var notFoundResponse = new ApiResult(HttpStatusCode.NotFound);

Working with Filter Options

// Create filter options for an API request
var filter = new FilterOptions
{
    FilterExpression = "username eq 'john' and active eq true",
    OrderBy = "created desc",
    Skip = 0,
    Top = 20,
    Select = new[] { "username", "email", "firstName", "lastName" },
    Expand = new[] { "roles", "permissions" }
};
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on MX.Api.Abstractions:

Package Downloads
MX.GeoLocation.LookupApi.Abstractions

This package provides an abstraction layer for interfaces and models for the GeoLocation service.

XtremeIdiots.Portal.Repository.Abstractions.V1

V1 Abstractions for the XtremeIdiots Portal Repository API.

XtremeIdiots.Portal.Repository.Api.Client.V1

Versioned client for the XtremeIdiots Portal Repository API V1.

XtremeIdiots.Portal.Repository.Api.Client.V2

Versioned client for the XtremeIdiots Portal Repository API V2.

XtremeIdiots.Portal.Repository.Abstractions.V2

V2 Abstractions for the XtremeIdiots Portal Repository API.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.185.1 151 9/4/2025
2.0.184.1 250 8/28/2025
2.0.183.1 223 8/26/2025
2.0.182.1 929 8/23/2025
2.0.181.1 86 8/22/2025
2.0.180.1 727 8/21/2025
2.0.179.1 295 8/20/2025
2.0.178.1 141 8/20/2025
2.0.177.1 896 8/18/2025
2.0.176.1 161 8/11/2025
2.0.175.1 354 8/4/2025
2.0.174.1 223 7/28/2025
2.0.173.1 306 7/21/2025
2.0.172.1 180 7/14/2025
2.0.171.1 629 7/8/2025
2.0.170.1 206 7/8/2025
2.0.169.1 148 7/8/2025
2.0.168.1 151 7/8/2025
2.0.167.1 186 7/7/2025
2.0.166.1 154 7/7/2025
2.0.165.1 258 7/6/2025
2.0.164.1 180 7/6/2025
2.0.163.1 142 7/6/2025
2.0.162.1 143 7/6/2025
2.0.161.1 146 7/5/2025
2.0.160.1 98 7/5/2025
2.0.159.1 101 7/5/2025
2.0.158.1 83 7/5/2025
2.0.157.1 81 7/5/2025
2.0.156.1 82 7/5/2025
2.0.155.1 89 7/5/2025
2.0.154.1 84 7/5/2025
2.0.153.1 85 7/5/2025
2.0.152.1 86 7/5/2025
2.0.151.1 88 7/5/2025