A practical walkthrough of adding dependency injection to an Avalonia UI desktop application using the Lamar IoC container. Covers scanning and registering ViewModels and controls, creating WindowBase and ViewModelBase classes with property injection via SetterProperty attributes, wiring up the container in the app entry point, and considerations around object lifetimes in desktop apps. Property injection is chosen over constructor injection to preserve Avalonia's designer preview in JetBrains Rider.

4m read timeFrom khalidabuhakmeh.com
Post cover image
Table of contents
Registering Your DependenciesWindowBase and ViewModelBaseMainWindow, MainWindowViewModel, and DependenciesThe EntrypointConclusion