Skip to content

Commit 90e67c9

Browse files
committed
Add new methods with action exec in finally block.
1 parent ac2401b commit 90e67c9

6 files changed

Lines changed: 1377 additions & 5 deletions

File tree

Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
// ***********************************************************************
2+
// Assembly : RzR.Shared.Extensions.TryToExecute
3+
// Author : RzR
4+
// Created On : 2024-12-04 19:08
5+
//
6+
// Last Modified By : RzR
7+
// Last Modified On : 2024-12-04 19:08
8+
// ***********************************************************************
9+
// <copyright file="TryToExecuteActionFinally.cs" company="RzR SOFT & TECH">
10+
// Copyright © RzR. All rights reserved.
11+
// </copyright>
12+
//
13+
// <summary>
14+
// </summary>
15+
// ***********************************************************************
16+
17+
#region U S A G E S
18+
19+
using System;
20+
using System.Diagnostics;
21+
using TryToExecute.Extensions;
22+
using TryToExecute.Helpers;
23+
24+
#if NETSTANDARD2_0_OR_GREATER
25+
using Microsoft.Extensions.Logging;
26+
#endif
27+
28+
// ReSharper disable CheckNamespace
29+
30+
#endregion
31+
32+
namespace TryToExecute.CodeExec
33+
{
34+
/// <inheritdoc cref="CodeExec.TryCatchExecuteBase"/>
35+
public abstract partial class TryCatchExecuteBase
36+
{ /// -------------------------------------------------------------------------------------------------
37+
/// <summary>
38+
/// Try to execute.
39+
/// </summary>
40+
/// <typeparam name="TResult">Type of the result.</typeparam>
41+
/// <param name="execRequest">The execute request.</param>
42+
/// <param name="onFailureResult">The on failure result.</param>
43+
/// <param name="finallyExecAction">The finally execute action.</param>
44+
/// <param name="forceCallGarbageCollector">
45+
/// (Optional) True to force call garbage collector.
46+
/// </param>
47+
/// <returns>
48+
/// A TResult.
49+
/// </returns>
50+
/// =================================================================================================
51+
protected virtual TResult TryToExecute<TResult>(
52+
TResult execRequest,
53+
TResult onFailureResult,
54+
Action finallyExecAction,
55+
bool forceCallGarbageCollector = false)
56+
{
57+
try
58+
{
59+
return execRequest;
60+
}
61+
catch (Exception e)
62+
{
63+
#if DEBUG
64+
Debug.WriteLine(e);
65+
#if NETSTANDARD1_3_OR_GREATER
66+
Console.WriteLine(e);
67+
#endif
68+
#endif
69+
70+
return onFailureResult;
71+
}
72+
finally
73+
{
74+
if (finallyExecAction.IsNotNull())
75+
finallyExecAction?.Invoke();
76+
77+
if (forceCallGarbageCollector.IsTrue())
78+
TryToExecuteAppHelper.ForceCallGC();
79+
}
80+
}
81+
82+
/// -------------------------------------------------------------------------------------------------
83+
/// <summary>
84+
/// Try to execute.
85+
/// </summary>
86+
/// <typeparam name="TResult">Type of the result.</typeparam>
87+
/// <param name="execFunc">The execute function.</param>
88+
/// <param name="onFailureResult">The on failure result.</param>
89+
/// <param name="finallyExecAction">The finally execute action.</param>
90+
/// <param name="forceCallGarbageCollector">
91+
/// (Optional) True to force call garbage collector.
92+
/// </param>
93+
/// <returns>
94+
/// A TResult.
95+
/// </returns>
96+
/// =================================================================================================
97+
protected virtual TResult TryToExecute<TResult>(
98+
Func<TResult> execFunc,
99+
TResult onFailureResult,
100+
Action finallyExecAction,
101+
bool forceCallGarbageCollector = false)
102+
{
103+
execFunc.ThrowIfArgNull(nameof(execFunc));
104+
105+
try
106+
{
107+
return execFunc.Invoke();
108+
}
109+
catch (Exception e)
110+
{
111+
#if DEBUG
112+
Debug.WriteLine(e);
113+
#if NETSTANDARD1_3_OR_GREATER
114+
Console.WriteLine(e);
115+
#endif
116+
#endif
117+
118+
return onFailureResult;
119+
}
120+
finally
121+
{
122+
if (finallyExecAction.IsNotNull())
123+
finallyExecAction?.Invoke();
124+
125+
if (forceCallGarbageCollector.IsTrue())
126+
TryToExecuteAppHelper.ForceCallGC();
127+
}
128+
}
129+
130+
#if NETSTANDARD2_0_OR_GREATER
131+
/// -------------------------------------------------------------------------------------------------
132+
/// <summary>
133+
/// Try to execute.
134+
/// </summary>
135+
/// <typeparam name="TResult">Type of the result.</typeparam>
136+
/// <typeparam name="TLogger">Type of the logger.</typeparam>
137+
/// <param name="execFunc">The execute function.</param>
138+
/// <param name="onFailureResult">The on failure result.</param>
139+
/// <param name="exceptionLogger">The exception logger.</param>
140+
/// <param name="finallyExecAction">The finally execute action.</param>
141+
/// <param name="forceCallGarbageCollector">
142+
/// (Optional) True to force call garbage collector.
143+
/// </param>
144+
/// <returns>
145+
/// A TResult.
146+
/// </returns>
147+
/// =================================================================================================
148+
protected virtual TResult TryToExecute<TResult, TLogger>(
149+
Func<TResult> execFunc,
150+
TResult onFailureResult,
151+
ILogger<TLogger> exceptionLogger,
152+
Action finallyExecAction,
153+
bool forceCallGarbageCollector = false)
154+
{
155+
execFunc.ThrowIfArgNull(nameof(execFunc));
156+
exceptionLogger.ThrowIfArgNull(nameof(exceptionLogger));
157+
158+
try
159+
{
160+
return execFunc.Invoke();
161+
}
162+
catch (Exception e)
163+
{
164+
#if DEBUG
165+
Debug.WriteLine(e);
166+
#if NETSTANDARD1_3_OR_GREATER
167+
Console.WriteLine(e);
168+
#endif
169+
#endif
170+
exceptionLogger.LogError(e, DefaultMessageHelper.InternalErrorOnTryExecute);
171+
172+
return onFailureResult;
173+
}
174+
finally
175+
{
176+
if (finallyExecAction.IsNotNull())
177+
finallyExecAction?.Invoke();
178+
179+
if (forceCallGarbageCollector.IsTrue())
180+
TryToExecuteAppHelper.ForceCallGC();
181+
}
182+
}
183+
#endif
184+
185+
/// -------------------------------------------------------------------------------------------------
186+
/// <summary>
187+
/// Try to execute.
188+
/// </summary>
189+
/// <typeparam name="TResult">Type of the result.</typeparam>
190+
/// <param name="execFunc">The execute function.</param>
191+
/// <param name="onFailureResult">The on failure result.</param>
192+
/// <param name="finallyExecAction">The finally execute action.</param>
193+
/// <param name="forceCallGarbageCollector">
194+
/// (Optional) True to force call garbage collector.
195+
/// </param>
196+
/// <returns>
197+
/// A TResult.
198+
/// </returns>
199+
/// =================================================================================================
200+
protected virtual TResult TryToExecute<TResult>(
201+
Func<TResult> execFunc,
202+
Func<TResult> onFailureResult,
203+
Action finallyExecAction,
204+
bool forceCallGarbageCollector = false)
205+
{
206+
execFunc.ThrowIfArgNull(nameof(execFunc));
207+
onFailureResult.ThrowIfArgNull(nameof(onFailureResult));
208+
209+
try
210+
{
211+
return execFunc.Invoke();
212+
}
213+
catch (Exception e)
214+
{
215+
#if DEBUG
216+
Debug.WriteLine(e);
217+
#if NETSTANDARD1_3_OR_GREATER
218+
Console.WriteLine(e);
219+
#endif
220+
#endif
221+
222+
return onFailureResult.Invoke();
223+
}
224+
finally
225+
{
226+
if (finallyExecAction.IsNotNull())
227+
finallyExecAction?.Invoke();
228+
229+
if (forceCallGarbageCollector.IsTrue())
230+
TryToExecuteAppHelper.ForceCallGC();
231+
}
232+
}
233+
234+
#if NETSTANDARD2_0_OR_GREATER
235+
/// -------------------------------------------------------------------------------------------------
236+
/// <summary>
237+
/// Try to execute.
238+
/// </summary>
239+
/// <typeparam name="TResult">Type of the result.</typeparam>
240+
/// <typeparam name="TLogger">Type of the logger.</typeparam>
241+
/// <param name="execFunc">The execute function.</param>
242+
/// <param name="onFailureResult">The on failure result.</param>
243+
/// <param name="exceptionLogger">The exception logger.</param>
244+
/// <param name="finallyExecAction">The finally execute action.</param>
245+
/// <param name="forceCallGarbageCollector">
246+
/// (Optional) True to force call garbage collector.
247+
/// </param>
248+
/// <returns>
249+
/// A TResult.
250+
/// </returns>
251+
/// =================================================================================================
252+
protected virtual TResult TryToExecute<TResult, TLogger>(
253+
Func<TResult> execFunc,
254+
Func<TResult> onFailureResult,
255+
ILogger<TLogger> exceptionLogger,
256+
Action finallyExecAction,
257+
bool forceCallGarbageCollector = false)
258+
{
259+
execFunc.ThrowIfArgNull(nameof(execFunc));
260+
onFailureResult.ThrowIfArgNull(nameof(onFailureResult));
261+
exceptionLogger.ThrowIfArgNull(nameof(exceptionLogger));
262+
263+
try
264+
{
265+
return execFunc.Invoke();
266+
}
267+
catch (Exception e)
268+
{
269+
#if DEBUG
270+
Debug.WriteLine(e);
271+
#if NETSTANDARD1_3_OR_GREATER
272+
Console.WriteLine(e);
273+
#endif
274+
#endif
275+
exceptionLogger.LogError(e, DefaultMessageHelper.InternalErrorOnTryExecute);
276+
277+
return onFailureResult.Invoke();
278+
}
279+
finally
280+
{
281+
if (finallyExecAction.IsNotNull())
282+
finallyExecAction?.Invoke();
283+
284+
if (forceCallGarbageCollector.IsTrue())
285+
TryToExecuteAppHelper.ForceCallGC();
286+
}
287+
}
288+
#endif
289+
}
290+
}

0 commit comments

Comments
 (0)