From c25845c8134b08d6827a9d4d101b26bd15cd7ebc Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Tue, 5 Jan 2021 16:45:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86runner=E4=B8=AD=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E6=89=A9=E5=BC=A0=E6=96=B9=E6=B3=95=E7=8B=AC=E7=AB=8B=E5=87=BA?= =?UTF-8?q?=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Falcon.StoredProcedureRunner/IRunner.cs | 19 ------- .../IRunnerExtend.cs | 54 +++++++++++++++++++ src/Falcon.StoredProcedureRunner/Runner.cs | 45 ---------------- 3 files changed, 54 insertions(+), 64 deletions(-) create mode 100644 src/Falcon.StoredProcedureRunner/IRunnerExtend.cs diff --git a/src/Falcon.StoredProcedureRunner/IRunner.cs b/src/Falcon.StoredProcedureRunner/IRunner.cs index 0ac9fba..569bd3f 100644 --- a/src/Falcon.StoredProcedureRunner/IRunner.cs +++ b/src/Falcon.StoredProcedureRunner/IRunner.cs @@ -17,25 +17,6 @@ namespace Falcon.StoredProcedureRunner /// 参数数据 int Execute(DbContext db,TPrarmType data); - /// - /// 根据模型定义参数执行存储过程进行查询,参数类型必须定义ReturnTypeAttribute特性 - /// - /// 存储过程参数类型 - /// 数据上下文 - /// 存储过程参数 - /// 返回类型枚举FalconSPReturnTypeAttribute定义的类型枚举。 - IEnumerable Run(DbContext db,TPrarmType data); - - /// - /// 通过数据库上下文执行存储过程,并返回查询结果 - /// - /// 参数类型 - /// 返回结果项类型 - /// 数据上下文 - /// 参数数据 - /// 查询结果枚举 - IEnumerable Run(DbContext db,TPrarmType data) where TReturnType : class, new(); - /// /// 通过数据库上下文执行存储过程,并返回查询结果 /// diff --git a/src/Falcon.StoredProcedureRunner/IRunnerExtend.cs b/src/Falcon.StoredProcedureRunner/IRunnerExtend.cs new file mode 100644 index 0000000..fbf76af --- /dev/null +++ b/src/Falcon.StoredProcedureRunner/IRunnerExtend.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Text; +using Microsoft.EntityFrameworkCore; + +namespace Falcon.StoredProcedureRunner +{ + /// + /// 执行器扩展 + /// + public static class IRunnerExtend + { + /// + /// 根据模型定义参数执行存储过程进行查询,参数类型必须定义ReturnTypeAttribute特性 + /// + /// 存储过程参数类型 + /// 执行器 + /// 数据上下文 + /// 存储过程参数 + /// 返回类型枚举FalconSPReturnTypeAttribute定义的类型枚举。 + public static IEnumerable Run(this IRunner runner,DbContext db,TPrarmType data) { + var dType = typeof(TPrarmType); + var attr = dType.GetCustomAttribute(); + if(attr != null && attr is FalconSPReturnTypeAttribute pna && pna.ReturnType != null) { + return runner.Run(db,dType,pna.ReturnType,data); + } else { + throw new ReturnTypeException(); + } + } + + /// + /// 通过数据库上下文执行存储过程,并返回查询结果 + /// + /// 参数类型 + /// 返回结果项类型 + /// 执行器 + /// 数据上下文 + /// 参数数据 + /// 查询结果枚举 + public static IEnumerable Run(this IRunner runner,DbContext db,TPrarmType data) { + try { + return runner.Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast(); + } catch(InvalidCastException ice) { + throw new ReturnTypeCastException(ice); + } catch(Exception ex) { + throw ex; + } + } + + } +} diff --git a/src/Falcon.StoredProcedureRunner/Runner.cs b/src/Falcon.StoredProcedureRunner/Runner.cs index 4f226bf..5d67b18 100644 --- a/src/Falcon.StoredProcedureRunner/Runner.cs +++ b/src/Falcon.StoredProcedureRunner/Runner.cs @@ -28,39 +28,6 @@ namespace Falcon.StoredProcedureRunner return db.Database.ExecuteSqlRaw(str,parms); } - /// - /// 根据模型定义参数执行存储过程进行查询,参数类型必须定义ReturnTypeAttribute特性 - /// - /// 存储过程参数类型 - /// 数据上下文 - /// 存储过程参数 - /// 返回类型枚举FalconSPReturnTypeAttribute定义的类型枚举。 - public IEnumerable Run(DbContext db,TPrarmType data) { - var rType = getRequtnType(typeof(TPrarmType)); - if(rType == null) { - throw new ReturnTypeException(); - } - return Run(db,typeof(TPrarmType),rType,data); - } - - /// - /// 通过数据库上下文执行存储过程,并返回查询结果 - /// - /// 参数类型 - /// 返回结果项类型 - /// 数据上下文 - /// 参数数据 - /// 查询结果枚举 - public IEnumerable Run(DbContext db,TPrarmType data) where TReturnType : class, new() { - try { - return Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast(); - } catch(InvalidCastException ice) { - throw new ReturnTypeCastException(ice); - } catch(Exception ex) { - throw ex; - } - } - /// /// 通过数据库上下文执行存储过程,并返回查询结果 /// @@ -121,18 +88,6 @@ namespace Falcon.StoredProcedureRunner return pramType.Name; } - /// - /// 获取存储过程名返回值类型 - /// - /// 返回值类型 - private static Type getRequtnType(Type pramType) { - var attr = pramType.GetCustomAttribute(); - if(attr != null && attr is FalconSPReturnTypeAttribute pna && pna.ReturnType != null) { - return pna.ReturnType; - } - return null; - } - /// /// 获取存储过程参数枚举 ///