docs(rules): add WPF accessibility and theming, trim WPF rules
A new WPF topic rule covers accessibility (AutomationProperties names and help text, keyboard access, visible focus, high contrast through SystemColors, automation peers for custom controls), DPI scaling in device-independent units with tests across scale factors and monitors, and the Fluent theme through ThemeMode on .NET 9 and later. The existing WPF rules lose about a quarter of their size by merging one-line generic bullets that the C# and shared rules already cover, while keeping the WPF-specific pitfalls. Duplicate async void and binding error guidance is merged, client secret handling defers to the client app security rule, the architecture rule asks to check .NET Framework versus .NET before using newer APIs, and the CommunityToolkit note states that partial properties require C# 14. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b6c33780c3
commit
df09ddeaa9
8 files changed
+69
-185
No files matched your search
@@ -1,77 +1,30 @@
|
||||
# WPF Architecture, Dependency Injection, Testing, and Performance
|
||||
|
||||
Apply these rules when structuring a WPF application, wiring dependency injection, testing view models, or optimizing performance. Load the C# rules for language-level guidance.
|
||||
Apply these rules when structuring a WPF application, wiring dependency injection, testing view models, or optimizing performance. The C# rules and the shared refactoring rule cover general structure, testing, and code quality; this file adds WPF specifics.
|
||||
|
||||
## Architecture
|
||||
|
||||
* Use the C# version the project targets (see the C# coding-style rule; .NET Framework 4.x WPF apps default to C# 7.3). Prefer MVVM for non-trivial views and keep code-behind limited to view-specific behavior.
|
||||
* Keep UI, application, and domain logic separate. Avoid business logic in views and direct service access from controls.
|
||||
* Keep dependencies explicit and testable; prefer composition over inheritance and avoid unnecessary abstractions or framework wrappers.
|
||||
* Avoid direct database access from views or ViewModels.
|
||||
* Avoid direct HTTP access from controls.
|
||||
* Keep external integrations behind focused services.
|
||||
* Avoid circular dependencies.
|
||||
* Keep feature boundaries clear.
|
||||
* Do not create shared utility classes for unrelated functionality.
|
||||
* Keep shared code genuinely reusable.
|
||||
* Minimize global state.
|
||||
* Avoid static service access.
|
||||
* Check whether the app targets .NET Framework or .NET (8, 9, 10, or 11) before using newer APIs, and do not migrate between them as a side effect of another task. Use the C# version the project targets (.NET Framework 4.x defaults to C# 7.3).
|
||||
* Prefer MVVM for non-trivial views and keep code-behind limited to view-specific behavior.
|
||||
* Keep database, HTTP, and other external access behind focused services; never call them from views, controls, or directly from view models.
|
||||
* Avoid static service access and global state.
|
||||
|
||||
## Dependency Injection
|
||||
|
||||
Follow the C# architecture rule for general dependency injection, plus:
|
||||
|
||||
* Register views and view models as transient unless they hold intentionally shared state; register shared services as singletons only when they are thread-safe and stateless or own deliberately shared state.
|
||||
* Do not resolve services from `App.Current` or a static container inside view models; inject them.
|
||||
* Create EF Core contexts per operation through `IDbContextFactory<T>` (see the C# architecture rule).
|
||||
|
||||
## Testing
|
||||
|
||||
* Keep ViewModels testable without a UI thread where practical.
|
||||
* Test behavior rather than implementation details.
|
||||
* Test domain and application logic independently from WPF.
|
||||
* Avoid brittle tests coupled to XAML structure.
|
||||
* Keep UI automation tests focused on critical flows.
|
||||
* Mock external boundaries rather than internal details.
|
||||
* Keep tests deterministic.
|
||||
* Avoid real network, database or filesystem dependencies in unit tests unless explicitly intended.
|
||||
|
||||
## Code Quality
|
||||
|
||||
Follow the C# coding-style and testing rules, plus:
|
||||
|
||||
* Treat binding errors as defects: check the debugger output or XAML binding failures diagnostics for failed bindings after UI changes.
|
||||
* Keep view models testable without a UI thread, and test domain and application logic independently from WPF.
|
||||
* Avoid tests coupled to XAML structure, and keep UI automation tests to critical flows.
|
||||
|
||||
## Performance
|
||||
|
||||
* Keep the UI thread responsive.
|
||||
* Minimize visual tree depth.
|
||||
* Keep virtualization enabled (see the virtualization section of the collections rule).
|
||||
* Avoid unnecessary bindings.
|
||||
* Avoid unnecessary converters.
|
||||
* Avoid unnecessary resource lookups.
|
||||
* Avoid unnecessary property change notifications.
|
||||
* Avoid frequent full collection refreshes.
|
||||
* Avoid loading large datasets eagerly.
|
||||
* Load images as the images section of the resources rule defines, and freeze `Freezable` objects where practical.
|
||||
* Reuse immutable resources where appropriate.
|
||||
* Avoid unnecessary animations.
|
||||
* Avoid high-frequency timers on the UI thread.
|
||||
* Measure startup time, UI responsiveness, memory usage and rendering performance before micro-optimizing.
|
||||
Measure startup, UI responsiveness, memory, and rendering before optimizing, and work in this order: UI architecture, UI thread blocking, data loading and I/O, virtualization, visual tree complexity, binding and notification frequency, rendering and images, memory retention, allocations, then CPU micro-optimizations.
|
||||
|
||||
## Performance Priority
|
||||
|
||||
Prioritize WPF performance work in this order:
|
||||
|
||||
* UI architecture
|
||||
* UI thread blocking
|
||||
* Data loading and I/O
|
||||
* Virtualization
|
||||
* Visual tree complexity
|
||||
* Binding and notification frequency
|
||||
* Rendering and images
|
||||
* Memory retention and leaks
|
||||
* Allocations
|
||||
* CPU micro-optimizations
|
||||
|
||||
Never optimize based purely on assumptions.
|
||||
* Keep the UI thread responsive and virtualization enabled (see the collections rule).
|
||||
* Keep visual trees shallow, and avoid unnecessary bindings, converters, resource lookups, property notifications, and full collection refreshes.
|
||||
* Load large datasets incrementally, load images as the resources rule defines, and freeze `Freezable` objects where practical.
|
||||
* Avoid unnecessary animations and high-frequency timers on the UI thread.
|
||||
Reference in new issue
Block a user