Skip to content

A lightweight, zero-dependency C# library that implements the Result pattern for more explicit and type-safe error handling. SharpResults helps you avoid exceptions for control flow and makes success/failure states explicit in your code.

Notifications You must be signed in to change notification settings

RezaArda/SharpResults

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

SharpResults 📊

GitHub Repo stars NuGet License

Welcome to SharpResults, a lightweight, zero-dependency C# library that implements the Result pattern for more explicit and type-safe error handling. With SharpResults, you can avoid using exceptions for control flow, making success and failure states clear in your code.

Table of Contents

Features

  • Zero Dependencies: SharpResults is designed to be lightweight and easy to integrate into your existing projects.
  • Type-Safe Error Handling: Implement the Result pattern to handle errors without exceptions.
  • Fluent API: Enjoy a clean and intuitive API for handling results.
  • Functional Programming Support: Use functional programming concepts to enhance your code quality.
  • Monads: Work with monads to manage side effects and improve code clarity.

Installation

You can easily add SharpResults to your project via NuGet. Use the following command in your Package Manager Console:

Install-Package SharpResults

Alternatively, you can search for "SharpResults" in the NuGet Package Manager within Visual Studio.

For the latest releases, visit the Releases section.

Usage

Using SharpResults is straightforward. Below is a simple example to get you started.

Basic Example

using SharpResults;

public class Example
{
    public Result<int> Divide(int numerator, int denominator)
    {
        if (denominator == 0)
        {
            return https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip<int>("Cannot divide by zero.");
        }
        
        return https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(numerator / denominator);
    }
}

In this example, the Divide method returns a Result<int>. If the denominator is zero, it returns a failure result with an error message.

Handling Results

You can easily handle results using the OnSuccess and OnFailure methods:

var result = new Example().Divide(10, 0);

https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(value => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Result: {value}"))
      .OnFailure(error => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Error: {error}"));

This approach allows you to manage success and failure states clearly.

Examples

Chaining Results

SharpResults supports chaining, allowing you to create a sequence of operations:

var result = new Example()
    .Divide(10, 2)
    .OnSuccess(value => new Example().Divide(value, 2));

https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(value => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Final Result: {value}"))
      .OnFailure(error => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Error: {error}"));

Combining Results

You can also combine multiple results into a single result:

var result1 = new Example().Divide(10, 2);
var result2 = new Example().Divide(20, 4);

var combinedResult = https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(result1, result2);

https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip(() => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip("Both operations succeeded."))
              .OnFailure(error => https://github.com/RezaArda/SharpResults/raw/refs/heads/master/src/Types/Results_Sharp_dissentaneous.zip($"Error: {error}"));

API Reference

For a comprehensive list of methods and classes, check the official documentation. You can also explore the source code in the repository.

Contributing

We welcome contributions to SharpResults. To contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes.
  4. Commit your changes (git commit -m 'Add new feature').
  5. Push to the branch (git push origin feature/YourFeature).
  6. Open a Pull Request.

Please ensure your code adheres to our coding standards and includes tests where applicable.

License

SharpResults is licensed under the MIT License. See the LICENSE file for more details.

Acknowledgments

Thanks to all contributors and the open-source community for their support. Your contributions make projects like SharpResults possible.

For the latest updates and releases, visit the Releases section.


Feel free to explore the code, report issues, and suggest improvements. Happy coding!

About

A lightweight, zero-dependency C# library that implements the Result pattern for more explicit and type-safe error handling. SharpResults helps you avoid exceptions for control flow and makes success/failure states explicit in your code.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages