title: UnitOfWork with Unity and Entity Framework
body: <p>Hi I am using Unity to manage my service layers, which in turn speak to UnitOfWork which manages all the repositories.</p>
  
  <p>Some of my services call other services, my question is how can i pass the same UnitOfWork between service layers?</p>
  
  <p>In my case all controller actions are initiated from a GUI on each button action or event on a timer, this is why I have a factory to create UnitOfWork on demand, but it is causing issues as i dont know how to pass this UnitOfWork between services.</p>
  
  <p>Especially difficult is knowing how to get this specific UnitOfWork instance injected into the service constructor. Please note that some of the services may be long running (10 minutes or so on a background thread), i don't know if that has any impact on the design or not.</p>
  
  <p>Currently the service that is called from the other service is then creating its own UnitOfWork which is causing issues for both transactional design, and Entity framework entity tracking.</p>
  
  <p>Suggestions very welcome!</p>
  
  <pre><code>class OtherService : IOtherService
  {
      public OtherService(IUnitOfWorkFactory unitOfworkFactory, 
          ISettingsService settingsService)
      {
          UnitOfWorkFactory = unitOfworkFactory;
          SettingsService = settingsService;
      }
      IUnitOfWorkFactory UnitOfWorkFactory;
      ISettingsService SettingsService;
  
      function SomeSeviceCall()
      {
          // Perhaps one way is to use a factory to instantiate a 
          // SettingService, and pass in the UnitOfWork here?
          // Ideally it would be nice for Unity to handle all of 
          // the details regardless of a service being called from
          // another service or called directly from a controller
          // ISettingsService settingsService = 
          //     UnityContainer.Resolve&lt;ISettingService&gt;();
  
          using (var uow = UnitOfWorkFactory.CreateUnitOfWork())
          {
              var companies = uow.CompaniesRepository.GetAll();
              foreach(Company company in companies)
              {
                  settingsService.SaveSettings(company, "value");
                  company.Processed = DateTime.UtcNow();
              }
              uow.Save();
          }
      }
  }
  
  class SettingsService : ISettingsService
  {
      public SettingsService(IUnitOfWorkFactory unitOfworkFactory)
      {
          UnitOfWorkFactory = unitOfworkFactory;
      }
      IUnitOfWorkFactory UnitOfWorkFactory;
  
      // ISettingsService.SaveSettings code in another module...
      function void ISettingsService.SaveSettings(Company company, 
          string value)
      {
          // this is causing an issue as it essentially creates a 
          // sub-transaction with the new UnitOfWork creating a new 
          // Entiy Framework context
          using (var uow = UnitOfWorkFactory.CreateUnitOfWork())
          {
              Setting setting = new Setting();
              setting.CompanyID = company.CompanyID;
              setting.SettingValue = value;
              uow.Insert(setting);
              uow.Save();
          }
      }
  }
  </code></pre>