title: Am I using correct lifetime managers for dependency injection?
body: <p>I have an asp.net web api application that uses Unity dependency injection libraries from MS Unity.AspNet.WebApi and Unity nuget packages. Also, the application uses Entity Framework version 6 database context for ORM and a generic repository. </p>
  
  <p>Custom service types are consumed by Api controllers. Custom Service classes consumes EF database contexts and the generic repository.</p>
  
  <p>My question is: Are HierarchicalLifetimeManager  and ContainerControlledLifetimeManager correct lifetime managers for my web api application?</p>
  
  <p>Codes in the UnityConfig class of my application: </p>
  
  <pre><code>    using System;
      using System.Configuration;
      using System.Data.Entity;
      using Microsoft.Practices.Unity;
      using Microsoft.Practices.Unity.Configuration;
      using App.Api.Models;
      using App.Dal;
  
      public class UnityConfig
          {
              #region Unity Container
              private static Lazy&lt;IUnityContainer&gt; container = new Lazy&lt;IUnityContainer&gt;(() =&gt;
              {
                  var container = new UnityContainer();
                  RegisterTypes(container);
                  return container;
              });
  
              /// &lt;summary&gt;
              /// Gets the configured Unity container.
              /// &lt;/summary&gt;
              public static IUnityContainer GetConfiguredContainer()
              {
                  return container.Value;
              }
              #endregion
  
              /// &lt;summary&gt;Registers the type mappings with the Unity container.&lt;/summary&gt;
              /// &lt;param name="container"&gt;The unity container to configure.&lt;/param&gt;
              /// &lt;remarks&gt;There is no need to register concrete types such as controllers or API controllers (unless you want to 
              /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.&lt;/remarks&gt;
              public static void RegisterTypes(IUnityContainer container)
              {
  
                  var connectionStringEntityFramework= ConfigurationManager.ConnectionStrings["AppEntities"].ToString();
  
  
              // Entity Framework database context and generic repository
  // HierarchicalLifetimeManager  is used:
  
                  container.RegisterType&lt;DbContext, FirstAngularJsEntities&gt;(new HierarchicalLifetimeManager(), new InjectionConstructor(connectionStringFirstAngularJsEntities));
                  container.RegisterType&lt;IRepository, GenRepository&gt;(new HierarchicalLifetimeManager(), new InjectionConstructor(typeof(DbContext)));
  
  
                  // services
  // ContainerControlledLifetimeManager is used:
  
                  container.RegisterType&lt;IContactService, ContactService&gt;(new ContainerControlledLifetimeManager());
                  container.RegisterType&lt;IProductService, ProductService&gt;(new ContainerControlledLifetimeManager());
  
              }
  </code></pre>
  
  <p>A sample api controller has custom service injected in its constructor:</p>
  
  <pre><code>public class ContactApiController : ApiController
      {
          private readonly IContactService _contactService;
  
          public ContactApiController(IContactService contactService)
          {
              _contactService = contactService;
          }
  
  ...
  }
  </code></pre>
  
  <p>A sample custom service has EF DbContext and repository injected in its constructor:</p>
  
  <pre><code>public class ContactService : IContactService
      {
          private readonly IRepository _repo;
          private readonly DbContext _context;
  
  
          public ContactService(DbContext context, IRepository repo)
          {
              _context = context;
              _repo = repo;
  
          }
  
  ...
  }
  </code></pre>