Add query execution listeners
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
Dapper could offer several AddListener methods: before query execution, after query execution, after exception.
AddBeforeQueryExectutionListener(Action<DbCommand> action, bool failOnError = false);
AddAfterQueryExectutionListener(Action<DbCommand, object/*the result*/, long /*execution time*/> action, bool failOnError = false);
AddAfterQueryExceptionListener(Action<DbCommand, Exception> action);
failOnError: should an exception thrown by the action stop the query execution or should it be ignored ?
execution time could be replaced by a Statistics object (connection acquisition time, query execution time, mapping execution time...).
These methods could be static:
Dapper.Listeners.AddAfterQueryExecutionListener((command, result, executionTime) => {
Console.WriteLine($"it took {executionTime} to execute {command.CommandText}");
});
namespace Dapper;
public static class Listeners
{
public static bool Enabled { get; set ;} // To enabled or disable listeners at runtime
internal static List<(Action<DbCommand> action, bool failOnError)> BeforeExecutionListeners { get; } = [];
internal static List<(Action<DbCommand, object, long> action, bool failOnError)> AfterExecutionListeners { get; } = [];
internal static List<Action<DbCommand, Exception>> AfterExceptionListeners { get; } = [];
public static void AddBeforeExecutionListener(Action<DbCommand> action, bool failOnError = false)
{
BeforeExecutionListeners.Add((action, failOnError));
Enabled = true;
}
public static void AddAfterQueryExecutionListener(Action<DbCommand, object, long> action, bool failOnError = false)
{
AfterExecutionListeners.Add((action, failOnError));
Enabled = true;
}
public static void AddAfterExceptionListeners(Action<DbCommand, Exception> action)
{
AfterExceptionListeners.Add(action);
Enabled = true;
}
}
With named listeners, RemoveListener methods could also be implemented.
SqlMapper Query and Execute method would check Listeners.Enabled value and, if it is true, call the listeners before and after query executions.
It would be useful for profiling and exception handling, in my case I need to expose the execution time of some queries to Prometheus.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the SqlMapper Query and Execute methods, which the issue identifies as the integration points. Define the listener API, exception behavior, timing or Statistics shape, and removal semantics before implementing; done means those paths consistently support the agreed before, after, and exception callbacks with profiling data available.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, sql
- Domain
- backend, observability
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100