title: Entity Framework + Life Time + StructureMap + Unit Of Work + Windows Forms?
body: <p>I have a <strong>windows service (or a Windows Forms)</strong> and when a timer is elapsed, my service completes some tasks.</p>
  
  <p>I don't want to use just <strong>one Entity Framework context</strong> as long as my app is running.</p>
  
  <p>I saw <strong>session factory in NHibernate</strong> samples. Is there any thing like that for <strong>EF</strong>?</p>
  
  <p>Here is how I use <strong>EF</strong> + <strong>StructureMap</strong> in my <strong>ASP.NET or MVC</strong> apps:</p>
  
  <p><em>Application Start:</em></p>
  
  <pre><code>ObjectFactory.Configure(Function(config) config.For(Of IUnitOfWork).HybridHttpOrThreadLocalScoped.Use(Of UnitOfWork)())
  </code></pre>
  
  <p><em>End Request:</em></p>
  
  <pre><code>ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects()
  </code></pre>
  
  <p>My <strong>UnitOfWork</strong> is Started <strong>per web request</strong>, and is <strong>disposed</strong> after request is ended.</p>
  
  <p>In my <strong>Win App scenario</strong>, <strong>UoW</strong> should start when the timer is elapsed and disposed after my job is complete and <strong>I don't want to handle it myself.</strong></p>
  
  <p><em>How can I achieve something like this in a windows form app?</em></p>
  
  <p>If I register my UoW using : <code>HybridHttprThreadLocalScoped</code> There is just <strong>one UoW per thread</strong> and if I <strong>dispose</strong> it, <strong>it's gone</strong>.</p>
  
  <p><em><strong>EDIT:</em></strong></p>
  
  <p>I have my <strong>repositories + services</strong> in a <strong>dll</strong> and I want to use them in <strong>both web or win apps</strong>, and because of that I used <code>HybridHttpOrThreadLocalScoped</code>.</p>
  
  <p><em><strong>Update 2:</em></strong></p>
  
  <p>Here is my <strong>Repository</strong>:</p>
  
  <pre><code>Public Class ProductRepository
  
      Private ReadOnly _databaseFactory As DataAccess.IDatabaseFactory
  
      Public Sub New(ByVal databaseFactory As DataAccess.IDatabaseFactory)
          _databaseFactory = databaseFactory
      End Sub
  
      Public Function GetById(ByVal id As Integer) As Product
          Return (From item In _databaseFactory.GetDataContext.Products Where item.ProductId = id).FirstOrDefault
      End Function
  
  End Class
  </code></pre>
  
  <p>and here is my <strong>Service</strong>:</p>
  
  <pre><code>Public Class ProductService
  
      Private ReadOnly _Repository As ProductRepository
  
      Public Sub New(ByVal repository As ProductRepository)
          _Repository = repository
      End Sub
  
      Public Function GetById(ByVal id As Integer) As Product
          Return _Repository.GetById(id)
      End Function
  
  End Class
  </code></pre>
  
  <p>And I'm using <strong>StructureMap</strong>. StructureMap takes car of my <strong>DatabaseFactory</strong> and <strong>UoW lifetime</strong> in <strong>web</strong> scenarios. But I have no idea what to do in <strong>win apps</strong>. I can manage lifetime of UoW <strong>myself</strong>, but I'm searching for a way so StructureMap would <strong>handle it for me</strong> as it handles the lifetime in web apps.</p>