title: IoC Registration differences between Unity and Simple Injector
body: <p>I have one project functioning perfectly using Unity. I try switching to use Simple Injector instead and now NO changes ever get saved in my database. I believe it has to do with the lifetime of the registered components. Here is the Unity container registration:</p>
  
  <pre><code>private IUnityContainer GetUnityContainer()
  {
      IUnityContainer container = new UnityContainer()
          .RegisterType&lt;IDatabaseFactory, DatabaseFactory&gt;(
              new HttpContextLifetimeManager&lt;IDatabaseFactory&gt;())
      .RegisterType&lt;IUnitOfWork, UnitOfWork&gt;(
          new HttpContextLifetimeManager&lt;IUnitOfWork&gt;())
      .RegisterType&lt;ICategoryRepository, CategoryRepository&gt;(
          new HttpContextLifetimeManager&lt;ICategoryRepository&gt;())
      .RegisterType&lt;ICategoryService, CategoryService&gt;(
          new HttpContextLifetimeManager&lt;ICategoryService&gt;());
  
      return container;         
  }
  </code></pre>
  
  <p>And here is the new Simple Injector registration.</p>
  
  <pre><code>container.Register&lt;IDatabaseFactory, DatabaseFactory&gt;();
  container.Register&lt;IUnitOfWork, UnitOfWork&gt;();
  container.Register&lt;ICategoryRepository, CategoryRepository&gt;();
  container.Register&lt;ICategoryService, CategoryService&gt;();
  </code></pre>
  
  <p>I'm not sure how the <code>HttpContextLifetimeManager</code> comes into play with Simple Injector. MVC is the client for the unity example, but I'm changing to a WPF project and Simple Injector. Any suggestions are much appreciated. Thanks.</p>
  
  <hr>
  
  <p>@Steven. Thanks for your comment. I just discovered that since my RepositoryBase and my UnitOfWork inject an IDatabaseFactory in their constructors that I needed to use <code>container.RegisterSingle&lt;IDatabaseFactory, DatabaseFactory&gt;()</code>. This resolved one issue. I still have a problem with lifetime though. Since my consuming app is WPF, how will the RegisterPerWebRequest work? </p>
  
  <p>My project has a DataLayer >> BusinessLayer >> WcfService >> WPF Front end. Simple Injector is set on the WcfService project and the business layer has Boostrapper to register items there. As of now, my WPF client will GetAllCountries() and display in a grid. If I change the name of one and try to update, I get the "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." error. I've done some debugging and find that after the GetCountries service call in the WPF client, when I go back to try to update, I see ALL of the countries are attached to the context via dbContext.ChangeTracker.Entries(). At this point I should have NO entities being tracked as my context should have been disposed after the first unit of work. </p>
  
  <p>In an MVC app the RegisterPerWebRequest fixes that, but what is the equivalent for WPF? I'm going to install the extension now and try it anyway but I have a feeling it isn't the solution I'm looking for.. or is it? Thanks again for the help.</p>
  
  <hr>
  
  <p>OK. I did a bit more digging and found a solution that works. I'm just not sure if it's the correct one. Anyway, now in my BLL where there is a bootstrapper to register things, I can register like this:</p>
  
  <pre><code>container.RegisterPerWcfOperation&lt;IDatabaseFactory, DatabaseFactory&gt;();
  container.RegisterPerWcfOperation&lt;IUnitOfWork, UnitOfWork&gt;();
  container.RegisterPerWcfOperation&lt;ICountryRepository, CountryRepository&gt;();
  </code></pre>
  
  <p>That gives me what I was looking for. Only a single instance of DatabaseFactory is ever created and thus my repository and unit of work share it like they should. Also, after GetCountries() on the client, when I do my second call to the service to perform and update, I check the dbContext.ChangeTracker.Entries() and see that there are NO entities being tracked, which is correct. I can now attach, set to modify, and call SaveChanges without getting the duplicate key error. Does this seem ok? Thanks.</p>