Gleeman.EffectiveResult 8.2.0

dotnet add package Gleeman.EffectiveResult --version 8.2.0                
NuGet\Install-Package Gleeman.EffectiveResult -Version 8.2.0                
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="Gleeman.EffectiveResult" Version="8.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gleeman.EffectiveResult --version 8.2.0                
#r "nuget: Gleeman.EffectiveResult, 8.2.0"                
#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.
// Install Gleeman.EffectiveResult as a Cake Addin
#addin nuget:?package=Gleeman.EffectiveResult&version=8.2.0

// Install Gleeman.EffectiveResult as a Cake Tool
#tool nuget:?package=Gleeman.EffectiveResult&version=8.2.0                

Effective Result

Result pattern for returning from domain and services.

dotnet CLI

$ dotnet add package Gleeman.EffectiveResult

Result Usage

Result<User>.Failure(messages: errors);
Result<User>.Success(value:user);
Result.Failure(messages: errors);
Result.Success();

Response Usage

Response<User>.Unsuccessful(statusCode:404 , error: "Not found!");

Response<User>.Successful(values: result.Values, statusCode: 200);


Response.Successful(204, "User has been created successfully.");

Response.Unsuccessful(400, errors: result.Messages!);

Response Builder Usage

AbstractBuilder builder = new ResponseBuilder()
                             .SetMessage("Message")
                             .SetStatusCode(400)
                             .SetSuccessedOrFailed(false);



AbstractBuilder<Role> builder = new ResponseBuilder<Role>()
                                .SetStatusCode(200)
                                .SetSuccessedOrFailed(true)
                                .SetValues(_roles);

Example

Model

User

public class User
{
    private static List<User> _users = new();
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
    public string Email { get; private set; }


    public static Result<User> CreateUser(string firstName, string lastName, string email)
    {
        var errors = new List<string>();
        if (string.IsNullOrEmpty(firstName)) errors.Add("First name cannot be empty!");
        if (string.IsNullOrEmpty(lastName)) errors.Add("Last name cannot be empty!");
        if (string.IsNullOrEmpty(email)) errors.Add("Email cannot be empty!");

        if (errors.Count > 0)
            return Result<User>.Failure(messages: errors);

        User user = new()
        {
            FirstName = firstName,
            LastName = lastName,
            Email = email
        };

        _users.Add(user);
        return Result<User>.Success(user);
    }


    public static Result ChangeEmail(string email)
    {
        if (string.IsNullOrEmpty(email))
            return Result.Failure("Email cannot be empty!");
        return Result.Success();
    }


    public static Result<User> GetUsers() => Result<User>.Success(values: _users);

}

Role

public class Role
{
    public string Title { get; private set; }

    private static List<Role> _roles = new();

    public Role(string title)
    {
        Title = title;
    }

    public static IResponse CreateRole(Role role)
    {
        AbstractBuilder builder = new ResponseBuilder();

        if (string.IsNullOrEmpty(role.Title))
        {
            builder = builder.SetMessage("Title cannot be null!")
                             .SetStatusCode(400)
                             .SetSuccessedOrFailed(false);
        }
        else
        {
            _roles.Add(role);
            builder = builder.SetMessage("Role has been added.")
                             .SetStatusCode(400)
                             .SetSuccessedOrFailed(true);
        }

        Response result = builder.Build;
        return result;

    }

    public static IResponse<Role>GetRoles()
    {
        AbstractBuilder<Role> builder = new ResponseBuilder<Role>();

        if(_roles.Count == 0)
        {
            builder = builder.
                              SetMessage("There is no any role!")
                             .SetStatusCode(404)
                             .SetSuccessedOrFailed(false)
                             .SetValues(_roles);
        }
        else
        {
            builder = builder
                             .SetStatusCode(200)
                             .SetSuccessedOrFailed(true)
                             .SetValues(_roles);
        }

        var result = builder.Build;
        return result;

    }

Service

public class UserService : IUserService
{
    public IResponse CreateUser(UserDto user)
    {
        var result = User.CreateUser(user.FirstName, user.LastName, user.Email);

        if (!result.IsSuccess)
            return Response.Unsuccessful(404, errors: result.Messages!);
        
        return Response.Successful(200, "User has been created successfully.");
    }

    public IResponse<User> GetUsers()
    {
        var result = User.GetUsers();

        if (result.Values!.Count() == 0)
            return Response<User>.Unsuccessful(statusCode: 404, error: "There is no any user!");

        return Response<User>.Successful(values: result.Values!, statusCode: 200);
    }
}

Controller

UserController

[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
    private readonly IUserService userService;

    public UserController(IUserService userService)
    {
        this.userService = userService;
    }

    [HttpPost]
    public IActionResult CreateUser([FromBody] UserDto user)
    {
        var response = userService.CreateUser(user);
        return response.IsSuccess ? Ok(response) : BadRequest(response.Messages);
    }

    [HttpGet]
    public IActionResult GetUsers()
    {
        IResponse<User> response = userService.GetUsers();

        return response.IsSuccess ? Ok(response) : NotFound(response);
    }
}

RoleController

[Route("api/[controller]")]
[ApiController]
public class RoleController : ControllerBase
{
    [HttpPost]
    public IActionResult CreateRole([FromBody] RoleDto roleDto)
    {
        var role = new Role(roleDto.Title);
        var response = Role.CreateRole(role);
        return response.IsSuccess ? Ok(response) : BadRequest(response);
    }

    [HttpGet]
    public IActionResult GetRoles()
    {
        var response = Role.GetRoles();
        return response.IsSuccess ? Ok(response) : NotFound(response);
    }
}
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. 
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.2.0 134 2/3/2024
8.1.0 105 2/2/2024
8.0.0 122 2/1/2024
7.2.0 104 2/3/2024
7.1.0 104 2/2/2024
7.0.2 104 2/1/2024
7.0.1 234 11/4/2023
7.0.0 126 11/3/2023
6.0.1 125 11/4/2023

Response results  was added