From d83467bdde54653b7c1c1ec323f68f4c18e79e02 Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Thu, 18 Apr 2019 20:58:10 +0800 Subject: [PATCH] add support DyRepository.UseTransactionAttribute --- .../EmitRepositoryBuilder.cs | 1 + .../Reflection/TypeConstants/DataType.cs | 1 - .../Reflection/TypeConstants/NullableType.cs | 16 ++++++++++++++++ .../TypeConstants/RequestContextType.cs | 2 +- src/SmartSql/RequestContext.cs | 5 ----- 5 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 src/SmartSql/Reflection/TypeConstants/NullableType.cs diff --git a/src/SmartSql.DyRepository/EmitRepositoryBuilder.cs b/src/SmartSql.DyRepository/EmitRepositoryBuilder.cs index 0e2f088f..130e417a 100644 --- a/src/SmartSql.DyRepository/EmitRepositoryBuilder.cs +++ b/src/SmartSql.DyRepository/EmitRepositoryBuilder.cs @@ -432,6 +432,7 @@ private static void EmitSetTransaction(ILGenerator ilGen, MethodInfo methodInfo) { ilGen.LoadLocalVar(0); ilGen.LoadInt32(useTransactionAttribute.Level.GetHashCode()); + ilGen.New(NullableType.Ctor); ilGen.Callvirt(RequestContextType.Method.SetTransaction); } } diff --git a/src/SmartSql/Reflection/TypeConstants/DataType.cs b/src/SmartSql/Reflection/TypeConstants/DataType.cs index 7b3f7a2a..9b3a1696 100644 --- a/src/SmartSql/Reflection/TypeConstants/DataType.cs +++ b/src/SmartSql/Reflection/TypeConstants/DataType.cs @@ -2,7 +2,6 @@ using System; using System.Collections; using System.Data; -using System.Reflection; namespace SmartSql.Reflection.TypeConstants { diff --git a/src/SmartSql/Reflection/TypeConstants/NullableType.cs b/src/SmartSql/Reflection/TypeConstants/NullableType.cs new file mode 100644 index 00000000..318967e9 --- /dev/null +++ b/src/SmartSql/Reflection/TypeConstants/NullableType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; + +namespace SmartSql.Reflection.TypeConstants +{ + public static class NullableType where TTarget : struct + { + public static Type TargetType = typeof(TTarget); + public static readonly Type Type = typeof(TTarget?); + + public static readonly ConstructorInfo Ctor = Type.GetConstructor(new Type[] { TargetType }); + } +} diff --git a/src/SmartSql/Reflection/TypeConstants/RequestContextType.cs b/src/SmartSql/Reflection/TypeConstants/RequestContextType.cs index 672db7c1..74a8ed56 100644 --- a/src/SmartSql/Reflection/TypeConstants/RequestContextType.cs +++ b/src/SmartSql/Reflection/TypeConstants/RequestContextType.cs @@ -25,7 +25,7 @@ public static class Method { public static readonly MethodInfo SetDataSourceChoice = AbstractType.GetMethod("set_DataSourceChoice"); public static readonly MethodInfo SetCommandType = AbstractType.GetMethod("set_CommandType"); - public static readonly MethodInfo SetTransaction = AbstractType.GetMethod("SetTransaction"); + public static readonly MethodInfo SetTransaction = AbstractType.GetMethod("set_Transaction"); public static readonly MethodInfo SetScope = AbstractType.GetMethod("set_Scope"); public static readonly MethodInfo SetSqlId = AbstractType.GetMethod("set_SqlId"); public static readonly MethodInfo SetRequest = AbstractType.GetMethod("SetRequest"); diff --git a/src/SmartSql/RequestContext.cs b/src/SmartSql/RequestContext.cs index 22446e96..40f56134 100644 --- a/src/SmartSql/RequestContext.cs +++ b/src/SmartSql/RequestContext.cs @@ -54,11 +54,6 @@ public ResultMap GetCurrentResultMap() MultipleResultMap.Results[ExecutionContext.DataReaderWrapper.ResultIndex]?.Map : ResultMap; } - public void SetTransaction(IsolationLevel isolationLevel) - { - Transaction = isolationLevel; - } - public abstract void SetupParameters(); public abstract void SetRequest(object requestObj); }