Skip to content

Commit

Permalink
#941 Introduced the TraceKeys and introduced some initial changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikependon committed Sep 10, 2022
1 parent 8466556 commit 80ef874
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 1 deletion.
71 changes: 71 additions & 0 deletions RepoDb.Core/RepoDb/CancellableTraceLog.cs
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.Data;

namespace RepoDb
{
/// <summary>
/// A cancellable tracing log object that is used in the tracing operations. This class holds the cancellable operations for all tracing logs.
/// </summary>
[Obsolete("Use the 'CancellableTraceLog<TResult>' class instead.")]
public class CancellableTraceLog : TraceLog
{
/// <summary>
Expand All @@ -25,6 +28,8 @@ public class CancellableTraceLog : TraceLog
null)
{ }

#region Properties

/// <summary>
/// Gets a value whether the operation is cancelled.
/// </summary>
Expand All @@ -35,6 +40,10 @@ public class CancellableTraceLog : TraceLog
/// </summary>
public bool IsThrowException { get; private set; }

#endregion

#region Methods

/// <summary>
/// Cancel the current executing repository operation.
/// </summary>
Expand All @@ -44,5 +53,67 @@ public void Cancel(bool throwException = true)
IsCancelled = true;
IsThrowException = throwException;
}

#endregion
}

/// <summary>
/// A cancellable tracing log object that is used in the tracing operations. This class holds the cancellable operations for all tracing logs.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
public class CancellableTraceLog<TResult> : TraceLog<TResult>
{
/// <summary>
/// Creates a new instance of <see cref="CancellableTraceLog"/> object.
/// </summary>
/// <param name="sessionId"></param>
/// <param name="statement"></param>
/// <param name="parameters"></param>
protected internal CancellableTraceLog(Guid sessionId,
string statement,
IEnumerable<IDbDataParameter> parameters)
: base(sessionId)
{
Statement = statement;
Parameters = parameters;
}

#region Properties

/// <summary>
/// Gets or sets the SQL Statement used on the actual operation execution.
/// </summary>
public string Statement { get; set; }

/// <summary>
/// Gets the parameters used on the actual operation.
/// </summary>
public IEnumerable<IDbDataParameter> Parameters { get; }

/// <summary>
/// Gets a value whether the operation is cancelled.
/// </summary>
public bool IsCancelled { get; private set; }

/// <summary>
/// Gets a value whether an exception will be thrown after the <see cref="Cancel(bool)"/> method was called.
/// </summary>
public bool IsThrowException { get; private set; }

#endregion

#region Methods

/// <summary>
/// Cancel the current executing repository operation.
/// </summary>
/// <param name="throwException">If true, an exception will be thrown.</param>
public void Cancel(bool throwException = true)
{
IsCancelled = true;
IsThrowException = throwException;
}

#endregion
}
}
53 changes: 52 additions & 1 deletion RepoDb.Core/RepoDb/Interfaces/ITrace.cs
@@ -1,11 +1,60 @@
namespace RepoDb.Interfaces
using System.Threading;
using System.Threading.Tasks;

