# 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. ## 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. ## 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` (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. ## 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. ## 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.