{"id":22792248,"url":"https://github.com/vikashchauhan51/resultify","last_synced_at":"2025-04-13T13:41:15.923Z","repository":{"id":266863710,"uuid":"899571560","full_name":"VikashChauhan51/resultify","owner":"VikashChauhan51","description":"ResultifyCore is a .NET library providing Result and Option patterns to simplify error handling and optional values. It includes extension methods for fluent API support and enhanced readability.","archived":false,"fork":false,"pushed_at":"2025-02-04T06:25:32.000Z","size":412,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T02:48:03.815Z","etag":null,"topics":["dotnet","dotnet-core","option-pattern","result-pattern"],"latest_commit_sha":null,"homepage":"","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/VikashChauhan51.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-06T14:43:50.000Z","updated_at":"2025-02-04T06:25:35.000Z","dependencies_parsed_at":"2024-12-06T16:32:52.113Z","dependency_job_id":"77d08d6b-aa10-4401-aedb-c50863135100","html_url":"https://github.com/VikashChauhan51/resultify","commit_stats":null,"previous_names":["vikashchauhan51/resultify"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VikashChauhan51%2Fresultify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VikashChauhan51%2Fresultify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VikashChauhan51%2Fresultify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VikashChauhan51%2Fresultify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VikashChauhan51","download_url":"https://codeload.github.com/VikashChauhan51/resultify/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248724229,"owners_count":21151557,"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":["dotnet","dotnet-core","option-pattern","result-pattern"],"created_at":"2024-12-12T03:10:13.928Z","updated_at":"2025-04-13T13:41:15.894Z","avatar_url":"https://github.com/VikashChauhan51.png","language":"C#","readme":"\n# ResultifyCore\n\n![ResultifyCore](resultifycore.ico)\n\n\n**ResultifyCore** is a .NET library providing `Result`, `Option`, `Outcome`, `OneOf` and `Guard` patterns to simplify error handling, optional values, and type discrimination. It includes extension methods for fluent API support and enhanced readability.\n\n## Installation\n\nYou can install the ResultifyCore package via NuGet:\n\n[![NuGet Version](https://img.shields.io/nuget/v/ResultifyCore.svg?style=flat-square)](https://www.nuget.org/packages/ResultifyCore/)\n[![NuGet Downloads](https://img.shields.io/nuget/dt/ResultifyCore.svg?style=flat-square)](https://www.nuget.org/packages/ResultifyCore/)\n[![Build Status](https://github.com/VikashChauhan51/resultify/actions/workflows/build.yml/badge.svg)](https://github.com/VikashChauhan51/resultify/actions)\n[![License](https://img.shields.io/github/license/VikashChauhan51/resultify.svg?style=flat-square)](https://github.com/VikashChauhan51/resultify/blob/main/LICENSE)\n\n\n```shell\ndotnet add package ResultifyCore\n```\n\nOr you can use the NuGet Package Manager:\n\n```shell\nInstall-Package ResultifyCore\n```\n\nYou can install the ResultifyCore.AspNetCore package via NuGet:\n\n[![NuGet Version](https://img.shields.io/nuget/v/ResultifyCore.AspNetCore.svg?style=flat-square)](https://www.nuget.org/packages/ResultifyCore.AspNetCore/)\n[![NuGet Downloads](https://img.shields.io/nuget/dt/ResultifyCore.AspNetCore.svg?style=flat-square)](https://www.nuget.org/packages/ResultifyCore/)\n[![Build Status](https://github.com/VikashChauhan51/resultify/actions/workflows/build.yml/badge.svg)](https://github.com/VikashChauhan51/resultify/actions)\n[![License](https://img.shields.io/github/license/VikashChauhan51/resultify.svg?style=flat-square)](https://github.com/VikashChauhan51/resultify/blob/main/LICENSE)\n\n```shell\ndotnet add package ResultifyCore.AspNetCore\n```\n\nOr you can use the NuGet Package Manager:\n\n```shell\nInstall-Package ResultifyCore.AspNetCore\n```\n\n## Usage\n\n### Result Pattern\n\nThe `Result` pattern represents the outcome of an operation that can either succeed or fail. It allows for better error handling and makes the flow of success or failure explicit.\n\n#### Creating a Result\n\n```csharp\nusing ResultifyCore;\n\nvar successResult = Result\u003cint\u003e.Success(42);  // A successful result containing the value 42\nvar failureResult = Result\u003cint\u003e.Failure(new Exception(\"Something went wrong\")); // A failed result containing an exception\n```\n\n#### `Success` and `Failure` Extension Methods\n\nYou can create successful and failure results more easily using these extension methods.\n\n##### Example of Success Extension Method:\n\n```csharp\nusing ResultifyCore;\n\nvar result = \"Success!\".Success(); // Create a successful result with a value\nConsole.WriteLine(result.IsSuccess);  // Outputs: True\nConsole.WriteLine(result.Value);     // Outputs: Success!\n\n// If an exception is passed, an InvalidOperationException will be thrown\ntry\n{\n    var resultWithException = new Exception(\"Test Exception\").Success();\n}\ncatch (InvalidOperationException ex)\n{\n    Console.WriteLine(ex.Message);  // Outputs: Cannot use an Exception as a value.\n}\n```\n\n##### Example of Failure Extension Method:\n\n```csharp\nusing ResultifyCore;\n\nvar failureResult = new Exception(\"Failure reason\").Failure\u003cstring\u003e(); // Create a failure result containing an exception\nConsole.WriteLine(failureResult.IsSuccess);  // Outputs: False\nConsole.WriteLine(failureResult.Exception.Message); // Outputs: Failure reason\n\n// If the exception is null, an ArgumentNullException will be thrown\ntry\n{\n    Exception exception = null;\n    var resultWithNullException = exception.Failure\u003cstring\u003e();\n}\ncatch (ArgumentNullException ex)\n{\n    Console.WriteLine(ex.Message);  // Outputs: Exception cannot be null.\n}\n```\n\n#### Method Chaining with `Result`\n\nMethod chaining allows you to perform multiple operations on the result, including transforming it, checking success or failure, or applying side-effects:\n\n```csharp\nvar successResult = 42.Success();\n\nsuccessResult\n    .Do(res =\u003e Console.WriteLine(\"Executing common action\"))     // Executes regardless of the result state\n    .Tap(value =\u003e Console.WriteLine($\"Tap into value: {value}\"))  // Executes a side-effect\n    .Map(value =\u003e value * 2)                                     // Transforms the value\n    .OnSuccess(value =\u003e Console.WriteLine($\"Transformed value: {value}\"));  // Executes if result is successful\n```\n\nFor a failed result, the same chaining pattern applies:\n\n```csharp\nvar failureResult = new Exception(\"Something went wrong\").Failure\u003cint\u003e();\n\nfailureResult\n    .Do(res =\u003e Console.WriteLine(\"Executing common action\"))\n    .Tap(value =\u003e Console.WriteLine($\"Tap into value: {value}\"))\n    .Map(value =\u003e value * 2)\n    .OnSuccess(value =\u003e Console.WriteLine($\"Transformed value: {value}\"));\n```\n\n### Option Pattern\n\nThe `Option` pattern represents an optional value that may or may not be present. It allows you to avoid `null` references and explicitly handle the absence of values.\n\n#### Creating an Option\n\n```csharp\nusing ResultifyCore;\n\nvar someOption = Option\u003cint\u003e.Some(42);  // Option containing a value\nvar noneOption = Option\u003cint\u003e.None;      // Option with no value\n var optionResult = 42.Some();\n```\n\n#### Match Method for Option\n\n```csharp\nvar matchOptionResult = someOption.Match\u003cstring\u003e(\n    onSome: value =\u003e \"Value is present\",\n    onNone: () =\u003e \"No value\"\n);\nConsole.WriteLine(matchOptionResult);  // Outputs: Value is present\n```\n\n### OneOf Pattern\n\nThe `OneOf` pattern allows you to encapsulate multiple possible types for a single value. This is useful for cases where a value can belong to one of several types.\n\n#### Example:\n\n```csharp\nusing ResultifyCore;\n\nOneOf\u003cint, string\u003e numberOrString = new OneOf\u003cint, string\u003e(\"Hello\");\n\n# Accessing Values\nif (numberOrString.IsT1)\n{\n    Console.WriteLine($\"Value is of type T1: {numberOrString.AsT1}\");\n}\nelse if (numberOrString.IsT2)\n{\n    Console.WriteLine($\"Value is of type T2: {numberOrString.AsT2}\");\n}\n\n# Match Method\nvar matchResult = numberOrString.Match(\n    matchT1: number =\u003e $\"Number: {number}\",\n    matchT2: text =\u003e $\"String: {text}\"\n);\nConsole.WriteLine(matchResult);  // Outputs: String: Hello\n```\n\n### Outcome Pattern\n\nThe `Outcome` pattern provides an alternative to exceptions for managing errors and failures, offering a clean, functional approach to success and failure scenarios.\n\n#### Example:\n\n```csharp\n\nvar optionResult = 42.SuccessOutcome();\n\npublic static Outcome ValidateInput(string input)\n{\n    if (string.IsNullOrWhiteSpace(input))\n    {\n        return Outcome.Failure(new OutcomeError(\"VALIDATION_ERROR\", \"Input cannot be null or whitespace.\"));\n    }\n\n    return Outcome.Success();\n}\n\npublic static Outcome\u003cint\u003e ParseNumber(string input)\n{\n    if (int.TryParse(input, out var number))\n    {\n        return Outcome\u003cint\u003e.Success(number);\n    }\n\n    return Outcome\u003cint\u003e.Failure(new OutcomeError(\"PARSE_ERROR\", \"Invalid number format.\"));\n}\n\nvar result = ParseNumber(\"abc\");\n\nresult.Match(\n    onSuccess: value =\u003e Console.WriteLine($\"Parsed number: {value}\"),\n    onFailure: errors =\u003e Console.WriteLine($\"Failed to parse number: {string.Join(\", \", errors)}\")\n);\n```\n### Guard Pattern\n\nThe Guard pattern helps enforce preconditions in your code by validating arguments.\n It ensures your methods receive valid input and fail early with meaningful exceptions when they don't.\n\n #### Example of Guard Usage\n\n ```csharp\npublic class Calculator\n{\n    public static int Divide(int dividend, int divisor)\n    {\n        // Validate arguments using Guard\n        Guard.ThrowIfArgumentIsNegative(dividend, nameof(dividend));\n        Guard.ThrowIfArgumentIsNegativeOrZero(divisor, nameof(divisor));\n\n        return dividend / divisor;\n    }\n}\n ```\n\n### Extensions\n\nResultifyCore provides several extension methods to facilitate fluent API and method chaining.\n\n#### Result Extensions\n\n- **`Do`**: Executes an action regardless of the result state.\n- **`OnSuccess`**: Executes an action if the result is successful.\n- **`OnError`**: Executes an action if the result is failed.\n- **`Map`**: Transforms the value inside a successful result.\n- **`Bind`**: Chains multiple operations that return results.\n- **`Tap`**: Executes a side-effect action without altering the result.\n\n#### Option Extensions\n\n- **`Do`**: Executes an action regardless of whether the option has a value.\n- **`OnSome`**: Executes an action if the option has a value.\n- **`OnNone`**: Executes an action if the option does not have a value.\n- **`Map`**: Transforms the value inside an option.\n- **`Bind`**: Chains multiple operations that return options.\n- **`Tap`**: Executes a side-effect action without altering the option.\n\n#### `IActionResult` Extensions\n\nThe following extension methods convert `Outcome` and `Result` objects to `IActionResult` in ASP.NET Core:\n\n```csharp\npublic static IActionResult ToActionResult\u003cT\u003e(this Outcome\u003cT\u003e result, ControllerBase controller, string? url = null)\npublic static IActionResult ToActionResult(this Outcome result, ControllerBase controller)\npublic static IActionResult ToActionResult\u003cT\u003e(this Result\u003cT\u003e result, ControllerBase controller, string? url = null)\npublic static IActionResult ToActionResult(this Result result, ControllerBase controller)\n```\n\n#### `IResult` Extensions\n\nThe following extension methods convert `Outcome` and `Result` objects to `IResult` in ASP.NET Core:\n\n```csharp\npublic static IResult ToResult\u003cT\u003e(this Outcome\u003cT\u003e result, string? url = null)\npublic static IResult ToResult(this Outcome result)\npublic static IResult ToResult\u003cT\u003e(this Result\u003cT\u003e result, string? url = null)\npublic static IResult ToResult(this Result result)\n```\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvikashchauhan51%2Fresultify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvikashchauhan51%2Fresultify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvikashchauhan51%2Fresultify/lists"}