Skip to content

Commit

Permalink
Merge pull request #10 from nuitsjp/support-for-automatic-registreati…
Browse files Browse the repository at this point in the history
…on-of-typehandler

Support for automatic registration of TypeHandler
  • Loading branch information
neuecc committed May 16, 2022
2 parents 4c02629 + baa1f85 commit b639067
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,16 +417,19 @@ 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
{
public class UserIdTypeHandler : Dapper.SqlMapper.TypeHandler<UserId>
}

// setup handler manually
Dapper.SqlMapper.AddTypeHandler(new UserId.UserIdTypeHandler());
[ModuleInitializer]
public static void AddTypeHandler()
{
Dapper.SqlMapper.AddTypeHandler(new A.ATypeHandler());
}
```

### EntityFrameworkValueConverter
Expand Down
11 changes: 10 additions & 1 deletion src/UnitGenerator/CodeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down
8 changes: 8 additions & 0 deletions src/UnitGenerator/CodeTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -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 #>
Expand Down Expand Up @@ -422,6 +425,11 @@ namespace <#= Namespace #>
}
}

[ModuleInitializer]
public static void AddTypeHandler()
{
Dapper.SqlMapper.AddTypeHandler(new <#= Name #>.<#= Name #>TypeHandler());
}
<# } #>

<# if (HasFlag(UnitGenerateOptions.EntityFrameworkValueConverter)) { #>
Expand Down

0 comments on commit b639067

Please sign in to comment.