Design and Architecture
Design enforcement
Define what must use what and what must not, generate the CQLinq rules, and follow their status.
Overview
Design enforcement turns architecture decisions into rules the analysis checks every time. You write them as sentences in a GUI (“UI must not use Data”, “Services may only use Core”, “this function must not call that one”), the tool generates the CQLinq rules, puts them in the query group Design Checkers of the project, and the page Design Enforcement Rules shows every rule with its status and violations.
Two pages work together:
- Settings > Design Checkers: where you define layers and rules.
- Design & Architecture > Design Enforcement Rules: where you see the results.
Quick start
- Open Settings > Design Checkers.
- In Layers and matrix press New layer, name it and add what belongs to it with Find in code (for example the namespaces of your UI).
- Create the other layers, then click the matrix cells: ✓ allowed, ✕ forbidden.
- Or in Rules press New rule and complete the sentence.
- Press Save. The queries are written into the group Design Checkers and the project is saved.
- Run the analysis, then open Design Enforcement Rules.
The Design Checkers editor
Three tabs:
| Tab | Purpose |
|---|---|
| Rules | List of rules on the left. On the right a rule is a sentence: Source, must / must not, Target, then name, severity, active, reason and how to fix. The panel at the bottom shows the checks of the rule. |
| Layers and matrix | Define layers (named groups of code) and fill the dependency matrix, which generates rules for you. |
| Generated queries | Read-only preview of the CQLinq of each active rule, as it will be written. A copy button is provided. |
The bottom of the page sets the group name (default Design Checkers) and whether the group is written each time the project is saved. The status line counts rules, layers, errors and warnings.
Layers and patterns
A source or target is either a layer (@UI, chosen with the @ layer button) or a list of patterns separated by ;. A layer is a list of patterns, one per line. A leading ! excludes.
| Pattern | Matches |
|---|---|
ns:App.Ui.* | The namespace of the element (a type is matched through its namespace). * and ? are wildcards. |
type:*Controller | The name of the type. |
full:Foo::Bar | The full name of a type. |
method:Foo::Bar(int) | The full name of a method or function (only in must not use). |
name:strcpy | A function by its simple name, for example a forbidden API. |
!ns:App.Ui.Tests* | Excludes what matches. |
Without a prefix, a pattern is a namespace pattern. Patterns are written as typed: if your code model separates levels with ::, write them that way.
Rule kinds
| Kind | Reads as | Reported |
|---|---|---|
| must not use | A must not use B | Types of A that use a type of B. If B (or A) holds methods or function names: the methods of A that call those functions or use those types. |
| may only use | A may only use B | Types of A that use project code other than themselves and B. Third-party code is ignored by default (option). |
| must use | A must use B | Types of A that use nothing of B. |
| must derive from | Classes of A must derive from B | Classes of A with no base class in B. |
| must not derive from | Classes of A must not derive from B | Classes of A that derive from B. |
| must be named like | Classes of A must be named like R | Classes of A whose name does not match the regular expression R. |
Each rule also has a severity (Low, Medium, High, Critical; it sets the severity and the debt of its violations), a reason (shown in the rule description) and an optional how to fix text.
Layer matrix
Rows are the layer that uses, columns the layer that is used. Click a cell to cycle free → allowed → forbidden.
- A row with at least one allowed cell becomes “A may only use…” with those layers.
- A row with only forbidden cells becomes “A must not use…”.
- Free cells say nothing.
Rules generated from the matrix are not listed in the Rules tab: they follow the matrix. Renaming a layer updates the matrix and the rules that refer to it.
Find in code
The Find in code button next to Source, Target and the layer patterns opens the product’s Code Search. Search by name, then double-click a namespace, type or method (or select it and press Add selected element). The tool asks the code model for its full name and writes it into the box as ns:…, full:… or method:…, then closes the search. A tick box adds * to include nested namespaces. Methods can only be picked when the rule kind is must not use.
The generated queries
Each rule becomes a CQLinq rule with the same layout as the rules shipped with the product, so it shows in the rule lists, the report and Coding Skills like any other rule. For example, UI must not use Data:
// <Name>UI must not use Data</Name>
//<RuleID>DC-001</RuleID>
warnif count > 0
from t in Types where t.ParentNamespace.NameLike(@"^.*\.Ui.*$")
let bad = t.TypesUsed.Where(u => u != t && u.ParentNamespace.NameLike(@"^.*\.Data.*$"))
where bad.Any()
select new { t, Uses = bad, Count = bad.Count(),
Debt = 20.ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Description>
// UI must not use Data.
// Layered design: UI must not depend on these layers.
//</Description>
//<HowToFix>
// Remove the dependency or the call, or go through an abstraction owned by an allowed layer.
//</HowToFix>
Rule names are unique in the project (a suffix is added if needed). A read-only tab shows the exact text, with a bracket and quote check.
Saving and the cdproj
Two things are saved in the project file (.cdproj):
- Your definitions, element
DesignCheckers: layers, rules and matrix cells. They are what you edit. - The generated queries, as the group
Design Checkers,ActiveandShownInReport, in the project’s query groups. They are what the analysis runs.
<DesignCheckers GroupName="Design Checkers" AutoApply="true">
<Layer Name="UI" Color="#4C8BF5"><Pattern>ns:*.Ui*</Pattern></Layer>
<Layer Name="Data" Color="#E8A33D"><Pattern>ns:*.Data*</Pattern></Layer>
<Rule Id="DC-001" Name="UI must not use Data" Kind="MustNotUse" Severity="High" Source="@UI" Target="@Data" />
<Cell From="UI" To="Data" State="Forbidden" />
</DesignCheckers>
The group is rewritten completely on every save: do not edit its queries by hand, edit the rules here. Errors block the save; warnings do not.
Design Enforcement Rules page
Section Design & Architecture, same design as the Hotspots pages.
| Area | Content |
|---|---|
| Tree (left) | Rules grouped as Dependencies and calls, Inheritance, Naming, with violated/total per group. Each rule shows a status icon and its count. |
| Tiles | Rules, Violated, Passing, Violations. A click selects the first matching rule. |
| Result (right) | The sentence, the status and your reason, then the generated CQLinq in the editor and the grid of violating elements. You can edit the query and run it again to explore. |
| Buttons | Edit the rules opens the Design Checkers editor; Open DSM opens the dependency matrix. |
| Status | Meaning |
|---|---|
| ✓ | Respected: no violation. |
| number | Violations. The colour follows the rule severity: red (Critical, High), orange (Medium), yellow (Low). |
| off | The rule is switched off. |
| not applied | Saved in the editor but the group was not written yet: save the rules. |
| not run | The query exists but there is no result yet: run the analysis. |
Results come from the analysis, exactly like other rules: the group is active and shown in the report, so counts and statuses appear in the report and can feed quality gates in the same way.
Checks before saving
- Rule name empty or used twice; source or target empty; unknown layer; invalid regular expression (naming rules).
- A method used in a kind that does not accept methods.
- Contradictions: the same source must use and must not use the same target.
- Warnings: a layer without patterns matches nothing; a source equal to its target; a spec with only exclusions.
Limits
- The queries use members of the code model (
TypesUsed,BaseClasses,MethodsCalled,ParentNamespace,NameLike,FullNameLike). Their availability depends on the language model; check the first generated query by running it. - Rules are checked on the code of the analysed projects; third-party code is ignored where noted.
- Matching is by name patterns: a renamed namespace needs a new pattern (Find in code gives exact full names).
- The editor does not run queries itself; run the analysis to get results.
Questions
A rule shows “not applied”.
Press Save in the editor, and check that Write the group each time the project is saved is on.
Can I add my own CQLinq rule to the group?
It would be removed at the next save. Put your own rules in another group.
Where do I see a violation in the code?
Select the rule on the Design Enforcement Rules page and double-click a row of the grid, like in any query result.
Thanks for your feedback. Tell us more or request a feature.
