title: C# Mongodb driver: Translate Action into an UpdateDefinition
body: <p>I am trying to generalise updates to my mongodb, so I can make updating my data - storage agnostic. With EntityFramework, this is super easy as it has change tracking and I therefore can seperate business logic and database specific operations. When it comes to MongoDb, it is not as easy, since I have to create these UpdateDefinitions, to do updates on documents.</p>
  
  <p>An easy way to mimic this would be to use <code>ReplaceOneAsync</code>, but this is not an option, since more than one party could be writing to my collections at the same time. I am for example using it for a chat system, within my application.</p>
  
  <p>I wanted to do something like this:</p>
  
  <pre><code>public class MongoActionWrapper&lt;TCollection&gt;
  {
      public MongoActionWrapper(Action&lt;TCollection&gt; updateAction)
      {
          UpdateAction = updateAction;
      }
  
      public Action&lt;TCollection&gt; UpdateAction { get; }
  }
  </code></pre>
  
  <p>And then I have my MongoRepository:</p>
  
  <pre><code>public abstract class BaseMongoRepository&lt;TAggregate, TCollection&gt; : IRepository&lt;TAggregate&gt; where TAggregate : BaseAggregateRoot, IMongoAggregate&lt;TCollection&gt;
  {
      private readonly IMongoCollection&lt;TCollection&gt; _mongoCollection;
  
      protected BaseMongoRepository(IMongoCollection&lt;TCollection&gt; mongoCollection)
      {
          _mongoCollection = mongoCollection;
      }
  
      public async void SaveAsync(TAggregate aggregate)
      {
          var state = aggregate.GetState();
          foreach (var action in state.ActionsToExecuteOnCollection)
          {
              await _mongoCollection.UpdateOneAsync&lt;TCollection&gt;(aggregate.GetSelector(), action.UpdateAction);
          }
      }
  }
  </code></pre>
  
  <p>Where I would like to execute the action on the collection instead of an UpdateDefinition. Or somehow translate my Action into an UpdateDefinition. </p>
  
  <p>This would make me able to apply the same updates on a list (or whatever other collection, where I want to store my data), so that I am not forced to stay with mongodb.</p>
  
  <p>My only solution so far, would be to make my <code>UpdateAction</code> more like an object that describes the updates, and then translate these objects, when I want to persist it in mongodb. This way I can translate them into Actions and apply them to my collection or whatever other DB I would choose.</p>
  
  <p>I hope somebody has an idea, since I am running out of options here.</p>