{"id":15039067,"url":"https://github.com/geocfu/reditus","last_synced_at":"2025-04-10T00:05:34.672Z","repository":{"id":237980129,"uuid":"789799365","full_name":"geocfu/Reditus","owner":"geocfu","description":"A Result pattern library for .NET.","archived":false,"fork":false,"pushed_at":"2024-11-16T17:00:19.000Z","size":109,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T00:05:20.813Z","etag":null,"topics":["csharp","dotnetcore","library","pattern","resultpattern"],"latest_commit_sha":null,"homepage":"https://github.com/geocfu/Reditus","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/geocfu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-21T15:33:27.000Z","updated_at":"2024-11-16T16:58:55.000Z","dependencies_parsed_at":"2024-09-17T20:49:07.371Z","dependency_job_id":null,"html_url":"https://github.com/geocfu/Reditus","commit_stats":null,"previous_names":["geocfu/reditus"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geocfu%2FReditus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geocfu%2FReditus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geocfu%2FReditus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geocfu%2FReditus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/geocfu","download_url":"https://codeload.github.com/geocfu/Reditus/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131319,"owners_count":21052819,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnetcore","library","pattern","resultpattern"],"created_at":"2024-09-24T20:41:26.340Z","updated_at":"2025-04-10T00:05:34.654Z","avatar_url":"https://github.com/geocfu.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Reditus\n\n[![NuGet Version](https://img.shields.io/nuget/v/Reditus)](https://www.nuget.org/packages/Reditus)\n[![NuGet Downloads](https://img.shields.io/nuget/dt/Reditus)](https://www.nuget.org/packages/Reditus)\n\nReditus, is a Result pattern library for every .NET application.\n\n## Getting Started\n\nYou can install [Reditus with NuGet](https://www.nuget.org/packages/Reditus):\n\n```text\n Install-Package Reditus\n```\n\n### Features\n\n- **Versatile** — Can be used in any .NET project.\n- **Immutable** — Once a Result is created, it cannot be changed.\n- **Detailed on Failure** — A Result, when failed, contains a specific Error class.\n- **With focus on DX** — Supports the implicit operator so it does not clutter your code.\n- **Thread safe** — Results are immutable and by nature, safe to work with in multithreaded scenarios.\n- **Extensible** — Extend the Result class or the Error class by introducing your very own classes.\n- **Fully tested** — The code has full coverage.\n\n## Usage\nThe `Result` object can be used as flow state control.\n\nThe `Result\u003cT\u003e` object can hold any value. A class, a\nvalue-type, a struct, anything.\n\n### Creating a Result\n\nTypically, the `Result` class is being used by methods that don't return a value.\n\n```csharp\nvar result = Result.CreateSuccess(); // creates a result in success state\n\nvar result = Result.CreatFail(); // creates a result in fail state\n\nvar error = new Error(\"An error occured.\"); // the error can also hold a message\nvar result = Result.CreatFail(error);\n\n// the error can also hold an exception\nvar error = new Error(\"An error occured.\", new Exception());\nvar result = Result.CreatFail(error);\n```\n\nAn example usage of the `Result` class.\n\n```csharp\npublic async Task\u003cResult\u003e ExecuteJob()\n{\n    try\n    {\n        var jobId = ExecuteJob();\n\n        if (jobId == 0)\n        {\n            // create an Error indicating the reason of failure\n            var error = new Error(\"Cleanup job was not executed.\");\n\n            return Result.CreateFail(error);\n        }\n\n        return Result.CreateSuccess();\n    }\n    catch (Exception ex)\n    {\n        // create an Error and attach the exception\n        var error = new Error(\"An unexpected error occured while trying execute Cleanup job.\", ex);\n\n        return Result.CreateFail(error);\n    }\n}\n```\n\nThe `Result` class also supports the implicit operator.\n\n```csharp\npublic async Task\u003cResult\u003e ExecuteJob()\n{\n    try\n    {\n        var jobId = ExecuteJob();\n\n        if (jobId == 0)\n        {\n            // create an Error indicating the reason of failure\n            var error = new Error(\"Cleanup job was not executed.\");\n\n            return error; // this implicitly is being converted into Result.CreateFail(error);\n        }\n\n        return Result.CreateSuccess(); // no implicit operator can be used since there is not value\n    }\n    catch (Exception ex)\n    {\n        // create an Error and attach the exception\n        var error = new Error(\"An unexpected error occured while trying execute Cleanup job.\", ex);\n\n        return error;\n    }\n}\n```\n\nThe `Result\u003cT\u003e` class is being used by methods that return a value.\n\n```csharp\nvar result = Result\u003cint\u003e.CreateSuccess(1); // creates a result in success state\n\nvar result = Result\u003cint\u003e.CreateFail(); // creates a result in fail state\n\n// the error can also hold a message\nvar error = new Error(\"An error occured.\");\nvar result = Result.CreateFail(error);\n\n// the error can also hold an exception\nvar error = new Error(\"An error occured.\", new Exception());\nvar result = Result\u003cint\u003e.CreateFail(error);\n```\n\nAn example usage of the `Result\u003cT\u003e` class.\n\n```csharp\npublic async Task\u003cResult\u003cint\u003e\u003e ExecuteJob()\n{\n    try\n    {\n        var jobId = ExecuteCleanupJob();\n\n        if (jobId == 0)\n        {\n            // create an Error indicating the reason of failure\n            var error = new Error(\"Cleanup job was not executed.\");\n\n            return Result\u003cint\u003e.CreateFail(error);\n        }\n\n        return Result\u003cint\u003e.CreateSuccess(jobId);\n    }\n    catch (Exception ex)\n    {\n        // create an Error and attach the exception\n        var error = new Error(\"An unexpected error occured while trying execute Cleanup job.\", ex);\n\n        return Result\u003cint\u003e.CreateFail(error);\n    }\n}\n```\n\nThe `Result\u003cT\u003e` class also supports the implicit operator.\n\n```csharp\npublic async Task\u003cResult\u003cint\u003e\u003e ExecuteJob()\n{\n    try\n    {\n        var jobId = ExecuteCleanupJob();\n\n        if (jobId == 0)\n        {\n            // create an Error indicating the reason of failure\n            var error = new Error(\"Cleanup job was not executed.\");\n\n            return error; // this implicitly is being converted into Result\u003cint\u003e.CreateFail(error);\n        }\n\n        return jobId; // this implicitly is being converted into Result\u003cint\u003e.CreateSuccess(error);\n    }\n    catch (Exception ex)\n    {\n        // create an Error and attach the exception\n        var error = new Error(\"An unexpected error occured while trying execute Cleanup job.\", ex);\n\n        return error;\n    }\n}\n```\n\n### The anatomy of a Result\n\nA `Result` holds certain information about itself.\n\n```csharp\nvar result = Result.CreateSuccess();\n\nresult.IsSuccessful // true\nresult.IsFailed // false\nresult.Error // throws InvalidOperationException as the result is not in a failed state\n\n\nvar result = Result.CreateFail();\n\nresult.IsSuccessful // false\nresult.IsFailed // true\nresult.Error // Error instance\n```\n\nWhen the `Result\u003cT\u003e` holds a return value.\n\n```csharp\nvar result = Result\u003cint\u003e.CreateSuccess(1);\n\nresult.IsSuccessful // true\nresult.IsFailed // false\nresult.Value // 1\nresult.Error // throws InvalidOperationException as the result is not in a fail state\n\n\nvar result = Result\u003cint\u003e.CreateFail();\n\nresult.IsSuccessful // false\nresult.IsFailed // true\nresult.Value // throws InvalidOperationException as the result is not in a success state\nresult.Error // IError instance\n```\n\n### Extending\n\nYou can introduce your very own Error classes by extending the existing one.\n\nThe below custom `NotFoundError` class is being used when an application might need to return a NotFound 404 response.\n\n```csharp\npublic interface ICustomError : IError\n{\n    public HttpStatusCode HttpStatusCode { get; }\n}\n\npublic sealed class NotFoundError : Error, ICustomError\n{\n    public HttpStatusCode HttpStatusCode =\u003e HttpStatusCode.NotFound;\n\n    public NotFoundError(string message)\n        : base(message)\n    {\n    }\n}\n```\n\nAn example of the above custom `Error` class.\n\n```csharp\npublic async Task\u003cResult\u003cIEnumerable\u003cProject\u003e\u003e\u003e GetProjects()\n{\n    try\n    {\n        var projects = await GetProjects();\n\n        if (!projects.Any())\n        {\n            var error = new NotFoundError(\"The request resource was not found.\"); // \u003c-- the new NotFoundError Error class\n\n            return error;\n        }\n\n        return jobId;\n    }\n    catch (Exception ex)\n    {\n        // create an Error and attach the exception\n        var error = new Error(\"An unexpected error occured while trying execute Cleanup job.\", ex);\n\n        return error;\n    }\n}\n```\n\nThe `Error` class provides many constructors, so you are free to use whichever suits your needs\nbest. [See definition](src/Reditus/Definitions/Error.cs)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeocfu%2Freditus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeocfu%2Freditus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeocfu%2Freditus/lists"}