-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement model * merge main * support [column] attribute * fix indents * fix indent x2 * rollback * fix `GetLocation(attributeName)` logic * Update DAP043.md * Update DAP042.md * Update DAP043.md * Update Inspection.cs * add attr usage --------- Co-authored-by: Marc Gravell <marc.gravell@gmail.com>
- Loading branch information
1 parent
643477a
commit 636fd63
Showing
17 changed files
with
512 additions
and
1,027 deletions.
There are no files selected for viewing
This file contains 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,41 @@ | ||
# DAP042 | ||
|
||
Usage of [`[Column]`](https://learn.microsoft.com/dotnet/api/system.componentmodel.dataannotations.schema.columnattribute) | ||
attribute and `[DbValue]` attributes are conflicting - Dapper will choose either one or another's name override. | ||
|
||
This conflict can lead to unexpected behavior, so we recommend to use only one of them. | ||
|
||
Bad: | ||
|
||
``` csharp | ||
class MyType | ||
{ | ||
[DbValue(Name = "MyOtherOtherName")] | ||
[UseColumnAttribute] | ||
[Column("MyOtherName")] | ||
public int MyThisName { get; set; } | ||
} | ||
``` | ||
|
||
Good: | ||
|
||
``` csharp | ||
class MyType | ||
{ | ||
[DbValue(Name = "MyOtherOtherName")] | ||
// without [Column] and [UseColumnAttribute] | ||
public int MyThisName { get; set; } | ||
} | ||
``` | ||
|
||
Another good: | ||
|
||
``` csharp | ||
class MyType | ||
{ | ||
// without [DbValue] | ||
[UseColumnAttribute] | ||
[Column("MyOtherName")] | ||
public int MyThisName { get; set; } | ||
} | ||
``` |
This file contains 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,28 @@ | ||
# DAP043 | ||
|
||
If you are looking at `DAP043`, then most probably you wanted to use [`[Column]`](https://learn.microsoft.com/dotnet/api/system.componentmodel.dataannotations.schema.columnattribute) | ||
attribute to override the name of the type member. | ||
|
||
However, due to [historical reasons](https://stackoverflow.com/a/77073456) you need to add a "marker attribute" `[UseColumnAttribute]` | ||
to explicitly let Dapper know you want to override the type member's name. | ||
|
||
Bad: | ||
|
||
``` csharp | ||
class MyType | ||
{ | ||
[Column("MyOtherName")] | ||
public int MyThisName { get; set; } | ||
} | ||
``` | ||
|
||
Good: | ||
|
||
``` csharp | ||
class MyType | ||
{ | ||
[UseColumnAttribute] | ||
[Column("MyOtherName")] | ||
public int MyThisName { get; set; } | ||
} | ||
``` |
This file contains 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 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 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 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 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,16 @@ | ||
using System; | ||
|
||
namespace Dapper | ||
{ | ||
/// <summary> | ||
/// Specifies whether to use [System.ComponentModel.DataAnnotations.Schema.ColumnAttribute] for additional behavioral configuration | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] | ||
public class UseColumnAttributeAttribute : Attribute | ||
{ | ||
/// <param name="enable">enable usage of [Column] attribute</param> | ||
public UseColumnAttributeAttribute(bool enable = true) | ||
{ | ||
} | ||
} | ||
} |
This file contains 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
41 changes: 41 additions & 0 deletions
41
test/Dapper.AOT.Test/Interceptors/ColumnAttribute.input.cs
This file contains 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,41 @@ | ||
using Dapper; | ||
using System.Data.Common; | ||
|
||
// needed for [Column] attribute | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
[module: DapperAot] | ||
|
||
public static class Foo | ||
{ | ||
static void SomeCode(DbConnection connection, string bar, bool isBuffered) | ||
{ | ||
_ = connection.Query<MyType>("def"); | ||
} | ||
|
||
public class MyType | ||
{ | ||
// default | ||
public int A { get; set; } | ||
|
||
// dbvalue | ||
[DbValue(Name = "DbValue_B")] | ||
public int B { get; set; } | ||
|
||
// standard enabled | ||
[UseColumnAttribute] | ||
[Column("StandardColumn_C")] | ||
public int C { get; set; } | ||
|
||
// explicitly enabled | ||
[UseColumnAttribute] | ||
[Column("ExplicitColumn_D")] | ||
public int D { get; set; } | ||
|
||
// dbValue & Column enabled | ||
[UseColumnAttribute] | ||
[DbValue(Name = "DbValue_E")] | ||
[Column("Column_E")] | ||
public int E { get; set; } | ||
} | ||
} |
Oops, something went wrong.
Out of curiosity, what was the rationale of having a local function vs have the code in the ValidateMember code? Readability?