Skip to content

Commit 92e11f9

Browse files
authored
Merge pull request #6 from I-RzR-I/feature/TryBuilder
try builder
2 parents ae6f1c0 + 5f71893 commit 92e11f9

28 files changed

Lines changed: 2844 additions & 64 deletions

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ In the following flow on trying to implement try execution (`TryToExecute`, `Try
1111
- code for the catch block `catch { }`;
1212
- code for finally block `finally { }`.
1313

14+
Or can be used `TryBuilder` for fluent invoke.
15+
```csharp
16+
TryBuilder.Do(async () => await FooAsync())
17+
.Catch<IOException>(ex => ...)
18+
.Finally(() => ...)
19+
.ExecuteAsync();
20+
```
21+
22+
1423
To understand more efficiently how you can use available functionalities please consult the [using documentation/file](docs/usage.md).
1524

1625
**In case you wish to use it in your project, u can install the package from <a href="https://www.nuget.org/packages/TryToExecute" target="_blank">nuget.org</a>** or specify what version you want:

build/generateCustomVersion.bat

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@echo off
2+
cls
3+
4+
echo Write the source branch name (you current branch name, where were the changes made):
5+
set /p userDefinedSourceBranch=
6+
echo.
7+
echo Write the destination branch name (the branch from which you can get the latest changes, like 'develop' or 'main/master'):
8+
set /p userDefinedDestinationBranch=
9+
echo.
10+
echo Write your custom version number:
11+
set /p userDefinedVersion=
12+
echo.
13+
14+
:: Set init params value
15+
set applicationName=TryToExecute
16+
set runVersionIncrement=y
17+
set runGenChangeLog=y
18+
:: If runBuild > y(yes), build in release mode
19+
set runBuild=y
20+
set runSolutionTest=n
21+
set runTest=n
22+
set runPack=y
23+
set assemblyPath=$('..\src\shared\GeneralAssemblyInfo.cs')
24+
set genType=-2
25+
set setInChangeLogNewVersion=y
26+
set autoCommitAndPush=n
27+
set autoGetLatestDevelop=y
28+
set changeLogPath=$('..\docs\CHANGELOG.MD')
29+
set sourceBranch=%userDefinedSourceBranch%
30+
set destinationBranch=%userDefinedDestinationBranch%
31+
set customVersion=%userDefinedVersion%
32+
set solutionPath=$('..\src\RzR.Shared.Extensions.sln')
33+
set packResultPath=$('..\nuget\')
34+
set packProjectsPath=$('..\src\TryToExecute\TryToExecute.csproj')
35+
set testProjectsPath=$('..\src\tests\TryExecuteTests\TryExecuteTests.csproj')
36+
37+
38+
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
39+
echo ::: Initialize: :::
40+
echo ::: - New application version generation :::
41+
echo ::: - Change log generation :::
42+
echo ::: - Build :::
43+
echo ::: - Test :::
44+
echo ::: - Create package :::
45+
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
46+
echo:
47+
echo:
48+
49+
PowerShell -NoProfile -ExecutionPolicy ByPass -Command ".\GenerateBuildInfo.exe -scriptCommands \"runVersionIncrement=%runVersionIncrement%;runGenChangeLog=%runGenChangeLog%;runBuild=%runBuild%;runSolutionTest=%runSolutionTest%;runTest=%runTest%;runPack=%runPack%;setInChangeLogNewVersion=%setInChangeLogNewVersion%;autoCommitAndPush=%autoCommitAndPush%;autoGetLatestDevelop=%autoGetLatestDevelop%;changeLogPath=%changeLogPath%;sourceBranch=%sourceBranch%;destinationBranch=%destinationBranch%;assemblyPath=%assemblyPath%;customVersion=%customVersion%;genType=%genType%;solutionPath=%solutionPath%;packResultPath=%packResultPath%;packProjectsPath=%packProjectsPath%;testProjectsPath=%testProjectsPath%\"";
50+
51+
echo
52+
pause

build/generateNextMajorVersion.bat

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@echo off
2+
cls
3+
4+
echo Write the source branch name (you current branch name, where were the changes made):
5+
set /p userDefinedSourceBranch=
6+
echo.
7+
echo Write the destination branch name (the branch from which you can get the latest changes, like 'develop' or 'main/master'):
8+
set /p userDefinedDestinationBranch=
9+
echo.
10+
11+
:: Set init params value
12+
set applicationName=TryToExecute
13+
set runVersionIncrement=y
14+
set runGenChangeLog=y
15+
:: If runBuild > y(yes), build in release mode
16+
set runBuild=y
17+
set runSolutionTest=n
18+
set runTest=n
19+
set runPack=y
20+
set assemblyPath=$('..\src\shared\GeneralAssemblyInfo.cs')
21+
set genType=0
22+
set setInChangeLogNewVersion=y
23+
set autoCommitAndPush=n
24+
set autoGetLatestDevelop=y
25+
set changeLogPath=$('..\docs\CHANGELOG.MD')
26+
set sourceBranch=%userDefinedSourceBranch%
27+
set destinationBranch=%userDefinedDestinationBranch%
28+
set customVersion=$null
29+
set solutionPath=$('..\src\RzR.Shared.Extensions.sln')
30+
set packResultPath=$('..\nuget\')
31+
set packProjectsPath=$('..\src\TryToExecute\TryToExecute.csproj')
32+
set testProjectsPath=$('..\src\tests\TryExecuteTests\TryExecuteTests.csproj')
33+
34+
35+
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
36+
echo ::: Initialize: :::
37+
echo ::: - New application version generation :::
38+
echo ::: - Change log generation :::
39+
echo ::: - Build :::
40+
echo ::: - Test :::
41+
echo ::: - Create package :::
42+
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
43+
echo:
44+
echo:
45+
46+
PowerShell -NoProfile -ExecutionPolicy ByPass -Command ".\GenerateBuildInfo.exe -scriptCommands \"runVersionIncrement=%runVersionIncrement%;runGenChangeLog=%runGenChangeLog%;runBuild=%runBuild%;runSolutionTest=%runSolutionTest%;runTest=%runTest%;runPack=%runPack%;setInChangeLogNewVersion=%setInChangeLogNewVersion%;autoCommitAndPush=%autoCommitAndPush%;autoGetLatestDevelop=%autoGetLatestDevelop%;changeLogPath=%changeLogPath%;sourceBranch=%sourceBranch%;destinationBranch=%destinationBranch%;assemblyPath=%assemblyPath%;customVersion=%customVersion%;genType=%genType%;solutionPath=%solutionPath%;packResultPath=%packResultPath%;packProjectsPath=%packProjectsPath%;testProjectsPath=%testProjectsPath%\"";
47+
48+
echo
49+
pause

build/generateNextMinorVersion.bat

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@echo off
2+
cls
3+
4+
echo Write the source branch name (you current branch name, where were the changes made):
5+
set /p userDefinedSourceBranch=
6+
echo.
7+
echo Write the destination branch name (the branch from which you can get the latest changes, like 'develop' or 'main/master'):
8+
set /p userDefinedDestinationBranch=
9+
echo.
10+
11+
:: Set init params value
12+
set applicationName=TryToExecute
13+
set runVersionIncrement=y
14+
set runGenChangeLog=y
15+
:: If runBuild > y(yes), build in release mode
16+
set runBuild=y
17+
set runSolutionTest=n
18+
set runTest=n
19+
set runPack=y
20+
set assemblyPath=$('..\src\shared\GeneralAssemblyInfo.cs')
21+
set genType=1
22+
set setInChangeLogNewVersion=y
23+
set autoCommitAndPush=n
24+
set autoGetLatestDevelop=y
25+
set changeLogPath=$('..\docs\CHANGELOG.MD')
26+
set sourceBranch=%userDefinedSourceBranch%
27+
set destinationBranch=%userDefinedDestinationBranch%
28+
set customVersion=$null
29+
set solutionPath=$('..\src\RzR.Shared.Extensions.sln')
30+
set packResultPath=$('..\nuget\')
31+
set packProjectsPath=$('..\src\TryToExecute\TryToExecute.csproj')
32+
set testProjectsPath=$('..\src\tests\TryExecuteTests\TryExecuteTests.csproj')
33+
34+
35+
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
36+
echo ::: Initialize: :::
37+
echo ::: - New application version generation :::
38+
echo ::: - Change log generation :::
39+
echo ::: - Build :::
40+
echo ::: - Test :::
41+
echo ::: - Create package :::
42+
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
43+
echo:
44+
echo:
45+
46+
PowerShell -NoProfile -ExecutionPolicy ByPass -Command ".\GenerateBuildInfo.exe -scriptCommands \"runVersionIncrement=%runVersionIncrement%;runGenChangeLog=%runGenChangeLog%;runBuild=%runBuild%;runSolutionTest=%runSolutionTest%;runTest=%runTest%;runPack=%runPack%;setInChangeLogNewVersion=%setInChangeLogNewVersion%;autoCommitAndPush=%autoCommitAndPush%;autoGetLatestDevelop=%autoGetLatestDevelop%;changeLogPath=%changeLogPath%;sourceBranch=%sourceBranch%;destinationBranch=%destinationBranch%;assemblyPath=%assemblyPath%;customVersion=%customVersion%;genType=%genType%;solutionPath=%solutionPath%;packResultPath=%packResultPath%;packProjectsPath=%packProjectsPath%;testProjectsPath=%testProjectsPath%\"";
47+
48+
echo
49+
pause

build/generateNextPatchVersion.bat

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@echo off
2+
cls
3+
4+
echo Write the source branch name (you current branch name, where were the changes made):
5+
set /p userDefinedSourceBranch=
6+
echo.
7+
echo Write the destination branch name (the branch from which you can get the latest changes, like 'develop' or 'main/master'):
8+
set /p userDefinedDestinationBranch=
9+
echo.
10+
11+
:: Set init params value
12+
set applicationName=TryToExecute
13+
set runVersionIncrement=y
14+
set runGenChangeLog=y
15+
:: If runBuild > y(yes), build in release mode
16+
set runBuild=y
17+
set runSolutionTest=n
18+
set runTest=n
19+
set runPack=y
20+
set assemblyPath=$('..\src\shared\GeneralAssemblyInfo.cs')
21+
set genType=2
22+
set setInChangeLogNewVersion=y
23+
set autoCommitAndPush=n
24+
set autoGetLatestDevelop=y
25+
set changeLogPath=$('..\docs\CHANGELOG.MD')
26+
set sourceBranch=%userDefinedSourceBranch%
27+
set destinationBranch=%userDefinedDestinationBranch%
28+
set customVersion=$null
29+
set solutionPath=$('..\src\RzR.Shared.Extensions.sln')
30+
set packResultPath=$('..\nuget\')
31+
set packProjectsPath=$('..\src\TryToExecute\TryToExecute.csproj')
32+
set testProjectsPath=$('..\src\tests\TryExecuteTests\TryExecuteTests.csproj')
33+
34+
35+
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
36+
echo ::: Initialize: :::
37+
echo ::: - New application version generation :::
38+
echo ::: - Change log generation :::
39+
echo ::: - Build :::
40+
echo ::: - Test :::
41+
echo ::: - Create package :::
42+
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
43+
echo:
44+
echo:
45+
46+
PowerShell -NoProfile -ExecutionPolicy ByPass -Command ".\GenerateBuildInfo.exe -scriptCommands \"runVersionIncrement=%runVersionIncrement%;runGenChangeLog=%runGenChangeLog%;runBuild=%runBuild%;runSolutionTest=%runSolutionTest%;runTest=%runTest%;runPack=%runPack%;setInChangeLogNewVersion=%setInChangeLogNewVersion%;autoCommitAndPush=%autoCommitAndPush%;autoGetLatestDevelop=%autoGetLatestDevelop%;changeLogPath=%changeLogPath%;sourceBranch=%sourceBranch%;destinationBranch=%destinationBranch%;assemblyPath=%assemblyPath%;customVersion=%customVersion%;genType=%genType%;solutionPath=%solutionPath%;packResultPath=%packResultPath%;packProjectsPath=%packProjectsPath%;testProjectsPath=%testProjectsPath%\"";
47+
48+
echo
49+
pause

docs/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### **v2.1.0.7515** [[RzR](mailto:108324929+I-RzR-I@users.noreply.github.com)] 15-12-2025
2+
* [0a85d60] (RzR) -> Auto commit uncommited files
3+
* [65d00ee] (RzR) -> Add code version generate scripts
4+
* [fbae95c] (RzR) -> Add `TryBuilder` helper.
5+
16
### v1.1.2.4376
27
-> Add `Func<Exception, Task<TResult>>` and `Func<Exception, TResult>` execute in catch block; <br/>
38

docs/usage.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,141 @@ ASYNC
7272
- `Task<TResult> TryToExecuteAsync<TResult, TLogger>(Func<Task<TResult>> execFunc, Func<TResult> onFailureResult, ILogger<TLogger> exceptionLogger, bool forceCallGarbageCollector = false)`
7373
- `Task<TResult> TryToExecuteAsync<TResult, TLogger>(Func<Task<TResult>> execFunc, Func<TResult> onFailureResult, ILogger<TLogger> exceptionLogger, Func<TResult> finallyExecFunc, bool forceCallGarbageCollector = false)`
7474
- `Task<TResult> TryToExecuteAsync<TResult, TLogger>(Func<Task<TResult>> execFunc, Func<Task<TResult>> onFailureResult, ILogger<TLogger> exceptionLogger, bool forceCallGarbageCollector = false)`
75-
- `Task<TResult> TryToExecuteAsync<TResult, TLogger>(Func<Task<TResult>> execFunc, Func<Task<TResult>> onFailureResult, ILogger<TLogger> exceptionLogger, Func<TResult> finallyExecFunc, bool forceCallGarbageCollector = false)`
75+
- `Task<TResult> TryToExecuteAsync<TResult, TLogger>(Func<Task<TResult>> execFunc, Func<Task<TResult>> onFailureResult, ILogger<TLogger> exceptionLogger, Func<TResult> finallyExecFunc, bool forceCallGarbageCollector = false)`
76+
-
77+
78+
79+
---
80+
81+
### TryBuilder -> A fluent code invoke API.
82+
83+
TryExecute is a fluent, strongly-typed Try / Catch / Finally execution framework for .NET with first-class support for:
84+
85+
- Async / sync / ValueTask
86+
- Typed catch handlers
87+
- Retry policies with backoff + jitter
88+
- Deterministic retry behavior for testing
89+
- Fallback values
90+
- Cancellation tokens
91+
- Multiple finally blocks (LIFO execution)
92+
- Allocation-aware design
93+
94+
This library is designed for explicit control flow, testability, and developer ergonomics.
95+
96+
97+
---
98+
##### The base methods
99+
100+
-> `Do` - Execute base code.
101+
102+
-> `Catch` - Catches the given handler (Exception catch handler).
103+
104+
-> `Finally` - The final execution function (in the finally block).
105+
106+
-> `Retry` - The retry execute function policy.
107+
108+
-> `Fallback` - The fallback execute function.
109+
110+
-> `Execute`/`ExecuteAsync` - Execute the given flow.
111+
112+
---
113+
114+
##### Basic Usage
115+
```csharp
116+
var result = await TryBuilder
117+
.Do(async ct =>
118+
{
119+
await Task.Delay(100, ct);
120+
return 42;
121+
})
122+
.ExecuteAsync();
123+
124+
if (result.Succeeded)
125+
{
126+
Console.WriteLine(result.Value);
127+
}
128+
else
129+
{
130+
Console.WriteLine(result.Exception);
131+
}
132+
```
133+
134+
##### Sync execution
135+
```csharp
136+
var result = TryBuilder
137+
.Do(() => 10 / 2)
138+
.Execute();
139+
140+
Console.WriteLine(result.Value);
141+
```
142+
143+
##### Catch Handlers (Typed)
144+
145+
You can attach multiple typed catch handlers.
146+
They are executed in order of registration.
147+
```csharp
148+
var result = await TryBuilder
149+
.Do<int>(async ct =>
150+
{
151+
throw new TimeoutException();
152+
})
153+
.Catch<TimeoutException>(ex =>
154+
{
155+
Console.WriteLine("Timeout occurred");
156+
})
157+
.Catch<Exception>(ex =>
158+
{
159+
Console.WriteLine("General error");
160+
})
161+
.ExecuteAsync();
162+
```
163+
164+
Notes:
165+
166+
Catch handlers **do not suppress execution**.
167+
168+
If you want to recover, use `Fallback`.
169+
170+
##### Finally Blocks (Multiple, LIFO)
171+
172+
You can register **multiple finally blocks**.
173+
They execute in reverse order (stack-style).
174+
175+
```csharp
176+
var result = await TryBuilder
177+
.Do(async ct =>
178+
{
179+
Console.WriteLine("Doing work");
180+
return 1;
181+
})
182+
.Finally(() => Console.WriteLine("Cleanup 1"))
183+
.Finally(() => Console.WriteLine("Cleanup 2"))
184+
.ExecuteAsync();
185+
```
186+
Execution order:
187+
188+
```xml
189+
Doing work
190+
Cleanup 2
191+
Cleanup 1
192+
```
193+
194+
Exceptions thrown inside `Finally` are swallowed by default.
195+
196+
197+
##### Retry Support
198+
**Basic retry**
199+
```csharp
200+
var result = await TryBuilder
201+
.Do(async ct =>
202+
{
203+
throw new IOException("temporary");
204+
})
205+
.Retry(new RetryOptions
206+
{
207+
MaxAttempts = 3
208+
})
209+
.ExecuteAsync();
210+
```
211+
212+

0 commit comments

Comments
 (0)