title: Hangfire and Unit of Work
body: <p>I am sure this is mostly my lack of understanding around generics and expressions, but I am trying to create a list of <code>Expression&lt;Action&lt;T&gt;&gt;</code> in order to call BackgroundJob.Enqueue once I am in a commit state.</p>
  
  <p>I have several areas of the code that may want to enqueue a job, but they don't know if the job should actually run until another section of the code, specifically a save to the db, occurs.</p>
  
  <p>What I am struggling with is creating a <code>List&lt;Expression&lt;Action&lt;T&gt;&gt;&gt;</code> when T is obviously different, and passing it to BackgroundJob.Enqueue when ready. </p>
  
  <pre><code>public class HangfireQueue
  {
      private readonly IList&lt;Expression&lt;Action&lt;object&gt;&gt;&gt; _queuedItems;
  
      public HangfireQueue()
      {
          _queuedItems = new List&lt;Expression&lt;Action&lt;object&gt;&gt;&gt;();
      }
  
      public virtual void EnqueueTask&lt;T&gt;( Expression&lt;Action&lt;T&gt;&gt;  methodCall )
      {
          _queuedItems.Add( methodCall );
      }
  
      public void CommitUnitOfWork()
      {
          foreach ( var item in _queuedItems )
          {
              BackgroundJob.Enqueue( (Expression&lt;Action&gt;) item );
          }
      }
  }
  </code></pre>
  
  <p>I have tried a number of different methods, this happens to be the lastest attempt, sometimes I get it to compile and it doesn't like it at run time, unable to cast <code>Expression&lt;Action&lt;T&gt;&gt;</code> to <code>Expression&lt;Action&gt;</code> would be part of the problems here, not to mention that the methodCall would not be accepted in the list.</p>