-
-
Notifications
You must be signed in to change notification settings - Fork 32
feat: TypeHandler support #162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
7amou3
wants to merge
1
commit into
DapperLib:main
Choose a base branch
from
7amou3:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # DAP050 | ||
|
|
||
| Duplicate classes have been registered as type handlers for the same type, | ||
| meaning it's not possible to determine which to use when handling the type. | ||
| Note type handlers can be registered at the assembly and module level, so | ||
| ensure the type used for the `TValue` parameter in the attribute is only | ||
| specified once. | ||
|
|
||
| Bad: | ||
|
|
||
| ``` c# | ||
| [module: TypeHandler<MyClass, MyHandler1>] | ||
| [module: TypeHandler<MyClass, MyHandler2>] | ||
| ``` | ||
|
|
||
| Good: | ||
|
|
||
| ``` c# | ||
| [module: TypeHandler<MyClass, MyHandler>] | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # DAP051 | ||
|
|
||
| TypeHandler attribute points to a type handler class (TTypeHandler in TypeHandler<TValue, TTypeHandler>) that is not a valid named type, | ||
| or cannot be constructed as required by Dapper AOT. | ||
| The type handler must be a concrete (non-abstract) class and must have a public parameterless constructor. | ||
|
|
||
| Bad: | ||
|
|
||
| ``` c# | ||
| [module: TypeHandler<MyClass, IMyHandler>] | ||
| [module: TypeHandler<MyClass, MyAbstractHandler>] | ||
| ``` | ||
|
|
||
| Good: | ||
|
|
||
| ``` c# | ||
| [module: TypeHandler<MyClass, MyHandler>] | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Type Handlers | ||
|
|
||
| Dapper AOT provides a mechanism to customize how specific .NET types are mapped to and from database values. | ||
| This is achieved through Type Handlers, which allow you to define custom serialization for parameters sent | ||
| to the database and custom deserialization for values read from query results. | ||
| This is similar to SqlMapper.TypeHandler in vanilla Dapper, but with a specific AOT-compatible registration process. | ||
|
|
||
| To register your custom type handler, you use either an assembly or module level attribute. | ||
| Both have the same effect. | ||
| This attribute tells Dapper AOT which .NET type (TValue) your handler processes | ||
| and which class (TTypeHandler) is responsible for that processing. | ||
|
|
||
| ``` csharp | ||
| // Example using a module-level attribute | ||
| using Dapper; | ||
|
|
||
| [module: TypeHandler<YourDotNetType, YourCustomTypeHandler>] | ||
|
|
||
| // Example using an assembly-level attribute (often in AssemblyInfo.cs or a shared file) | ||
| // [assembly: TypeHandler<YourDotNetType, YourCustomTypeHandler>] | ||
| ``` | ||
|
|
||
| Your custom type handler class must: | ||
| * Inherit from Dapper.TypeHandler<TValue>, where TValue is the .NET type it handles. | ||
| * Have a public parameterless constructor (new()). | ||
| * Implement (override) the necessary virtual methods for your specific conversion needs. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 180 additions & 48 deletions
228
src/Dapper.AOT.Analyzers/CodeAnalysis/DapperInterceptorGenerator.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| using Dapper; | ||
| using System.Data; | ||
| using System.Data.Common; | ||
|
|
||
| [module: DapperAot] | ||
| [module: TypeHandler<CustomClass, CustomClassTypeHandler>] | ||
|
|
||
| public class CustomClassTypeHandler : TypeHandler<CustomClass> | ||
| { | ||
| } | ||
|
|
||
| public class CustomClass | ||
| { | ||
| } | ||
|
|
||
| public static class Foo | ||
| { | ||
| static void SomeCode(DbConnection connection, string bar, bool isBuffered) | ||
| { | ||
| _ = connection.Query<MyType>("def"); | ||
| _ = connection.Query<int>("def", new { Param = new CustomClass() }); | ||
| _ = connection.Query<int>("@OutputValue = def", new CommandParameters()); | ||
| } | ||
|
|
||
| public class CommandParameters | ||
| { | ||
| [DbValue(Direction = ParameterDirection.Output)] | ||
| public CustomClass OutputValue { get; set; } | ||
| } | ||
|
|
||
| public class MyType | ||
| { | ||
| public CustomClass C { get; set; } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I have a query that required certain set of typeHandlers,
And other do not,
Then how to restore them, and then add if and when needed ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello, this PR does not currently allow for switching or disabling type handlers on the fly.
This is definitely a feature that could be added, though.