-
Notifications
You must be signed in to change notification settings - Fork 455
Interceptor
so edited this page May 22, 2021
·
1 revision
Chloe 借鉴 EF,支持了拦截器功能。通过该功能可以拦截 sql 语句和监视 sql 执行用时。
拦截器定义:
自定义的拦截器必须实现 IDbCommandInterceptor 接口。
class DbCommandInterceptor : IDbCommandInterceptor
{
/* 执行 DbCommand.ExecuteReader() 时调用 */
public void ReaderExecuting(IDbCommand command, DbCommandInterceptionContext<IDataReader> interceptionContext)
{
interceptionContext.DataBag.Add("startTime", DateTime.Now);
Console.WriteLine(command.CommandText);
}
/* 执行 DbCommand.ExecuteReader() 后调用 */
public void ReaderExecuted(IDbCommand command, DbCommandInterceptionContext<IDataReader> interceptionContext)
{
DateTime startTime = (DateTime)(interceptionContext.DataBag["startTime"]);
Console.WriteLine(DateTime.Now.Subtract(startTime).TotalMilliseconds);
if (interceptionContext.Exception == null)
Console.WriteLine(interceptionContext.Result.FieldCount);
}
/* 执行 DbCommand.ExecuteNonQuery() 时调用 */
public void NonQueryExecuting(IDbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
interceptionContext.DataBag.Add("startTime", DateTime.Now);
Console.WriteLine(command.CommandText);
}
/* 执行 DbCommand.ExecuteNonQuery() 后调用 */
public void NonQueryExecuted(IDbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
DateTime startTime = (DateTime)(interceptionContext.DataBag["startTime"]);
Console.WriteLine(DateTime.Now.Subtract(startTime).TotalMilliseconds);
if (interceptionContext.Exception == null)
Console.WriteLine(interceptionContext.Result);
}
/* 执行 DbCommand.ExecuteScalar() 时调用 */
public void ScalarExecuting(IDbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
interceptionContext.DataBag.Add("startTime", DateTime.Now);
Console.WriteLine(command.CommandText);
}
/* 执行 DbCommand.ExecuteScalar() 后调用 */
public void ScalarExecuted(IDbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
DateTime startTime = (DateTime)(interceptionContext.DataBag["startTime"]);
Console.WriteLine(DateTime.Now.Subtract(startTime).TotalMilliseconds);
if (interceptionContext.Exception == null)
Console.WriteLine(interceptionContext.Result);
}
}
全局拦截器:
这种用法针对所有的 DbContext 有效,建议在程序启动时添加。
IDbCommandInterceptor interceptor = new DbCommandInterceptor();
DbInterception.Add(interceptor);
单个DbContext拦截器:
这种用法只会对单个 DbContext 有效,建议在创建 DbContext 时添加。
IDbContext context = new MsSqlContext("Your connection string");
IDbCommandInterceptor interceptor = new DbCommandInterceptor();
context.Session.AddInterceptor(interceptor);
熬夜的时候容易引起脱发,熬夜时人的免疫力会下降,还会引起内分泌的变化,导致皮脂分泌增多,这些因素都不利于头发的生长,有可能引起脱发。如果出现熬夜脱发的情况,需要及时纠正日常的生活习惯,合理安排休息时间,早睡早起,适当的锻炼身体,多进食优质的蛋白质,增强身体的抵抗力,促进头发的生长。
发量有限,远离996!!!