-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
V9.0.0/task based sceanario relocation #95
Changes from 1 commit
cdda21b
87e60bf
ecc8a3e
2aed276
bca8c62
a2e4fbc
ed49cef
85b2456
e0beb79
40ac494
f1799f8
92c163b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Cuemon.Threading | ||
{ | ||
/// <summary> | ||
/// Provides an easy way of invoking an <see cref="Action" /> delegate regardless of the amount of parameters provided. | ||
/// </summary> | ||
/// <typeparam name="TTuple">The type of the n-tuple representation of a <see cref="Template"/>.</typeparam> | ||
public sealed class TaskActionFactory<TTuple> : TemplateFactory<TTuple> where TTuple : Template | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="TaskActionFactory{TTuple}"/> class. | ||
/// </summary> | ||
/// <param name="method">The <see cref="Task"/> based function delegate to invoke.</param> | ||
/// <param name="tuple">The n-tuple argument of <paramref name="method"/>.</param> | ||
public TaskActionFactory(Func<TTuple, CancellationToken, Task> method, TTuple tuple) : this(method, tuple, method) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="TaskActionFactory{TTuple}"/> class. | ||
/// </summary> | ||
/// <param name="method">The <see cref="Task"/> based function delegate to invoke.</param> | ||
/// <param name="tuple">The n-tuple argument of <paramref name="method"/>.</param> | ||
/// <param name="originalDelegate">The original delegate wrapped by <paramref name="method"/>.</param> | ||
public TaskActionFactory(Func<TTuple, CancellationToken, Task> method, TTuple tuple, Delegate originalDelegate) : base(tuple, originalDelegate != null) | ||
{ | ||
Method = method; | ||
DelegateInfo = Decorator.RawEnclose(method).ResolveDelegateInfo(originalDelegate); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Declare the 'DelegateInfo' Property You assign a value to If needed, declare the property as follows (replace +public DelegateInfoType DelegateInfo { get; private set; }
|
||
} | ||
|
||
/// <summary> | ||
/// Gets the delegate to invoke. | ||
/// </summary> | ||
/// <value>The delegate to invoke.</value> | ||
private Func<TTuple, CancellationToken, Task> Method { get; } | ||
|
||
/// <summary> | ||
/// Executes the delegate associated with this instance. | ||
/// </summary> | ||
/// <param name="ct">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> | ||
/// <returns>A task that represents the asynchronous operation.</returns> | ||
/// <exception cref="InvalidOperationException"> | ||
/// No delegate was specified on the factory. | ||
/// </exception> | ||
/// <exception cref="OperationCanceledException"> | ||
/// The <paramref name="ct"/> was canceled. | ||
/// </exception> | ||
public Task ExecuteMethodAsync(CancellationToken ct) | ||
{ | ||
ThrowIfNoValidDelegate(Condition.IsNull(Method)); | ||
ct.ThrowIfCancellationRequested(); | ||
return Method.Invoke(GenericArguments, ct); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a shallow copy of the current <see cref="TaskActionFactory{TTuple}"/> object. | ||
/// </summary> | ||
/// <returns>A new <see cref="TaskActionFactory{TTuple}"/> that is a copy of this instance.</returns> | ||
/// <remarks>When thread safety is required this is the method to invoke.</remarks> | ||
public override TemplateFactory<TTuple> Clone() | ||
{ | ||
return new TaskActionFactory<TTuple>(Method, GenericArguments.Clone() as TTuple); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure Safe Casting in 'Clone' Method In the return new TaskActionFactory<TTuple>(Method, GenericArguments.Clone() as TTuple); Using Consider the following adjustment: -return new TaskActionFactory<TTuple>(Method, GenericArguments.Clone() as TTuple);
+return new TaskActionFactory<TTuple>(Method, (TTuple)GenericArguments.Clone()); Or, if there's a possibility of var clonedArguments = GenericArguments.Clone() as TTuple;
if (clonedArguments == null)
{
throw new InvalidOperationException("Cloning of GenericArguments failed.");
}
return new TaskActionFactory<TTuple>(Method, clonedArguments); |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,69 @@ | ||||||||||||||||||||||||||||||||||||||
using System; | ||||||||||||||||||||||||||||||||||||||
using System.Threading; | ||||||||||||||||||||||||||||||||||||||
using System.Threading.Tasks; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
namespace Cuemon.Threading | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||
/// Provides an easy way of invoking an <see cref="Func{TResult}" /> function delegate regardless of the amount of parameters provided. | ||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update XML documentation to reflect the correct delegate type The summary currently mentions Apply this diff to correct the documentation: -/// Provides an easy way of invoking an <see cref="Func{TResult}" /> function delegate regardless of the amount of parameters provided.
+/// Provides an easy way of invoking a <see cref="Func{TTuple, CancellationToken, Task{TResult}}"/> function delegate regardless of the amount of parameters provided. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
/// <typeparam name="TTuple">The type of the n-tuple representation of a <see cref="Template"/>.</typeparam> | ||||||||||||||||||||||||||||||||||||||
/// <typeparam name="TResult">The type of the return value of the function delegate <see cref="Method"/>.</typeparam> | ||||||||||||||||||||||||||||||||||||||
public sealed class TaskFuncFactory<TTuple, TResult> : TemplateFactory<TTuple> where TTuple : Template | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||
/// Initializes a new instance of the <see cref="TaskFuncFactory{TTuple,TResult}"/> class. | ||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||
/// <param name="method">The function delegate to invoke.</param> | ||||||||||||||||||||||||||||||||||||||
/// <param name="tuple">The n-tuple argument of <paramref name="method"/>.</param> | ||||||||||||||||||||||||||||||||||||||
public TaskFuncFactory(Func<TTuple, CancellationToken, Task<TResult>> method, TTuple tuple) : this(method, tuple, method) | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||
/// Initializes a new instance of the <see cref="TaskFuncFactory{TTuple,TResult}"/> class. | ||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||
/// <param name="method">The function delegate to invoke.</param> | ||||||||||||||||||||||||||||||||||||||
/// <param name="tuple">The n-tuple argument of <paramref name="method"/>.</param> | ||||||||||||||||||||||||||||||||||||||
/// <param name="originalDelegate">The original delegate wrapped by <paramref name="method"/>.</param> | ||||||||||||||||||||||||||||||||||||||
public TaskFuncFactory(Func<TTuple, CancellationToken, Task<TResult>> method, TTuple tuple, Delegate originalDelegate) : base(tuple, originalDelegate != null) | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
Method = method; | ||||||||||||||||||||||||||||||||||||||
DelegateInfo = Decorator.RawEnclose(method).ResolveDelegateInfo(originalDelegate); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add null checks for constructor parameters To prevent potential Apply this diff to add null checks: public TaskFuncFactory(Func<TTuple, CancellationToken, Task<TResult>> method, TTuple tuple, Delegate originalDelegate) : base(tuple, originalDelegate != null)
{
+ if (method == null) throw new ArgumentNullException(nameof(method));
+ if (tuple == null) throw new ArgumentNullException(nameof(tuple));
Method = method;
DelegateInfo = Decorator.RawEnclose(method).ResolveDelegateInfo(originalDelegate);
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||
/// Gets the function delegate to invoke. | ||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||
/// <value>The function delegate to invoke.</value> | ||||||||||||||||||||||||||||||||||||||
private Func<TTuple, CancellationToken, Task<TResult>> Method { get; set; } | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||
/// Executes the function delegate associated with this instance. | ||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||
/// <param name="ct">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> | ||||||||||||||||||||||||||||||||||||||
/// <returns>A task that represents the asynchronous operation. The task result contains the return value of the function delegate associated with this instance.</returns> | ||||||||||||||||||||||||||||||||||||||
/// <exception cref="InvalidOperationException"> | ||||||||||||||||||||||||||||||||||||||
/// No delegate was specified on the factory. | ||||||||||||||||||||||||||||||||||||||
/// </exception> | ||||||||||||||||||||||||||||||||||||||
/// <exception cref="OperationCanceledException"> | ||||||||||||||||||||||||||||||||||||||
/// The <paramref name="ct"/> was canceled. | ||||||||||||||||||||||||||||||||||||||
/// </exception> | ||||||||||||||||||||||||||||||||||||||
public Task<TResult> ExecuteMethodAsync(CancellationToken ct) | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
ThrowIfNoValidDelegate(Condition.IsNull(Method)); | ||||||||||||||||||||||||||||||||||||||
ct.ThrowIfCancellationRequested(); | ||||||||||||||||||||||||||||||||||||||
return Method.Invoke(GenericArguments, ct); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Handle exceptions from the invoked method While invoking Apply this diff to handle exceptions: public Task<TResult> ExecuteMethodAsync(CancellationToken ct)
{
ThrowIfNoValidDelegate(Condition.IsNull(Method));
ct.ThrowIfCancellationRequested();
- return Method.Invoke(GenericArguments, ct);
+ try
+ {
+ return Method.Invoke(GenericArguments, ct);
+ }
+ catch (Exception ex)
+ {
+ // Handle or log the exception as appropriate
+ throw new OperationCanceledException("An error occurred during method execution.", ex, ct);
+ }
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
/// <summary> | ||||||||||||||||||||||||||||||||||||||
/// Creates a shallow copy of the current <see cref="TaskFuncFactory{TTuple,TResult}"/> object. | ||||||||||||||||||||||||||||||||||||||
/// </summary> | ||||||||||||||||||||||||||||||||||||||
/// <returns>A new <see cref="TaskFuncFactory{TTuple,TResult}"/> that is a copy of this instance.</returns> | ||||||||||||||||||||||||||||||||||||||
/// <remarks>When thread safety is required this is the method to invoke.</remarks> | ||||||||||||||||||||||||||||||||||||||
public override TemplateFactory<TTuple> Clone() | ||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||
return new TaskFuncFactory<TTuple, TResult>(Method, GenericArguments.Clone() as TTuple); | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update Summary to Reflect Delegate Type
The XML documentation mentions invoking an
<see cref="Action" />
delegate, but the class actually works with aFunc<TTuple, CancellationToken, Task>
. To avoid confusion, consider updating the summary to accurately reflect the delegate type being invoked.Apply this diff to correct the documentation:
📝 Committable suggestion