Skip to content
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

Add query execution listeners #2078

Open
ah1508 opened this issue Apr 25, 2024 · 1 comment
Open

Add query execution listeners #2078

ah1508 opened this issue Apr 25, 2024 · 1 comment

Comments

@ah1508
Copy link

ah1508 commented Apr 25, 2024

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.

@sovetkin
Copy link

sovetkin commented May 4, 2024

Hello Dapper`s community!!!

This is really useful feature, that I'm waiting for too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants