title: Unit of work in mongodb and C#
body: <p>I know that MongoDB is not supposed to support unit of work, etc. But I think it would be nice to implement the repository which would store only the intentions (similar to criteria) and then commit them to the DB. Otherwise in every method in your repository you have to create connection to DB and then close it. If we place the connection to DB in some BaseRepository class, then we tie our repository to concrete DB and it is really difficult to test repositories, to test IoC which resolve repositories.</p>
  
  <p>Is creating a session in MongoDB a bad idea? Is there a way to separate the connection logic from repository?</p>
  
  <p>Here is some code by Rob Conery. Is it a good idea to always connect to your DB on every request? What is the best practice?</p>
  
  <p>There is one more thing. Imagine I want to provide an index for a collection. Previously I did in a constructor but with Rob's approach this seems out of logic to do it there. </p>
  
  <pre><code> using Norm;
      using Norm.Responses;
      using Norm.Collections;
      using Norm.Linq;
  
      public class MongoSession {
  
          private string _connectionString;
  
          public MongoSession() {
              //set this connection as you need. This is left here as an example, but you could, if you wanted,
              _connectionString = "mongodb://127.0.0.1/MyDatabase?strict=false";
          }
  
          public void Delete&lt;T&gt;(System.Linq.Expressions.Expression&lt;Func&lt;T, bool&gt;&gt; expression) where T : class, new() {
              //not efficient, NoRM should do this in a way that sends a single command to MongoDB.
              var items = All&lt;T&gt;().Where(expression);
              foreach (T item in items) {
                  Delete(item);
              }
          }
  
          public void Delete&lt;T&gt;(T item) where T : class, new() {
              using(var db = Mongo.Create(_connectionString))
              {
                db.Database.GetCollection&lt;T&gt;().Delete(item);
              }
          }
  
          public void DeleteAll&lt;T&gt;() where T : class, new() {
              using(var db = Mongo.Create(_connectionString))
              {
                db.Database.DropCollection(typeof(T).Name);
              }
          }
  
          public T Single&lt;T&gt;(System.Linq.Expressions.Expression&lt;Func&lt;T, bool&gt;&gt; expression) where T : class, new() {
              T retval = default(T);
              using(var db = Mongo.Create(_connectionString))
              {
                retval = db.GetCollection&lt;T&gt;().AsQueryable()
                           .Where(expression).SingleOrDefault();
              }
              return retval;
          }
  
          public IQueryable&lt;T&gt; All&lt;T&gt;() where T : class, new() {
              //don't keep this longer than you need it.
              var db = Mongo.Create(_connectionString);
              return db.GetCollection&lt;T&gt;().AsQueryable();
          }
  
          public void Add&lt;T&gt;(T item) where T : class, new() {
              using(var db = Mongo.Create(_connectionString))
              {
                db.GetCollection&lt;T&gt;().Insert(item);
              }
          }
  
          public void Add&lt;T&gt;(IEnumerable&lt;T&gt; items) where T : class, new() {
              //this is WAY faster than doing single inserts.
              using(var db = Mongo.Create(_connectionString))
              {
                db.GetCollection&lt;T&gt;().Insert(items);
              }
          }
  
          public void Update&lt;T&gt;(T item) where T : class, new() {
              using(var db = Mongo.Create(_connectionString))
              {
                db.GetCollection&lt;T&gt;().UpdateOne(item, item);
              }
          }
  
          //this is just some sugar if you need it.
          public T MapReduce&lt;T&gt;(string map, string reduce) {
              T result = default(T);
              using(var db = Mongo.Create(_connectionString))
              {
              var mr = db.Database.CreateMapReduce();
              MapReduceResponse response =
                  mr.Execute(new MapReduceOptions(typeof(T).Name) {
                      Map = map,
                      Reduce = reduce
                  });
              MongoCollection&lt;MapReduceResult&lt;T&gt;&gt; coll = response.GetCollection&lt;MapReduceResult&lt;T&gt;&gt;();
              MapReduceResult&lt;T&gt; r = coll.Find().FirstOrDefault();
              result = r.Value;
              }
              return result;
          }
  
          public void Dispose() {
              _server.Dispose();
          }
      }
  </code></pre>