title: Unity IOC and Factory pattern, and repository pattern
body: <p>I am trying to use the Unity Block of the enterprise library in a program i am writing.</p>
  
  <p>But i think i am using dependency injection wrong.  I was wondering if some one could point me in the right direction.</p>
  
  <pre><code>    static void Main(string[] args)
          {
                  using (IUnityContainer container = new UnityContainer())
                  {
                      InitialiseContainer(container);
                      DataCopierFactory dcFactory = new DataCopierFactory();
  
                      ERunOptions dataCopierType = ExtractParams(args);
  
                      IDataCopier dataCopier = dcFactory.CreateDataCopier((int)dataCopierType, container);
                      dataCopier.DetectChanges();
                      dataCopier.ParseData();
                      dataCopier.CopyData();
                  }
              }
           }
   //use the ioc container to register the EF context type to the repository interfaces..
          private static void InitialiseContainer(IUnityContainer container)
          {
  
              //add Extensions:
              container.AddNewExtension&lt;Interception&gt;();
  
              //Licence Schedule
              container.RegisterType&lt;IEFContext, LTE_DownFromWeb_EFContext&gt;("DataCopier.ScheduleDataCopier.Source");
              container.RegisterType&lt;IEFContext, LTE_Licensing_EFContext&gt;("DataCopier.ScheduleDataCopier.Destination");
  
              container.RegisterType&lt;IRepositorySession&gt;("Schedule_Source",new InjectionConstructor(container.Resolve&lt;IEFContext&gt;("DataCopier.ScheduleDataCopier.Source")));
              container.RegisterType&lt;IRepositorySession&gt;("Schedule_Destination",new InjectionConstructor(container.Resolve&lt;IEFContext&gt;("DataCopier.ScheduleDataCopier.Destination")));
  
  
  
          }
  </code></pre>
  
  <p>So basically the DataCopier Factory creates an instance of a DataCopier like so:</p>
  
  <p>DataCopierFactory:</p>
  
  <pre><code> //return a data copier that will transfer data from any DB to any other DB
          public IDataCopier CreateDataCopier(int i, IUnityContainer container)
          {
              switch(i)
              {
                  case 1:
                      return new ScheduleDataCopier(container);
  
                  default:
                      throw new InvalidOperationException("Parameter " + i + " does not exist");
              }
          }
  </code></pre>
  
  <p>a data copier looks like this:</p>
  
  <pre><code>     class ScheduleDataCopier : IDataCopier
      {
          private List&lt;Site&gt; _sites;
          private List&lt;SitesAndApparatuses&gt; _scheduleList;
          private IUnityContainer _container;
          public ScheduleDataCopier(IUnityContainer container)
          {
              _container = container;
              _scheduleList = new List&lt;SitesAndApparatuses&gt;();
          }
  
          //check if new sites registration has arrived in tblSites on down from web db.
          public bool DetectChanges()
          {
              using (var db = _container.Resolve&lt;IRepositorySession&gt;("Schedule_Source"))
              { 
                  SiteAudit lastSite = new SitesAuditRepository().GetLatest();
                  var sitesRepo = new SitesRepository();
                  var sites = sitesRepo.Where(x =&gt; x.SID &gt; lastSite.SALatestSID);
  
                  if (sites.Count() &lt; 1)
                  {
                      return false;
                  }
                  _sites = sites.ToList();
                  db.Dispose();
              }
              return true;
          }
          //parse the data into a list of object SitesAndApparatuses
          public bool ParseData()
          {
              try
              {
                  foreach (Site s in _sites)
                  {
                      var schedule = (SitesAndApparatuses)XmlObjectBuilder.Deserialize(typeof(SitesAndApparatuses), s.XMLFile);
                      schedule.acCode = s.Registration.RAcCode;
                      _scheduleList.Add(schedule);
                  }
              }
              catch (Exception ex)
              {
                  throw new NotImplementedException("HANDLE THIS SHIT!", ex);
              }
              return true;
          }
  
          public bool CopyData()
          {
              try
              {
                 using (var db = _container.Resolve&lt;IRepositorySession&gt;("Schedule_Destination"))
                  {
                          var licensingScheduleRepo = new LicensingScheduleRepository();
                         //some logic
                      db.Commit();
                  }
              }
              catch (Exception ex)
              {
              }
                  return true;
          }
  }
  </code></pre>
  
  <p>Second question, i resolve my unit of work object called RepositorySession in the Datacopier classes using the unity container i passed... is this the wrong approach and why, im struggling to find any info on it online?</p>
  
  <p>This is probably too much code for someone to read.. but im hoping for an answer!</p>
  
  <p>Thanks in advance
  Neil</p>