forked from DapperLib/DapperAOT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: trivial parameterization anti-patterns analysis support (Dapper…
…Lib#61) * analyze interpolated strings * add test for local variable usage * supported local var declaration * support concat strings * support string.Format in direct usage * explain special case * add docs + interpolated raw string literal syntax test * refactor to not use specific language in `Inspection` * support VB
- Loading branch information
1 parent
8a71154
commit 207f229
Showing
15 changed files
with
426 additions
and
6 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,28 @@ | ||
# DAP241 | ||
|
||
Dapper allows writing [parameterized queries](https://github.com/DapperLib/Dapper/blob/main/Readme.md#parameterized-queries), | ||
but developer should **never interpolate** values into the query string (`sql` parameter for any Dapper-method). | ||
|
||
Bad: | ||
|
||
``` csharp | ||
var id = 42; | ||
conn.Execute($"select Id from Customers where Id = {id}"); | ||
``` | ||
|
||
Instead the intended way is to pass the anynomous object, | ||
members of which will be mapped into the query parameter using the same name. | ||
|
||
In example for the object `new { A = 42, B = "hello world" }`: | ||
- member `A` will be mapped into the parameter `@A` | ||
- member `B` will be mapped into the parameter `@B` | ||
|
||
Good: | ||
|
||
``` csharp | ||
var id = 42; | ||
conn.Execute( | ||
$"select Id from Customers where Id = @queryId", | ||
new { queryId = id } | ||
); | ||
``` |
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 @@ | ||
# DAP242 | ||
|
||
Dapper allows writing [parameterized queries](https://github.com/DapperLib/Dapper/blob/main/Readme.md#parameterized-queries), | ||
but developer should **never concatenate** values into the query string (`sql` parameter for any Dapper-method). | ||
|
||
Bad: | ||
|
||
``` csharp | ||
var id = 42; | ||
conn.Execute("select Id from Customers where Id = " + id); | ||
``` | ||
|
||
Instead the intended way is to pass the anynomous object, | ||
members of which will be mapped into the query parameter using the same name. | ||
|
||
In example for the object `new { A = 42, B = "hello world" }`: | ||
- member `A` will be mapped into the parameter `@A` | ||
- member `B` will be mapped into the parameter `@B` | ||
|
||
Good: | ||
|
||
``` csharp | ||
var id = 42; | ||
conn.Execute( | ||
$"select Id from Customers where Id = @queryId", | ||
new { queryId = id } | ||
); | ||
``` |
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
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
18 changes: 18 additions & 0 deletions
18
src/Dapper.AOT.Analyzers/Internal/Roslyn/StringSyntaxKind.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,18 @@ | ||
namespace Dapper.Internal.Roslyn | ||
{ | ||
/// <summary> | ||
/// Represents a syntaxKind for the string usage | ||
/// </summary> | ||
internal enum StringSyntaxKind | ||
{ | ||
NotRecognized, | ||
|
||
ConcatenatedString, | ||
InterpolatedString, | ||
|
||
/// <summary> | ||
/// Represents a syntax for `string.Format()` API | ||
/// </summary> | ||
FormatString | ||
} | ||
} |
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
Oops, something went wrong.