title: Dependency Injection using Template 10
body: <p>I am trying to migrate some code from an old Windows 8.1 app that I had developed using Prism/Unity to a new UWP app using Template 10 and Unity. I have seen in the documentation for Template 10 <a href="https://github.com/Windows-XAML/Template10/wiki/Docs-%7C-Bootstrapper#dependency-injection" rel="nofollow">here</a> that you can <code>override</code> the <code>ResolveForPage</code> method.</p>
  
  <p>In my old Windows 8.1 app, there is a <code>Resolve</code> method in Prism that I would <code>override</code> like this:</p>
  
  <pre><code>protected override object Resolve(Type type)
  {
      return Container.Resolve(type);
  }
  </code></pre>
  
  <p>The signature for the Template 10 method is </p>
  
  <pre><code>public override INavigable ResolveForPage(Page page, NavigationService navigationService)
  </code></pre>
  
  <p>so I am not exactly sure how to convert this. I have registered my repository in <code>OnInitializeAsync</code> in my <code>App.xaml.cs</code>, like so:</p>
  
  <pre><code>Container.RegisterType&lt;IPayeesRepository, PayeesRepository&gt;(new ContainerControlledLifetimeManager());
  </code></pre>
  
  <p>Where <code>Container</code> is a <code>UnityContainer</code> instance. My problem is that when I try to inject the dependency on another page, I get a <code>NullReferenceException</code> because <code>_payeesRepository</code> is <code>null</code>. It seems to me like the constructor with the dependency injection is not being called, and if I remove the default constructor then I get an error. Has anyone gotten Unity to work with Template 10 that may have any suggestions what I may be missing?</p>
  
  <p>I also tried using the <code>Dependency</code> attribute like so:</p>
  
  <pre><code>[Dependency]
  private IPayeesRepository _payeesRepository { get; set; }
  </code></pre>
  
  <p>But that doesn't work either. It seems like the <code>IPayeesRepository</code> is just not being instantiated, but I'm not exactly sure. In my Windows 8.1 app, it is never explicitly instantiated, so I have a feeling it has something to do with not overriding the <code>Resolve</code> method.</p>