Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docfx_project/_site/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DocFX build output — generated by `docfx build`
*
!.gitignore
3 changes: 3 additions & 0 deletions docfx_project/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# DocFX auto-generated metadata files — rebuilt by `docfx metadata`
*.yml
!.gitignore
11 changes: 11 additions & 0 deletions docfx_project/api/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# API Reference

Welcome to the Wolfgang.TryPattern API documentation.

This section contains the complete API reference, automatically generated from XML documentation comments in the source code.

Browse the navigation menu to explore available namespaces and types.

## Getting Started

For usage examples and guides, see the [Documentation](../docs/getting-started.md) section.
38 changes: 19 additions & 19 deletions docfx_project/docfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,42 @@
{
"src": [
{
"files": [
"src/<path>/<project>.csproj"
],
"files": ["src/**/*.csproj"],
"src": "../"
}
],
"dest": "api"
"dest": "api",
"properties": {
"TargetFramework": "net8.0"
},
"disableGitFeatures": false,
"disableDefaultFilter": false
}
],
"build": {
"content": [
{
"files": [
"**/*.{md,yml}"
],
"exclude": [
"_site/**"
]
"files": ["**/*.{md,yml}"],
"exclude": ["_site/**"]
}
],
"resource": [
{
"files": [
"images/**"
]
"files": ["logo.svg", "images/**"]
}
],
"output": "_site",
"template": [
"modern",
"default"
],
"template": ["default", "modern"],
"globalMetadata": {
"_appName": "",
"_appTitle": "<title>",
"_appName": "Wolfgang.TryPattern",
"_appTitle": "Wolfgang.TryPattern Documentation",
"_appLogoPath": "logo.svg",
"_enableSearch": true,
"_appFooter": "Made with DocFX",
"_disableSidebar": false,
"_disableTocFilter": false,
"_enableDarkMode": true,
"colorMode": "dark",
"pdf": true
}
}
Expand Down
144 changes: 143 additions & 1 deletion docfx_project/docs/getting-started.md
Original file line number Diff line number Diff line change
@@ -1 +1,143 @@
# Getting Started
# Getting Started

## Prerequisites

- .NET 10 SDK or later (required to build and test this repository, because the library multi-targets .NET 10.0)
- Any project targeting .NET Framework 4.6.2+, .NET Standard 2.0, .NET 8.0, or .NET 10.0

## Installation

### Package Manager Console

```powershell
Install-Package Wolfgang.TryPattern
```

### .NET CLI

```bash
dotnet add package Wolfgang.TryPattern
```

### PackageReference

```xml
<PackageReference Include="Wolfgang.TryPattern" Version="0.2.0" />
```

## Basic Usage

### Running an Action

Use `Try.Run` to execute an action that might throw:

```csharp
using Wolfgang.TryPattern;

var result = Try.Run(() => File.Delete(tempPath));

if (result.Failed)
{
Console.WriteLine($"Could not delete file: {result.ErrorMessage}");
}
```

### Running a Function

Use `Try.Run<T>` to execute a function and capture its return value:

```csharp
var result = Try.Run(() => int.Parse(userInput));

if (result.Succeeded)
{
Console.WriteLine($"Parsed value: {result.Value}");
}
else
{
Console.WriteLine($"Invalid input: {result.ErrorMessage}");
}
```

### Async Operations

Both `Action` and `Func<Task<T>>` have async counterparts:

```csharp
var result = await Try.RunAsync(
() => httpClient.GetStringAsync(url)
);

if (result.Succeeded)
{
ProcessResponse(result.Value);
}
```

Async operations support cancellation:

```csharp
var result = await Try.RunAsync(
() => httpClient.GetStringAsync(url),
cancellationToken
);
```

> [!NOTE]
> `OperationCanceledException` is intentionally **not** caught by `Try.RunAsync`. Cancellation propagates normally so callers can distinguish between a failed operation and a cancelled one.

### Composing Results

When you need to run multiple operations and check the overall outcome:

```csharp
var result1 = Try.Run(() => ValidateName(name));
var result2 = Try.Run(() => ValidateEmail(email));
var result3 = Try.Run(() => ValidateAge(age));

// Check if any failed
if (Result.AnyFailed(result1, result2, result3))
{
Console.WriteLine("Validation errors occurred.");
}

// Or flatten into a single result with combined error messages
var combined = Result.Flatten(result1, result2, result3);

if (combined.Failed)
{
// ErrorMessage contains all failure messages joined by newlines
Console.WriteLine(combined.ErrorMessage);
}
```

### Creating Results Directly

You can also create `Result` objects directly for your own methods:

```csharp
public Result ValidateOrder(Order order)
{
if (order.Items.Count == 0)
{
return Result.Failure("Order must contain at least one item.");
}

return Result.Success();
}

public Result<decimal> CalculateDiscount(Order order)
{
if (order.Total < 50)
{
return Result<decimal>.Failure("Minimum order for discount is $50.");
}

return Result<decimal>.Success(order.Total * 0.10m);
}
```

## Next Steps

