将runner中一些扩张方法独立出来

This commit is contained in:
falcon 2021-01-05 16:45:12 +08:00
parent 6b137ee6e8
commit c25845c813
3 changed files with 54 additions and 64 deletions

View File

@ -17,25 +17,6 @@ namespace Falcon.StoredProcedureRunner
/// <param name="data">参数数据</param> /// <param name="data">参数数据</param>
int Execute<TPrarmType>(DbContext db,TPrarmType data); int Execute<TPrarmType>(DbContext db,TPrarmType data);
/// <summary>
/// 根据模型定义参数执行存储过程进行查询参数类型必须定义ReturnTypeAttribute特性
/// </summary>
/// <typeparam name="TPrarmType">存储过程参数类型</typeparam>
/// <param name="db">数据上下文</param>
/// <param name="data">存储过程参数</param>
/// <returns>返回类型枚举FalconSPReturnTypeAttribute定义的类型枚举。</returns>
IEnumerable<object> Run<TPrarmType>(DbContext db,TPrarmType data);
/// <summary>
/// 通过数据库上下文执行存储过程,并返回查询结果
/// </summary>
/// <typeparam name="TPrarmType">参数类型</typeparam>
/// <typeparam name="TReturnType">返回结果项类型</typeparam>
/// <param name="db">数据上下文</param>
/// <param name="data">参数数据</param>
/// <returns>查询结果枚举</returns>
IEnumerable<TReturnType> Run<TPrarmType, TReturnType>(DbContext db,TPrarmType data) where TReturnType : class, new();
/// <summary> /// <summary>
/// 通过数据库上下文执行存储过程,并返回查询结果 /// 通过数据库上下文执行存储过程,并返回查询结果
/// </summary> /// </summary>

View File

@ -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
{
/// <summary>
/// 执行器扩展
/// </summary>
public static class IRunnerExtend
{
/// <summary>
/// 根据模型定义参数执行存储过程进行查询参数类型必须定义ReturnTypeAttribute特性
/// </summary>
/// <typeparam name="TPrarmType">存储过程参数类型</typeparam>
/// <param name="runner">执行器</param>
/// <param name="db">数据上下文</param>
/// <param name="data">存储过程参数</param>
/// <returns>返回类型枚举FalconSPReturnTypeAttribute定义的类型枚举。</returns>
public static IEnumerable<object> Run<TPrarmType>(this IRunner runner,DbContext db,TPrarmType data) {
var dType = typeof(TPrarmType);
var attr = dType.GetCustomAttribute<FalconSPReturnTypeAttribute>();
if(attr != null && attr is FalconSPReturnTypeAttribute pna && pna.ReturnType != null) {
return runner.Run(db,dType,pna.ReturnType,data);
} else {
throw new ReturnTypeException();
}
}
/// <summary>
/// 通过数据库上下文执行存储过程,并返回查询结果
/// </summary>
/// <typeparam name="TPrarmType">参数类型</typeparam>
/// <typeparam name="TReturnType">返回结果项类型</typeparam>
/// <param name="runner">执行器</param>
/// <param name="db">数据上下文</param>
/// <param name="data">参数数据</param>
/// <returns>查询结果枚举</returns>
public static IEnumerable<TReturnType> Run<TPrarmType, TReturnType>(this IRunner runner,DbContext db,TPrarmType data) {
try {
return runner.Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast<TReturnType>();
} catch(InvalidCastException ice) {
throw new ReturnTypeCastException(ice);
} catch(Exception ex) {
throw ex;
}
}
}
}

View File

@ -28,39 +28,6 @@ namespace Falcon.StoredProcedureRunner
return db.Database.ExecuteSqlRaw(str,parms); return db.Database.ExecuteSqlRaw(str,parms);
} }
/// <summary>
/// 根据模型定义参数执行存储过程进行查询参数类型必须定义ReturnTypeAttribute特性
/// </summary>
/// <typeparam name="TPrarmType">存储过程参数类型</typeparam>
/// <param name="db">数据上下文</param>
/// <param name="data">存储过程参数</param>
/// <returns>返回类型枚举FalconSPReturnTypeAttribute定义的类型枚举。</returns>
public IEnumerable<object> Run<TPrarmType>(DbContext db,TPrarmType data) {
var rType = getRequtnType(typeof(TPrarmType));
if(rType == null) {
throw new ReturnTypeException();
}
return Run(db,typeof(TPrarmType),rType,data);
}
/// <summary>
/// 通过数据库上下文执行存储过程,并返回查询结果
/// </summary>
/// <typeparam name="TPrarmType">参数类型</typeparam>
/// <typeparam name="TReturnType">返回结果项类型</typeparam>
/// <param name="db">数据上下文</param>
/// <param name="data">参数数据</param>
/// <returns>查询结果枚举</returns>
public IEnumerable<TReturnType> Run<TPrarmType, TReturnType>(DbContext db,TPrarmType data) where TReturnType : class, new() {
try {
return Run(db,typeof(TPrarmType),typeof(TReturnType),data).Cast<TReturnType>();
} catch(InvalidCastException ice) {
throw new ReturnTypeCastException(ice);
} catch(Exception ex) {
throw ex;
}
}
/// <summary> /// <summary>
/// 通过数据库上下文执行存储过程,并返回查询结果 /// 通过数据库上下文执行存储过程,并返回查询结果
/// </summary> /// </summary>
@ -121,18 +88,6 @@ namespace Falcon.StoredProcedureRunner
return pramType.Name; return pramType.Name;
} }
/// <summary>
/// 获取存储过程名返回值类型
/// </summary>
/// <param name="pramType">返回值类型</param>
private static Type getRequtnType(Type pramType) {
var attr = pramType.GetCustomAttribute<FalconSPReturnTypeAttribute>();
if(attr != null && attr is FalconSPReturnTypeAttribute pna && pna.ReturnType != null) {
return pna.ReturnType;
}
return null;
}
/// <summary> /// <summary>
/// 获取存储过程参数枚举 /// 获取存储过程参数枚举
/// </summary> /// </summary>