namespace RepoDb.Interfaces
{
/// <summary>
/// An interface that is used to mark a class to be usable for tracing the operations. A trace object is being used to provide an auditing and debugging capability when executing the actual operation.
/// The caller can modify the SQL Statements or the parameters being passed prior the actual execution, or even cancel the prior-execution.
/// </summary>
public interface ITrace
{
#region New

#region Sync

/// <summary>
/// A method that is being raised before the actual execution of the operation.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="log">The cancellable trace log object referenced by the execution.</param>
void BeforeExecution<TResult>(CancellableTraceLog<TResult> log);

/// <summary>
/// A method that is being raised after the actual execution of the operation.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="log">The trace log object referenced by the execution.</param>
void AfterExcecution<TResult>(TraceLog<TResult> log);

#endregion

#region Async

/// <summary>
/// A method that is being raised before the actual execution of the operation in an asynchronous way.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="log">The cancellable trace log object referenced by the execution.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
Task BeforeExecutionAsync<TResult>(CancellableTraceLog<TResult> log,
CancellationToken cancellationToken = default);

/// <summary>
/// A method that is being raised after the actual execution of the operation in an asynchronous way.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="log">The trace log object referenced by the execution.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
Task AfterExecutionAsync<TResult>(TraceLog<TResult> log,
CancellationToken cancellationToken = default);

#endregion

#endregion

#region Old

#region Average

/// <summary>
Expand Down Expand Up @@ -454,5 +503,7 @@ public interface ITrace
void AfterUpdateAll(TraceLog log);

#endregion

#endregion
}
}
118 changes: 118 additions & 0 deletions RepoDb.Core/RepoDb/TraceKeys.cs
@@ -0,0 +1,118 @@
namespace RepoDb
{
/// <summary>
/// A class that holds the contant values of the operation tracking keys.
/// </summary>
public static class TraceKeys
{
/// <summary>
/// The trace key for the 'Average' operation.
/// </summary>
public const string Average = "Average";

/// <summary>
/// The trace key for the 'AverageAll' operation.
/// </summary>
public const string AverageAll = "AverageAll";

/// <summary>
/// The trace key for the 'BatchQuery' operation.
/// </summary>
public const string BatchQuery = "BatchQuery";

/// <summary>
/// The trace key for the 'Count' operation.
/// </summary>
public const string Count = "Count";

/// <summary>
/// The trace key for the 'CountAll' operation.
/// </summary>
public const string CountAll = "CountAll";

/// <summary>
/// The trace key for the 'Delete' operation.
/// </summary>
public const string Delete = "Delete";

/// <summary>
/// The trace key for the 'DeleteAll' operation.
/// </summary>
public const string DeleteAll = "DeleteAll";

/// <summary>
/// The trace key for the 'Exists' operation.
/// </summary>
public const string Exists = "Exists";

/// <summary>
/// The trace key for the 'Insert' operation.
/// </summary>
public const string Insert = "Insert";

/// <summary>
/// The trace key for the 'InsertAll' operation.
/// </summary>
public const string InsertAll = "InsertAll";

/// <summary>
/// The trace key for the 'Max' operation.
/// </summary>
public const string Max = "Max";

/// <summary>
/// The trace key for the 'MaxAll' operation.
/// </summary>
public const string MaxAll = "MaxAll";

/// <summary>
/// The trace key for the 'Min' operation.
/// </summary>
public const string Min = "Min";

/// <summary>
/// The trace key for the 'MinAll' operation.
/// </summary>
public const string MinAll = "MinAll";

/// <summary>
/// The trace key for the 'Query' operation.
/// </summary>
public const string Query = "Query";

/// <summary>
/// The trace key for the 'QueryAll' operation.
/// </summary>
public const string QueryAll = "QueryAll";

/// <summary>
/// The trace key for the 'QueryMultiple' operation.
/// </summary>
public const string QueryMultiple = "QueryMultiple";

/// <summary>
/// The trace key for the 'Sum' operation.
/// </summary>
public const string Sum = "Sum";

/// <summary>
/// The trace key for the 'SumAll' operation.
/// </summary>
public const string SumAll = "SumAll";

/// <summary>
/// The trace key for the 'Truncate' operation.
/// </summary>
public const string Truncate = "Truncate";

/// <summary>
/// The trace key for the 'Update' operation.
/// </summary>
public const string Update = "Update";

/// <summary>
/// The trace key for the 'UpdateAll' operation.
/// </summary>
public const string UpdateAll = "UpdateAll";
}
}
51 changes: 51 additions & 0 deletions RepoDb.Core/RepoDb/TraceLog.cs
@@ -1,10 +1,13 @@
using System;
using System.Collections.Generic;
using System.Data;

namespace RepoDb
{
/// <summary>
/// A class that holds the information of the tracing operations.
/// </summary>
[Obsolete("Use the 'TraceLog<TResult>' class instead.")]
public class TraceLog
{
/// <summary>
Expand All @@ -31,6 +34,8 @@ public class TraceLog
}
}

#region Properties

/// <summary>
/// Gets the session identifier of the current trace.
/// </summary>
Expand All @@ -55,5 +60,51 @@ public class TraceLog
/// Gets the actual length of the operation execution.
/// </summary>
public TimeSpan ExecutionTime { get; }

#endregion
}

/// <summary>
/// A class that holds the information of the tracing operations.
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
public class TraceLog<TResult>
{
/// <summary>
/// Creates an instance of <see cref="TraceLog"/> class.
/// </summary>
/// <param name="sessionId"></param>
/// <param name="result"></param>
/// <param name="executionTime"></param>
protected internal TraceLog(Guid sessionId,
TResult result = default,
TimeSpan? executionTime = null)
{
SessionId = sessionId;
Result = result;
if (executionTime != null)
{
ExecutionTime = executionTime.Value;
}
}

#region Properties

/// <summary>
/// Gets the session identifier used by the current trace.
/// </summary>
public Guid SessionId { get; }

/// <summary>
/// Gets the actual result of the operation.
/// </summary>
public TResult Result { get; }

/// <summary>
/// Gets the actual length of the operation execution.
/// </summary>
public TimeSpan ExecutionTime { get; }

#endregion
}
}

0 comments on commit 80ef874

Please sign in to comment.