LCOV - code coverage report
Current view: top level - Cqrs.MongoDB/DataStores - MongoDbDataStore.cs Hit Total Coverage
Test: doc-coverage.info Lines: 8 10 80.0 %
Date: 2017-07-26

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="cdmdotnet Limited">
       4             : // //   Copyright cdmdotnet Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System;
      10             : using System.Collections;
      11             : using System.Collections.Generic;
      12             : using System.Linq;
      13             : using System.Linq.Expressions;
      14             : using Cqrs.DataStores;
      15             : using cdmdotnet.Logging;
      16             : using MongoDB.Driver;
      17             : using Cqrs.Entities;
      18             : 
      19             : namespace Cqrs.MongoDB.DataStores
      20             : {
      21             :         public class MongoDbDataStore<TData> : IDataStore<TData>
      22             :                 where TData : Entity
      23           0 :         {
      24             :                 protected IMongoCollection<TData> MongoCollection { get; private set; }
      25             : 
      26             :                 protected ILogger Logger { get; private set; }
      27             : 
      28           0 :                 public MongoDbDataStore(ILogger logger, IMongoCollection<TData> mongoCollection)
      29             :                 {
      30             :                         Logger = logger;
      31             :                         MongoCollection = mongoCollection;
      32             :                         // MongoCollection.Database.RequestStart();
      33             :                 }
      34             : 
      35             :                 #region Implementation of IEnumerable
      36             : 
      37             :                 /// <summary>
      38             :                 /// Returns an enumerator that iterates through the collection.
      39             :                 /// </summary>
      40             :                 /// <returns>
      41             :                 /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
      42             :                 /// </returns>
      43             :                 /// <filterpriority>1</filterpriority>
      44           1 :                 public IEnumerator<TData> GetEnumerator()
      45             :                 {
      46             :                         return MongoCollection.AsQueryable().GetEnumerator();
      47             :                 }
      48             : 
      49             :                 /// <summary>
      50             :                 /// Returns an enumerator that iterates through a collection.
      51             :                 /// </summary>
      52             :                 /// <returns>
      53             :                 /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
      54             :                 /// </returns>
      55             :                 /// <filterpriority>2</filterpriority>
      56             :                 IEnumerator IEnumerable.GetEnumerator()
      57             :                 {
      58             :                         return GetEnumerator();
      59             :                 }
      60             : 
      61             :                 #endregion
      62             : 
      63             :                 #region Implementation of IQueryable
      64             : 
      65             :                 /// <summary>
      66             :                 /// Gets the expression tree that is associated with the instance of <see cref="T:System.Linq.IQueryable"/>.
      67             :                 /// </summary>
      68             :                 /// <returns>
      69             :                 /// The <see cref="T:System.Linq.Expressions.Expression"/> that is associated with this instance of <see cref="T:System.Linq.IQueryable"/>.
      70             :                 /// </returns>
      71             :                 public Expression Expression
      72             :                 {
      73             :                         get { return MongoCollection.AsQueryable().Expression; }
      74             :                 }
      75             : 
      76             :                 /// <summary>
      77             :                 /// Gets the type of the element(s) that are returned when the expression tree associated with this instance of <see cref="T:System.Linq.IQueryable"/> is executed.
      78             :                 /// </summary>
      79             :                 /// <returns>
      80             :                 /// A <see cref="T:System.Type"/> that represents the type of the element(s) that are returned when the expression tree associated with this object is executed.
      81             :                 /// </returns>
      82             :                 public Type ElementType
      83             :                 {
      84             :                         get { return MongoCollection.AsQueryable().ElementType; }
      85             :                 }
      86             : 
      87             :                 /// <summary>
      88             :                 /// Gets the singleResultQuery provider that is associated with this data source.
      89             :                 /// </summary>
      90             :                 /// <returns>
      91             :                 /// The <see cref="T:System.Linq.IQueryProvider"/> that is associated with this data source.
      92             :                 /// </returns>
      93             :                 public IQueryProvider Provider
      94             :                 {
      95             :                         get { return MongoCollection.AsQueryable().Provider; }
      96             :                 }
      97             : 
      98             :                 #endregion
      99             : 
     100             :                 #region Implementation of IDataStore<TData>
     101             : 
     102           1 :                 public virtual void Add(TData data)
     103             :                 {
     104             :                         Logger.LogDebug("Adding data to the Mongo database", "MongoDataStore\\Add");
     105             :                         try
     106             :                         {
     107             :                                 DateTime start = DateTime.Now;
     108             :                                 MongoCollection.InsertOne(data);
     109             :                                 DateTime end = DateTime.Now;
     110             :                                 Logger.LogDebug(string.Format("Adding data in the Mongo database took {0}.", end - start), "MongoDataStore\\Add");
     111             :                         }
     112             :                         finally
     113             :                         {
     114             :                                 Logger.LogDebug("Adding data to the Mongo database... Done", "MongoDataStore\\Add");
     115             :                         }
     116             :                 }
     117             : 
     118           1 :                 public virtual void Add(IEnumerable<TData> data)
     119             :                 {
     120             :                         Logger.LogDebug("Adding data collection to the Mongo database", "MongoDataStore\\Add");
     121             :                         try
     122             :                         {
     123             :                                 MongoCollection.InsertMany(data);
     124             :                         }
     125             :                         finally
     126             :                         {
     127             :                                 Logger.LogDebug("Adding data collection to the Mongo database... Done", "MongoDataStore\\Add");
     128             :                         }
     129             :                 }
     130             : 
     131             :                 /// <summary>
     132             :                 /// Will mark the <paramref name="data"/> as logically (or soft) by setting <see cref="Entity.IsLogicallyDeleted"/> to true
     133             :                 /// </summary>
     134           1 :                 public virtual void Remove(TData data)
     135             :                 {
     136             :                         Logger.LogDebug("Removing data from the Mongo database", "MongoDataStore\\Remove");
     137             :                         try
     138             :                         {
     139             :                                 data.IsLogicallyDeleted = true;
     140             :                                 Update(data);
     141             :                         }
     142             :                         finally
     143             :                         {
     144             :                                 Logger.LogDebug("Removing data from the Mongo database... Done", "MongoDataStore\\Remove");
     145             :                         }
     146             :                 }
     147             : 
     148           1 :                 public virtual void Destroy(TData data)
     149             :                 {
     150             :                         Logger.LogDebug("Removing data from the Mongo database", "MongoDataStore\\Destroy");
     151             :                         try
     152             :                         {
     153             :                                 DateTime start = DateTime.Now;
     154             :                                 MongoCollection.DeleteOne(x => x.Rsn == data.Rsn);
     155             :                                 DateTime end = DateTime.Now;
     156             :                                 Logger.LogDebug(string.Format("Updating data in the Mongo database took {0}.", end - start), "MongoDataStore\\Update");
     157             :                         }
     158             :                         finally
     159             :                         {
     160             :                                 Logger.LogDebug("Removing data from the Mongo database... Done", "MongoDataStore\\Destroy");
     161             :                         }
     162             :                 }
     163             : 
     164           1 :                 public virtual void RemoveAll()
     165             :                 {
     166             :                         Logger.LogDebug("Removing all from the Mongo database", "MongoDataStore\\RemoveAll");
     167             :                         try
     168             :                         {
     169             :                                 MongoCollection.DeleteMany(x => true);
     170             :                         }
     171             :                         finally
     172             :                         {
     173             :                                 Logger.LogDebug("Removing all from the Mongo database... Done", "MongoDataStore\\RemoveAll");
     174             :                         }
     175             :                 }
     176             : 
     177           1 :                 public virtual void Update(TData data)
     178             :                 {
     179             :                         Logger.LogDebug("Updating data in the Mongo database", "MongoDataStore\\Update");
     180             :                         try
     181             :                         {
     182             :                                 DateTime start = DateTime.Now;
     183             :                                 MongoCollection.ReplaceOne(x => x.Rsn == data.Rsn, data);
     184             :                                 DateTime end = DateTime.Now;
     185             :                                 Logger.LogDebug(string.Format("Updating data in the Mongo database took {0}.", end - start), "MongoDataStore\\Update");
     186             :                         }
     187             :                         finally
     188             :                         {
     189             :                                 Logger.LogDebug("Updating data to the Mongo database... Done", "MongoDataStore\\Update");
     190             :                         }
     191             :                 }
     192             : 
     193             :                 #endregion
     194             : 
     195             :                 #region Implementation of IDisposable
     196             : 
     197             :                 /// <summary>
     198             :                 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
     199             :                 /// </summary>
     200           1 :                 public virtual void Dispose()
     201             :                 {
     202             :                         // MongoCollection.Database.RequestDone();
     203             :                 }
     204             : 
     205             :                 #endregion
     206             :         }
     207             : }

Generated by: LCOV version 1.10