SimpleResults 0.1.1-alpha

This is a prerelease version of SimpleResults.
There is a newer version of this package available.
See the version list below for details.
dotnet add package SimpleResults --version 0.1.1-alpha
NuGet\Install-Package SimpleResults -Version 0.1.1-alpha
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="SimpleResults" Version="0.1.1-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleResults --version 0.1.1-alpha
#r "nuget: SimpleResults, 0.1.1-alpha"
#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 SimpleResults as a Cake Addin
#addin nuget:?package=SimpleResults&version=0.1.1-alpha&prerelease

// Install SimpleResults as a Cake Tool
#tool nuget:?package=SimpleResults&version=0.1.1-alpha&prerelease

SimpleResults

SimpleResults SimpleResults-AspNetCore

SimpleResults-logo

A simple library to implement the Result pattern for returning from services.

This library was inspired by Arcadis.Result.

Installation

Run the following command from the terminal:

dotnet add package SimpleResults --prerelease

Or you can also install the package for ASP.NET Core:

dotnet add package SimpleResults.AspNetCore --prerelease

Usage

This example is simple and is based on the EF Core introductory tutorial.

using SimpleResults;

public class BlogService
{
    private readonly BloggingContext _db;

    public BlogService(BloggingContext db)
    {
        _db = db;
    }

    public Result<CreatedId> Create(string url)
    {
        if(string.IsNullOrWhiteSpace(url))
        {
            return Result.Invalid();
        }

        var blog = new Blog { Url = "http://blogs.msdn.com/adonet" };
        _db.Add(blog);
        _db.SaveChanges();
        return Result.CreatedResource(blog.BlogId);
    }

    public Result<Blog> Read(int id)
    {
        if(id < 0)
        {
            return Result.Invalid("ID must not be negative");
        }

        var blog = _db.Blogs
            .Where(b => b.BlogId == id)
            .FirstOrDefault();

        if(blog is null)
        {
            return Result.NotFound();
        }

        return Result.Success(blog);
    }
}

This approach provides a new way to handle error without the need to use exceptions.

Integration with ASP.NET Core

You can convert the Result object to an ActionResult, such as:

using SimpleResults;

public class BlogRequest 
{ 
    public string Url { get; init; }
}

[ApiController]
[Route("[controller]")]
public class BlogController : ControllerBase
{
    private readonly BlogService _blogService;

    public BlogController(BlogService blogService)
    {
        _blogService = blogService;
    }

    [HttpGet("{id}")]
    public ActionResult<Result<Blog>> Get(int id)
    {
        return _blogService
            .Read(id)
            .ToActionResult();
    }

    [HttpPost]
    public ActionResult<Result<CreatedId>> Create([FromBody]BlogRequest request)
    {
        return _blogService
            .Create(request.Url)
            .ToActionResult();
    }
}

You can find a complete and functional example in these projects:

Contribution

Any contribution is welcome! Remember that you can contribute not only in the code, but also in the documentation or even improve the tests.

Follow the steps below:

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Added some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.
  • net7.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on SimpleResults:

Package Downloads
SimpleResults.AspNetCore

A simple package that translates the Result object to ActionResult or IResult.

SimpleResults.FluentValidation

A simple package that provides extension methods for the Fluent Validation library.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 1,369 3/17/2024
2.4.0 1,036 3/2/2024
2.3.2 938 12/30/2023
2.3.1 286 12/16/2023
2.3.0 151 12/10/2023
2.2.2 307 11/20/2023
2.2.1 189 11/12/2023
2.2.0 133 11/6/2023
2.1.0 244 10/27/2023
2.0.0 157 10/25/2023
1.1.0 131 10/24/2023
1.0.0 201 10/22/2023
0.5.0-alpha 105 10/21/2023
0.4.0-alpha 108 10/17/2023
0.3.0-alpha 142 10/16/2023
0.2.0-alpha 108 10/15/2023
0.1.2-alpha 95 10/13/2023
0.1.1-alpha 105 10/12/2023
0.1.0-alpha 120 10/12/2023