- Browse the [API Reference](../api/index.md) for complete method signatures and XML documentation
- View the [source code](https://github.com/Chris-Wolfgang/Try-Pattern) on GitHub
8 changes: 8 additions & 0 deletions docfx_project/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Wolfgang.TryPattern Documentation

Welcome to the documentation section. Browse the topics in the navigation menu to get started.

## Available Documentation

- [Introduction](introduction.md) - Overview and introduction
- [Getting Started](getting-started.md) - Quick start guide
80 changes: 79 additions & 1 deletion docfx_project/docs/introduction.md
Original file line number Diff line number Diff line change
@@ -1 +1,79 @@
# Introduction
# Introduction

Wolfgang.TryPattern is a lightweight .NET library that brings structured error handling to your codebase through the Try pattern. It replaces scattered try/catch blocks with a clean, composable API that makes error handling explicit and predictable.

## The Problem

Traditional .NET error handling relies on try/catch blocks that can obscure control flow, make it difficult to compose operations, and lead to inconsistent error handling across a codebase:

```csharp
string content;
try
{
content = File.ReadAllText(path);
}
catch (Exception ex)
{
// Now what? Log? Return null? Throw a different exception?
logger.LogError(ex, "Failed to read file");
return null;
}
```

## The Solution

Wolfgang.TryPattern wraps operations in `Try.Run` and returns a `Result` object that clearly communicates the outcome:

```csharp
var result = Try.Run(() => File.ReadAllText(path));

if (result.Succeeded)
{
Console.WriteLine(result.Value);
}
else
{
Console.WriteLine($"Error: {result.ErrorMessage}");
}
```

## Core Types

### `Try`

A static class providing methods to execute actions and functions with automatic exception handling:

- **`Try.Run(Action)`** - Execute an action, returning a `Result`
- **`Try.Run<T>(Func<T>)`** - Execute a function, returning a `Result<T>` with the return value
- **`Try.RunAsync(Action, CancellationToken)`** - Execute an action asynchronously
- **`Try.RunAsync<T>(Func<Task<T>>, CancellationToken)`** - Execute an async function

### `Result`

Represents the outcome of an operation:

- **`Succeeded`** - `true` if the operation completed without exceptions
- **`Failed`** - `true` if the operation threw an exception
- **`ErrorMessage`** - The exception message if the operation failed

### `Result<T>`

Extends `Result` with a typed return value:

- **`Value`** - The return value from the function (throws `InvalidOperationException` if accessed on a failed result)

### Composition Helpers

- **`Result.Flatten(params Result[])`** - Combine multiple results into one; if any failed, the error messages are joined
- **`Result.AnyFailed(params Result[])`** - Check if any result in a set indicates failure
- **`Result.AllSucceeded(params Result[])`** - Check if every result in a set indicates success

## Platform Support

Wolfgang.TryPattern targets multiple frameworks for broad compatibility:

| Target | Version |
|--------|---------|
| .NET Framework | 4.6.2+ |
| .NET Standard | 2.0 |
| .NET | 8.0, 10.0 |
4 changes: 2 additions & 2 deletions docfx_project/docs/toc.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
- name: Index
href: Index.md
href: index.md
- name: Introduction
href: introduction.md
- name: Getting Started
href: getting-started.md
- name: Project website
href: https://github.com/Chris-Wolfgang/DbContextBuilder
href: 'https://github.com/Chris-Wolfgang/Try-Pattern'
40 changes: 37 additions & 3 deletions docfx_project/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,42 @@
_layout: landing
---

# Title
# Wolfgang.TryPattern Documentation

## Quick Start Notes:
Welcome to the Wolfgang.TryPattern documentation. This site contains comprehensive guides, API reference, and examples to help you get started.

<!--1. Add images to the *images* folder if the file is referencing an image.-->
## Quick Links

- [Getting Started](docs/getting-started.md) - Installation and basic usage
- [API Reference](api/index.md) - Complete API documentation
- [GitHub Repository](https://github.com/Chris-Wolfgang/Try-Pattern) - View source code
Comment thread
Chris-Wolfgang marked this conversation as resolved.

## About Wolfgang.TryPattern

Wolfgang.TryPattern is a lightweight .NET library that provides structured error handling through the Try pattern. Instead of scattering try/catch blocks throughout your code, wrap operations in `Try.Run` and get back a `Result` that clearly indicates success or failure.

## Installation

```bash
dotnet add package Wolfgang.TryPattern
```

## Documentation Sections

### [Documentation](docs/getting-started.md)

Step-by-step guides and tutorials to help you use Wolfgang.TryPattern effectively.

### [API Reference](api/index.md)

Complete API documentation automatically generated from source code XML comments.

## Additional Resources

- [Contributing Guidelines](https://github.com/Chris-Wolfgang/Try-Pattern/blob/main/CONTRIBUTING.md)
- [Code of Conduct](https://github.com/Chris-Wolfgang/Try-Pattern/blob/main/CODE_OF_CONDUCT.md)
- [License](https://github.com/Chris-Wolfgang/Try-Pattern/blob/main/LICENSE)

---

*Documentation built with [DocFX](https://dotnet.github.io/docfx/)*
Loading