SimpleResults.AspNetCore 0.1.1-alpha

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

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.  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.

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
3.0.1 1,050 11/30/2024
3.0.0 6,125 3/17/2024
2.4.0 856 3/2/2024
2.3.2 1,474 12/30/2023
2.3.1 273 12/19/2023
2.3.0 158 12/10/2023
2.2.2 318 11/20/2023
2.2.1 239 11/12/2023
2.2.0 162 11/6/2023
2.1.0 298 10/27/2023
2.0.0 162 10/25/2023
1.1.0 135 10/24/2023
1.0.0 303 10/22/2023
0.5.0-alpha 132 10/21/2023
0.4.0-alpha 101 10/17/2023
0.3.0-alpha 203 10/16/2023
0.2.0-alpha 114 10/15/2023
0.1.2-alpha 101 10/13/2023
0.1.1-alpha 113 10/12/2023
0.1.0-alpha 105 10/12/2023