|
1 | 1 | # Dataverse Analyzer |
2 | 2 |
|
3 | | -A Roslyn analyzer for .NET Core projects that enforces specific coding standards and best practices. |
| 3 | +A Roslyn analyzer enforcing coding standards for Dataverse/.NET projects. |
4 | 4 |
|
5 | | -## Rule CT0001: Control Flow Braces Rule |
| 5 | +## Rules Overview |
6 | 6 |
|
7 | | -This analyzer enforces that `if` and `else` control flow statements without braces can only contain: |
8 | | -- `return` statements |
9 | | -- `throw` statements |
10 | | -- `continue` statements |
11 | | -- `break` statements |
12 | | -- `yield break` statements |
| 7 | +| ID | Rule | Category | Severity | |
| 8 | +|----|------|----------|----------| |
| 9 | +| CT0001 | Braceless control flow must be return/throw/continue/break/yield break | Style | Error | |
| 10 | +| CT0002 | Enum properties should not be assigned literal values | Usage | Warning | |
| 11 | +| CT0003 | Remove empty parentheses from object initialization | Style | Error | |
| 12 | +| CT0004 | Remove braces from single control flow statements | Style | Error | |
| 13 | +| CT0005 | Constructor has duplicate DI parameter types | Usage | Warning | |
| 14 | +| CT0006 | Plugin class missing XML documentation | Documentation | Warning | |
| 15 | +| CT0007 | Use ContainsAttributes instead of Contains on Entity | Usage | Warning | |
| 16 | +| CT0008 | AddFilteredAttributes not allowed on Create | Usage | Error | |
| 17 | +| CT0009 | PreImage not allowed on Create | Usage | Error | |
| 18 | +| CT0010 | PostImage not allowed on Delete | Usage | Error | |
13 | 19 |
|
14 | | -### Examples |
| 20 | +## Style Rules |
| 21 | + |
| 22 | +### CT0001: Braceless Control Flow |
| 23 | + |
| 24 | +Control flow statements without braces can only contain: `return`, `throw`, `continue`, `break`, or `yield break`. |
15 | 25 |
|
16 | | -✅ **Allowed** (no braces needed): |
17 | 26 | ```csharp |
| 27 | +// Allowed |
18 | 28 | if (condition) |
19 | 29 | return; |
20 | 30 |
|
21 | | -if (error) |
22 | | - throw new Exception(); |
| 31 | +// Not allowed - triggers CT0001 |
| 32 | +if (condition) |
| 33 | + DoSomething(); |
| 34 | +``` |
| 35 | + |
| 36 | +### CT0003: Object Initialization Parentheses |
23 | 37 |
|
24 | | -while (true) |
25 | | - break; |
| 38 | +Remove empty parentheses when using object initializers. |
26 | 39 |
|
27 | | -foreach (var item in items) |
28 | | - continue; |
| 40 | +```csharp |
| 41 | +// Allowed |
| 42 | +var account = new Account { Name = "Test" }; |
29 | 43 |
|
30 | | -if (done) |
31 | | - yield break; |
| 44 | +// Not allowed - triggers CT0003 |
| 45 | +var account = new Account() { Name = "Test" }; |
32 | 46 | ``` |
33 | 47 |
|
34 | | -❌ **Not allowed** (braces required): |
| 48 | +### CT0004: Braced Control Flow |
| 49 | + |
| 50 | +Remove braces when a block contains only a single control flow statement. |
| 51 | + |
35 | 52 | ```csharp |
36 | | -// This will trigger CT0001 |
| 53 | +// Allowed |
37 | 54 | if (condition) |
38 | | - DoSomething(); |
| 55 | + return; |
| 56 | + |
| 57 | +// Not allowed - triggers CT0004 |
| 58 | +if (condition) |
| 59 | +{ |
| 60 | + return; |
| 61 | +} |
39 | 62 | ``` |
40 | 63 |
|
41 | | -## Rule CT0002: Enum Assignment Rule |
| 64 | +## Usage Rules |
42 | 65 |
|
43 | | -This analyzer prevents assigning literal values to enum properties, requiring the use of proper enum values instead. |
| 66 | +### CT0002: Enum Assignment |
44 | 67 |
|
45 | | -### Examples |
| 68 | +Enum properties should use enum values, not literals. |
46 | 69 |
|
47 | | -✅ **Allowed**: |
48 | 70 | ```csharp |
| 71 | +// Allowed |
49 | 72 | AccountCategoryCode = AccountCategoryCode.Standard; |
| 73 | + |
| 74 | +// Not allowed - triggers CT0002 |
| 75 | +AccountCategoryCode = 1; |
50 | 76 | ``` |
51 | 77 |
|
52 | | -❌ **Not allowed**: |
| 78 | +### CT0005: Duplicate DI Parameters |
| 79 | + |
| 80 | +Constructors shouldn't have multiple parameters of the same DI type (Service, Repository, Handler, Provider, Factory, Manager, Client). |
| 81 | + |
53 | 82 | ```csharp |
54 | | -// This will trigger CT0002 |
55 | | -AccountCategoryCode = 1; |
| 83 | +// Not allowed - triggers CT0005 |
| 84 | +public MyClass(IUserService userService, IUserService adminService) { } |
56 | 85 | ``` |
57 | 86 |
|
58 | | -## Rule CT0003: Object Initialization Rule |
| 87 | +### CT0007: Entity Contains |
| 88 | + |
| 89 | +Use type-safe `ContainsAttributes` instead of string-based `Contains` on Entity types. |
59 | 90 |
|
60 | | -This analyzer prevents the use of empty parentheses in object initialization when using object initializers. |
| 91 | +```csharp |
| 92 | +// Allowed |
| 93 | +if (account.ContainsAttributes(x => x.Name)) |
| 94 | + |
| 95 | +// Not allowed - triggers CT0007 |
| 96 | +if (account.Contains("name")) |
| 97 | +``` |
61 | 98 |
|
62 | | -### Examples |
| 99 | +### CT0008-CT0010: Plugin Step Configuration |
| 100 | + |
| 101 | +These rules prevent invalid plugin step configurations in Dataverse: |
| 102 | + |
| 103 | +| Rule | Restriction | Reason | |
| 104 | +|------|-------------|--------| |
| 105 | +| CT0008 | No `AddFilteredAttributes` on Create | Filtered attributes have no effect on Create | |
| 106 | +| CT0009 | No PreImage on Create | No previous record state exists | |
| 107 | +| CT0010 | No PostImage on Delete | No record state exists after deletion | |
63 | 108 |
|
64 | | -✅ **Allowed**: |
65 | 109 | ```csharp |
66 | | -var account = new Account |
67 | | -{ |
68 | | - Name = "MoneyMan", |
69 | | -}; |
| 110 | +// Not allowed - triggers CT0008 |
| 111 | +RegisterPluginStep<Account>(EventOperation.Create) |
| 112 | + .AddFilteredAttributes(a => a.Name); |
70 | 113 |
|
71 | | -var account = new Account(accountId) |
72 | | -{ |
73 | | - Name = "MoneyMan", |
74 | | -}; |
| 114 | +// Not allowed - triggers CT0009 |
| 115 | +RegisterPluginStep<Account>(EventOperation.Create) |
| 116 | + .AddImage(ImageType.PreImage); |
| 117 | + |
| 118 | +// Not allowed - triggers CT0010 |
| 119 | +RegisterPluginStep<Account>(EventOperation.Delete) |
| 120 | + .AddImage(ImageType.PostImage); |
75 | 121 | ``` |
76 | 122 |
|
77 | | -❌ **Not allowed**: |
| 123 | +## Documentation Rules |
| 124 | + |
| 125 | +### CT0006: Plugin Documentation |
| 126 | + |
| 127 | +Classes implementing `IPlugin` must have XML documentation. |
| 128 | + |
78 | 129 | ```csharp |
79 | | -// This will trigger CT0003 |
80 | | -var account = new Account() |
81 | | -{ |
82 | | - Name = "MoneyMan", |
83 | | -}; |
| 130 | +// Allowed |
| 131 | +/// <summary>Updates account name on create.</summary> |
| 132 | +public class UpdateAccountPlugin : IPlugin { } |
| 133 | + |
| 134 | +// Not allowed - triggers CT0006 |
| 135 | +public class UpdateAccountPlugin : IPlugin { } |
84 | 136 | ``` |
85 | 137 |
|
86 | 138 | ## Usage |
87 | 139 |
|
88 | | -The analyzer is designed to be consumed as a project reference or NuGet package in other .NET projects. When integrated, it will automatically analyze your code and report violations of the rules. |
89 | | - |
90 | 140 | ### Building |
91 | 141 |
|
92 | 142 | ```bash |
93 | 143 | dotnet build src\DataverseAnalyzer\DataverseAnalyzer.csproj --configuration Release |
94 | 144 | ``` |
95 | 145 |
|
96 | | -The built analyzer DLL will be available in `src\DataverseAnalyzer\bin\Release\netstandard2.0\DataverseAnalyzer.dll`. |
| 146 | +### Integration |
97 | 147 |
|
98 | | -## Integration |
99 | | - |
100 | | -To use this analyzer in your projects, reference the built DLL as an analyzer: |
| 148 | +Reference the analyzer in your project: |
101 | 149 |
|
102 | 150 | ```xml |
103 | 151 | <ItemGroup> |
104 | 152 | <Analyzer Include="path\to\DataverseAnalyzer.dll" /> |
105 | 153 | </ItemGroup> |
106 | 154 | ``` |
107 | | - |
108 | | -The analyzer includes an automatic code fix provider that can add braces when violations are detected. |
|
0 commit comments