{"id":37052013,"url":"https://github.com/toolsfactory/toolsfactory.common.result","last_synced_at":"2026-01-14T06:00:37.113Z","repository":{"id":266729427,"uuid":"898668761","full_name":"toolsfactory/Toolsfactory.Common.Result","owner":"toolsfactory","description":"A small lib to simplify error handling without exceptions","archived":false,"fork":false,"pushed_at":"2024-12-19T08:59:31.000Z","size":53,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-07T19:28:49.673Z","etag":null,"topics":["error","errorhandling","exceptions","result"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/Toolsfactory.Common.Result","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/toolsfactory.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-12-04T20:10:44.000Z","updated_at":"2024-12-19T08:58:47.000Z","dependencies_parsed_at":"2024-12-05T20:33:39.957Z","dependency_job_id":null,"html_url":"https://github.com/toolsfactory/Toolsfactory.Common.Result","commit_stats":null,"previous_names":["toolsfactory/toolsfactory.common.result"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/toolsfactory/Toolsfactory.Common.Result","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toolsfactory%2FToolsfactory.Common.Result","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toolsfactory%2FToolsfactory.Common.Result/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toolsfactory%2FToolsfactory.Common.Result/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toolsfactory%2FToolsfactory.Common.Result/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/toolsfactory","download_url":"https://codeload.github.com/toolsfactory/Toolsfactory.Common.Result/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/toolsfactory%2FToolsfactory.Common.Result/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28412180,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T05:26:33.345Z","status":"ssl_error","status_checked_at":"2026-01-14T05:21:57.251Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["error","errorhandling","exceptions","result"],"created_at":"2026-01-14T06:00:22.803Z","updated_at":"2026-01-14T06:00:37.073Z","avatar_url":"https://github.com/toolsfactory.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Toolsfactory.Common Result and Result\u003cT\u003e utility library\n\nThe `Result` and `Result\u003cT\u003e` classes provide a robust way to represent the outcome of an operation in C#. This approach ensures operations can communicate success, failure, and relevant error details.\n\n## Why use `Result` and `Result\u003cT\u003e`?\n- **Clarity**: \n  - Clearly communicate the outcome of an operation without relying on exceptions.\n  - Exceptions are for - as the name already implies - exceptional cases, not for regular control flow.\n- **Error Handling**: \n  - Easily access error details when an operation fails.\n- **Simplicity**: \n  - Simplify the handling of success and failure scenarios.\n  - The code gets more readable and maintainable.\n- **Performance**: \n  - Avoid the performance overhead of throwing and catching exceptions.\n\n## Key Features\n\n- **Encapsulation of Success/Failure**: \n  - The `Result` class encapsulates whether an operation was successful or not (`IsSuccess`).\n  - Access to associated errors through the `Errors` collection.\n  \n- **Generic Support**: \n  - The `Result\u003cT\u003e` class extends `Result` to include a value (`T`) when the operation succeeds.\n  \n- **Immutable Design**:\n  - Both classes are designed to ensure immutability, enhancing reliability and thread safety.\n\n## Class Details\n\n### `Result`\n- Indicates whether an operation succeeded (`IsSuccess`) or failed (`IsFaulted`).\n- Provides a collection of errors (`IReadOnlyList\u003cError\u003e`) if the operation fails.\n- Constructor overloading allows creating success results or faulted results with error details.\n\n### `Result\u003cT\u003e`\n- Extends `Result` by adding a value (`Value`) of type `T` for successful operations.\n- Throws an exception if you try to access the `Value` of a failed operation.\n- Ensures `Value` is cleared upon failure to prevent misuse.\n\n### Extension methods\n- `Switch` and `Map` methods are provided to simplify handling success and failure scenarios.\n- `Bind`, `BindTryCatch`, and `Tap` methods are provided to simplify chaining operations following the Railway oriented pattern. (Inspired by this [Video](https://www.youtube.com/watch?v=C1oGnDEnS14))\n\n# Usage Examples\n\n### Creating a Success Result\n```csharp\nvar successResult = Result.Success();\n```\n\n### Creating a Faulted Result\n```csharp\nvar faultedResult = Result.Failure(new Error(\"Operation failed\"));\n```\n\n### Using `Result\u003cT\u003e` with a Value\n```csharp\nvar resultWithValue = Result\u003cint\u003e.Success(42);\nif (resultWithValue.IsSuccess)\n{\n    Console.WriteLine(resultWithValue.Value); // Output: 42\n}\n```\n\n### Using `Result\u003cT\u003e` with implicit conversion\n```csharp\nrecord Person(string Name, int Age);\nvar PersonDB = new List\u003cPerson\u003e { new Person(\"Alice\", 25), new Person(\"Bob\", 30) };\nvar result1 = GetPersonWithName(\"Alice\");\nvar result2 = GetPersonWithName(\"James\");\n\nResult\u003cPerson\u003e GetPersonWithName(string Name)\n{\n    if (string.IsNullOrWhiteSpace(Name))\n    {\n        return new Error(\"Name cannot be empty\"); // \u003c= Implicit conversion to Result\u003cPerson\u003e\n    }\n    var person = PersonDB.Where(p =\u003e p.Name == Name).FirstOrDefault();\n    return person != null ? person : new Error(\"Person not found\"); // \u003c= Implicit conversion to Result\u003cPerson\u003e\n}\n\n\n```\n\n### Handling Errors\n```csharp\nif (result.IsFaulted)\n{\n    foreach (var error in result.Errors)\n    {\n        Console.WriteLine(error.Message);\n    }\n}\n```\n\n## Utility methods\nSwitch and Map methods are provided to simplify handling success and failure scenarios.\n### Switch Method\n```csharp\nusing System;\nusing System.Collections.Generic;\nusing Toolsfactory.Common;\n\nclass Program\n{\n    static void Main()\n    {\n        // Example 1: Success scenario\n        var successResult = Result.Success();\n\n        successResult.Switch(\n            onSuccess: () =\u003e Console.WriteLine(\"Operation was successful!\"),\n            onFailure: errors =\u003e\n            {\n                foreach (var error in errors)\n                {\n                    Console.WriteLine($\"Error: {error.Message}\");\n                }\n            }\n        );\n\n        // Example 2: Failure scenario\n        var failureResult = Result.Failure(new List\u003cError\u003e\n        {\n            new Error(\"Invalid input\"),\n            new Error(\"Connection timeout\")\n        });\n\n        failureResult.Switch(\n            onSuccess: () =\u003e Console.WriteLine(\"Operation was successful!\"),\n            onFailure: errors =\u003e\n            {\n                foreach (var error in errors)\n                {\n                    Console.WriteLine($\"Error: {error.Message}\");\n                }\n            }\n        );\n    }\n}\n```\n\n### Generic Switch Method\n```csharp\nusing System;\nusing System.Collections.Generic;\nusing Toolsfactory.Common;\n\nclass Program\n{\n    static void Main()\n    {\n        // Example 1: Success scenario\n        var successResult = Result\u003cint\u003e.Success(42);\n\n        successResult.Switch(\n            onSuccess: value =\u003e Console.WriteLine($\"Success! Value: {value}\"),\n            onFailure: errors =\u003e\n            {\n                foreach (var error in errors)\n                {\n                    Console.WriteLine($\"Error: {error.Message}\");\n                }\n            }\n        );\n\n        // Example 2: Failure scenario\n        var failureResult = Result\u003cint\u003e.Failure(new List\u003cError\u003e\n        {\n            new Error(\"Division by zero\"),\n            new Error(\"Value out of range\")\n        });\n\n        failureResult.Switch(\n            onSuccess: value =\u003e Console.WriteLine($\"Success! Value: {value}\"),\n            onFailure: errors =\u003e\n            {\n                foreach (var error in errors)\n                {\n                    Console.WriteLine($\"Error: {error.Message}\");\n                }\n            }\n        );\n    }\n}\n```\n\n\n### Map Method\n```csharp\nusing System;\nusing System.Collections.Generic;\nusing Toolsfactory.Common;\nclass Program\n{\n    static void Main()\n    {\n        // Example 1: Success scenario\n        var successResult = Result.Success();\n\n        string successMessage = successResult.Map(\n            onSuccess: () =\u003e \"Operation was successful!\",\n            onFailure: errors =\u003e string.Join(\", \", errors.Select(e =\u003e e.Message))\n        );\n\n        Console.WriteLine(successMessage); // Output: Operation was successful!\n\n        // Example 2: Failure scenario\n        var failureResult = Result.Failure(new List\u003cError\u003e\n        {\n            new Error(\"Invalid credentials\"),\n            new Error(\"Account locked\")\n        });\n\n        string failureMessage = failureResult.Map(\n            onSuccess: () =\u003e \"Operation was successful!\",\n            onFailure: errors =\u003e string.Join(\"; \", errors.Select(e =\u003e e.Message))\n        );\n\n        Console.WriteLine(failureMessage); \n        // Output: Invalid credentials; Account locked\n    }\n}\n```\n\n### Generic Map Method\n```csharp\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing Toolsfactory.Common;\n\nclass Program\n{\n    static void Main()\n    {\n        // Example 1: Success scenario\n        var successResult = Result\u003cint\u003e.Success(100);\n\n        string successMessage = successResult.Map(\n            onSuccess: value =\u003e $\"Operation succeeded with value: {value}\",\n            onFailure: errors =\u003e string.Join(\"; \", errors.Select(e =\u003e e.Message))\n        );\n\n        Console.WriteLine(successMessage); \n        // Output: Operation succeeded with value: 100\n\n        // Example 2: Failure scenario\n        var failureResult = Result\u003cint\u003e.Failure(new List\u003cError\u003e\n        {\n            new Error(\"Network issue\"),\n            new Error(\"Timeout occurred\")\n        });\n\n        string failureMessage = failureResult.Map(\n            onSuccess: value =\u003e $\"Operation succeeded with value: {value}\",\n            onFailure: errors =\u003e $\"Errors: {string.Join(\", \", errors.Select(e =\u003e e.Message))}\"\n        );\n\n        Console.WriteLine(failureMessage);\n        // Output: Errors: Network issue, Timeout occurred\n    }\n}\n```\n\n### Railway oriented pattern samples\n```csharp\nResult\u003cstring\u003e result = Result\u003cbool\u003e.Success(true)\n        .Bind\u003cbool, string\u003e(x =\u003e x ? \"Great!\" : \"Not so great...\")\n        .Tap(Console.WriteLine);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoolsfactory%2Ftoolsfactory.common.result","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftoolsfactory%2Ftoolsfactory.common.result","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftoolsfactory%2Ftoolsfactory.common.result/lists"}