From baa1f85b4fa30e42ddb9a422a1cc80713692ed54 Mon Sep 17 00:00:00 2001 From: Atsushi Nakamura Date: Mon, 16 May 2022 13:21:40 +0900 Subject: [PATCH] Modified Dapper's TypeHandler so that it is automatically registered when the Module is initialized. --- README.md | 9 ++++++--- src/UnitGenerator/CodeTemplate.cs | 11 ++++++++++- src/UnitGenerator/CodeTemplate.tt | 8 ++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bad6065..f4cafa0 100644 --- a/README.md +++ b/README.md @@ -417,7 +417,7 @@ public readonly partial struct UserId ### DapperTypeHandler -Implements Dapper's TypeHandler by public accessibility. It is not registered automatically so you need to register manually. +Implements Dapper's TypeHandler by public accessibility. TypeHandler is automatically registered at the time of Module initialization. ```csharp public readonly partial struct UserId @@ -425,8 +425,11 @@ public readonly partial struct UserId public class UserIdTypeHandler : Dapper.SqlMapper.TypeHandler } -// setup handler manually -Dapper.SqlMapper.AddTypeHandler(new UserId.UserIdTypeHandler()); +[ModuleInitializer] +public static void AddTypeHandler() +{ + Dapper.SqlMapper.AddTypeHandler(new A.ATypeHandler()); +} ``` ### EntityFrameworkValueConverter diff --git a/src/UnitGenerator/CodeTemplate.cs b/src/UnitGenerator/CodeTemplate.cs index f63a74e..ec7405f 100644 --- a/src/UnitGenerator/CodeTemplate.cs +++ b/src/UnitGenerator/CodeTemplate.cs @@ -32,6 +32,9 @@ public virtual string TransformText() } if (HasFlag(UnitGenerateOptions.JsonConverter)) { this.Write(" \r\nusing System.Text.Json;\r\nusing System.Text.Json.Serialization;\r\n"); + } + if (HasFlag(UnitGenerateOptions.DapperTypeHandler)) { + this.Write(" \r\nusing System.Runtime.CompilerServices;\r\n"); } this.Write("\r\n"); if (!string.IsNullOrEmpty(Namespace)) { @@ -478,7 +481,13 @@ public virtual string TransformText() this.Write(this.ToStringHelper.ToStringWithCulture(Name)); this.Write(" value)\r\n {\r\n parameter.DbType = System.Data.DbType."); this.Write(this.ToStringHelper.ToStringWithCulture(GetDbType())); - this.Write(";\r\n parameter.Value = value.value;\r\n }\r\n }\r\n\r\n"); + this.Write(";\r\n parameter.Value = value.value;\r\n }\r\n }\r\n\r\n " + + " [ModuleInitializer]\r\n public static void AddTypeHandler()\r\n " + + "{\r\n Dapper.SqlMapper.AddTypeHandler(new "); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write("."); + this.Write(this.ToStringHelper.ToStringWithCulture(Name)); + this.Write("TypeHandler());\r\n }\r\n"); } this.Write("\r\n"); if (HasFlag(UnitGenerateOptions.EntityFrameworkValueConverter)) { diff --git a/src/UnitGenerator/CodeTemplate.tt b/src/UnitGenerator/CodeTemplate.tt index 6199c65..d281dde 100644 --- a/src/UnitGenerator/CodeTemplate.tt +++ b/src/UnitGenerator/CodeTemplate.tt @@ -16,6 +16,9 @@ using MessagePack.Formatters; using System.Text.Json; using System.Text.Json.Serialization; <# } #> +<# if (HasFlag(UnitGenerateOptions.DapperTypeHandler)) { #> +using System.Runtime.CompilerServices; +<# } #> <# if (!string.IsNullOrEmpty(Namespace)) { #> namespace <#= Namespace #> @@ -422,6 +425,11 @@ namespace <#= Namespace #> } } + [ModuleInitializer] + public static void AddTypeHandler() + { + Dapper.SqlMapper.AddTypeHandler(new <#= Name #>.<#= Name #>TypeHandler()); + } <# } #> <# if (HasFlag(UnitGenerateOptions.EntityFrameworkValueConverter)) { #>