Appco.ServiceProtocol 1.0.2

dotnet add package Appco.ServiceProtocol --version 1.0.2
                    
NuGet\Install-Package Appco.ServiceProtocol -Version 1.0.2
                    
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="Appco.ServiceProtocol" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Appco.ServiceProtocol" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Appco.ServiceProtocol" />
                    
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 Appco.ServiceProtocol --version 1.0.2
                    
#r "nuget: Appco.ServiceProtocol, 1.0.2"
                    
#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 Appco.ServiceProtocol@1.0.2
                    
#: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=Appco.ServiceProtocol&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Appco.ServiceProtocol&version=1.0.2
                    
Install as a Cake Tool

Appco.ServiceProtocol

A library for standardized service responses in .NET applications, providing consistent status and content handling for API and service layers.

Installation

Install via NuGet Package Manager: Install-Package Appco.ServiceProtocol

Or using the .NET CLI: dotnet add package Appco.ServiceProtocol

Requirements

  • .NET 9 or later

Usage

Import the namespace in your service or controller: using Appco.ServiceProtocol;

Exemples

Service class

/// Represents a simple resource with a name property.
/// Used as an example entity for service operations.
public class ExempleResource
{
    public string Name { get; set; } = string.Empty;
}

/// Example service demonstrating basic resource management operations.
/// This class provides sample implementations for adding, retrieving, and checking resources.
public class ExempleService : BaseService
{
    // In-memory list to store example resources.
    private readonly List<ExempleResource> _resources = [];

    /// Adds a new resource to the service.
    /// Demonstrates validation and creation response.
    public ServiceResponse<int> AddResource(ExempleResource resource)
    {
        // Example of input validation: checks if the resource name is empty.
        if (string.IsNullOrWhiteSpace(resource.Name))
            return InvalidSchema<int>("Empty name.");

        _resources.Add(resource);

        // Returns a created response with the total number of resources.
        return Created("Resource added successfully.", _resources.Count);
    }

    /// Retrieves a resource by its index.
    /// Demonstrates not found and success responses.
    public ServiceResponse<ExempleResource> GetById(int id)
    {
        // Example of bounds checking: returns not found if the index is out of range.
        if (id > _resources.Count)
            return NotFound<ExempleResource>("Id not found.");

        // Returns the found resource.
        return Ok("Resource found successfully.", _resources[id]);
    }

    /// Checks if a resource exists by its index.
    /// Demonstrates not found and success responses without returning the resource.
    public ServiceResponse IdExists(int id)
    {
        // Example of existence check: returns not found if the index is out of range.
        if (id > _resources.Count)
            return NotFound("Id not found.");

        // Returns a success response if the resource exists.
        return Ok($"Resource number {id} found");
    }

    public ServiceResponse Update(int id, ExempleResource resource)
    {
        _resources[id] = resource;
        return Updated("Resource updated successfully.");
    }
}

Controller adapter (based on Microsoft.AspNetCore.Mvc)


[ApiController]
public class ExempleController(ExempleService service) : ControllerBase
{
    private readonly ExempleService _service = service;

	[HttpPost("/exemple")]
	public ObjectResult AddResource([FromBody] ExempleResource resource) =>
		Adapt(_service.AddResource(resource));

	[HttpGet("/exemple/{id}/exists")]
	public ObjectResult IdExists(int id) => Adapt(_service.IdExists(id));

	[HttpGet("/exemple/{id}")]
	public ObjectResult GetById(int id) => Adapt(_service.GetById(id));

	[HttpPut("/exemple/{id}")]
	public ObjectResult Update(int id, [FromBody] ExempleResource resource)
	{
		var checkIdExistsResponse = _service.IdExists(id);

		if (!checkIdExistsResponse.Status.Success())
			return Adapt(checkIdExistsResponse);

		return Adapt(_service.Update(id, resource));
	}

	public ObjectResult Adapt<TContent>(ServiceResponse<TContent> serviceResponse)
	{
		return StatusCode(serviceResponse.Status.HttpStatusCode(), new
		{
			StatusMessage = serviceResponse.Status.Message,
			serviceResponse.Content,
		});
	}

	public ObjectResult Adapt(ServiceResponse serviceResponse)
	{
		return StatusCode(serviceResponse.Status.HttpStatusCode(), new
		{
			StatusMessage = serviceResponse.Status.Message,
		});
	}
}

License

This project is licensed under the MIT License.

Support

For questions or issues, please open an issue on the repository or contact the maintainers.

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.
  • net9.0

    • No dependencies.

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
1.0.2 699 10/7/2025
1.0.1 236 9/25/2025
1.0.0 202 9/24/2025