4

I'm new to WP 7. For purposes of dependency injection, I want to adhere to practices acquired doing WinForms apps. I therefore want to build my app graph at the composition root. What part of a regular WP 7 app source code can be viewed as the composition root?

Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
  • 2
    @Claus - DI doesn't require reflection. Funq, for example, uses registered factory delegates to get the job done with little to no performance penalty – Richard Szalay Sep 05 '11 at 18:53

1 Answers1

4

The pattern I prefer, as recommended by the Patterns & Practices team, is:

  • Add a ViewModelLocator class to your resources App.xaml with an x:Key="ViewModelLocator", and add to that class a property for each ViewModel type and lazy-instantiates the VM using the container.
  • Configure your dependencies in the application constructor (App() in App.xaml.cs) and provide the container instance to the ViewModelLocator (via Application.Resources["ViewModelLocator"])
  • Bind the ViewModel to each page by assigning DataContext="{Binding ViewModelPropertyName, Source={StaticResource ViewModelLocator}}"

See the WP7 Guide for a working implementation.

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237