# WPF Architecture, Dependency Injection, Testing, and Performance 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 * 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 * 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 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 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. * 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.