FG.Result 8.0.2

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

FG.Result - .NET API Response Structure

NuGet

FG.Result is a lightweight and easy-to-use .NET library that simplifies handling API responses by providing consistent and structured Response<T> and Response classes. It encapsulates success/failure status, data (optional), error messages, and HTTP status codes, promoting cleaner code and better error handling. This eliminates the need for custom response classes for every API endpoint.

Features

  • Generic and Non-Generic Responses:
    • Response<T>: For operations returning data of a specific type T.
    • Response: For operations not returning data (only success/failure and optional messages).
  • Static Factory Methods: Uses clear and concise static methods (Succeed, Fail, SucceedCreate, SucceedUpdate, SucceedDelete, SucceedRemove) for creating response objects.
  • Multiple Error Messages: Supports a List<string> for detailed error reporting.
  • HTTP Status Codes: Includes an int StatusCode property for standard HTTP status code integration.
  • Fluent API (Optional): With... methods for easy modification of response objects.
  • Lightweight: No external dependencies.
  • Fully Documented: XML documentation comments for excellent IntelliSense support in Visual Studio.

Installation

NuGet Package Manager Console:

Install-Package FG.Result  # Replace with your actual PackageId

.NET CLI

dotnet add package FG.Result  # Replace with your actual PackageId

Usage Examples Response<T> (Generic - For Responses WITH Data)

using TS.Result; // Replace with your actual namespace
using System.Net;

public class UserService
{
    public Response<User> GetUser(int id)
    {
        if (id <= 0)
        {
            return Response<User>.Fail("Invalid user ID", (int)HttpStatusCode.BadRequest);
        }

        // Simulate fetching a user (replace with your actual data access logic)
        User? user = GetUserFromDatabase(id);

        if (user == null)
        {
            return Response<User>.Fail("User not found", (int)HttpStatusCode.NotFound);
        }

        return Response<User>.Succeed(user);
    }

    public Response<User> CreateUser(User user)
    {
        if (user is null)
        {
            return Response<User>.Fail("User cannot be null.", (int)HttpStatusCode.BadRequest);
        }
        // ... (validation and database logic here) ...
        return Response<User>.Succeed(user, (int)HttpStatusCode.Created); //201
    }


    // Simulate database interaction (replace with your actual data access)
    private User? GetUserFromDatabase(int id)
    {
        if (id == 1)
        {
            return new User { Id = 1, Name = "Test User" };
        }
        return null;
    }
}

// Example User class (you'll likely have your own model classes)
public class User
{
    public int Id { get; set; }
    public string? Name { get; set; }
}

Response (Non-Generic - For Responses WITHOUT Data)

using TS.Result; // Replace with your actual namespace
using System.Net;

public class SettingsService
{
    public Response UpdateSetting(string key, string value)
    {
        if (string.IsNullOrEmpty(key))
        {
            return Response.Fail("Setting key cannot be empty", (int)HttpStatusCode.BadRequest);
        }

        // Simulate updating a setting (replace with your actual logic)
        bool success = UpdateSettingInDatabase(key, value);

        if (!success)
        {
          return Response.Fail("Failed to update setting", (int)HttpStatusCode.InternalServerError);
        }
        return Response.Succeed("Setting updated successfully"); //Custom Message
    }

    public Response DeleteSetting(string key)
    {
        // ... (your deletion logic here) ...
       return Response.SucceedDelete<string>(); // Example for delete
    }

    //Simulate database
    private bool UpdateSettingInDatabase(string key, string value)
    {
        // In a real application, you would update the setting in a database or configuration file.
        return true; // Simulate success
    }
}

CRUD Operations

using TS.Result;  // Replace with YOUR namespace

public class ProductService
{
    public Response CreateProduct(Product product)
    {
        // ... validation and database logic ...
        return Response.SucceedCreate<Product>();  // Returns "Product is created" in Data
    }

    public Response<Product> GetProduct(int id)
    {
        // ... database logic to retrieve product ...
        var product = new Product { Id = id, Name = "Example Product" }; // Replace with actual retrieval
        return Response<Product>.Succeed(product);
    }

    public Response UpdateProduct(Product product)
    {
        // ... validation and database logic ...
        return Response.SucceedUpdate<Product>(); // Returns "Product is updated" in Data

    }

    public Response DeleteProduct(int id)
    {
        // ... database logic ...
        return Response.SucceedDelete<Product>(); // Returns "Product is deleted" in Data
    }

     public Response RemoveProduct(int id)
    {
        // ... database logic ...
        return Response.SucceedRemove<Product>(); // Returns "Product is removed" in Data
    }
}

// Example Product class
public class Product
{
    public int Id { get; set; }
    public string? Name { get; set; }
}

Fluent API (Optional)


using TS.Result;
using System.Net;

var response = Response.Fail("Validation failed")
    .WithStatusCode((int)HttpStatusCode.BadRequest)
    .WithError("Name is required")
    .WithError("Email is invalid");

// Accessing the values:
if (!response.Success)
{
    Console.WriteLine(<span class="math-inline">"Status Code\: \{response\.StatusCode\}"\);  // Output\: 400
foreach \(var error in response\.Errors\)
\{
Console\.WriteLine\(</span>"Error: {error}"); // Output: Error: Name is required, Error: Email is invalid
    }
}

Class Structures

Response<T> (Generic)

public class Response<T>
{
    public T? Data { get; private set; }
    public List<string>? Errors { get; private set; }
    public bool Success { get; private set; }
    public int StatusCode { get; private set; }
    // ... (other members) ...
}

Response (Non-Generic)

public class Response
{
    public object? Data { get; private set; }  // Use object? for non-generic responses
    public List<string>? Errors { get; private set; }
    public bool Success { get; private set; }
    public int StatusCode { get; private set; }
    // ... (other members) ...
}

Contributing Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository (replace with your actual repository URL).

License This project is licensed under the MIT License. See the LICENSE file for details.

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.
  • net8.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
8.0.2 98 2/11/2025