Documentation Coverage Report
Current view: top level - Cqrs.MongoDB/DataStores - MongoDbDataStore.cs Hit Total Coverage
Version: 2.2 Artefacts: 11 11 100.0 %
Date: 2017-09-22

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="Chinchilla Software Limited">
       4             : // //   Copyright Chinchilla Software 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             :         /// <summary>
      22             :         /// A <see cref="IDataStore{TData}"/> that uses MongoDB for storage.
      23             :         /// </summary>
      24             :         /// <typeparam name="TData">The <see cref="Type"/> of <see cref="IEntity"/> the <see cref="IDataStore{TData}"/> will contain.</typeparam>
      25             :         public class MongoDbDataStore<TData> : IDataStore<TData>
      26             :                 where TData : Entity
      27           1 :         {
      28             :                 /// <summary>
      29             :                 /// Gets or sets the <see cref="IMongoCollection{TData}"/>
      30             :                 /// </summary>
      31             :                 protected IMongoCollection<TData> MongoCollection { get; private set; }
      32             : 
      33             :                 /// <summary>
      34             :                 /// Gets or sets the <see cref="ILogger"/>
      35             :                 /// </summary>
      36             :                 protected ILogger Logger { get; private set; }
      37             : 
      38             :                 /// <summary>
      39             :                 /// Instantiates and Initialises a new instance of the <see cref="MongoDbDataStore{TData}"/> class.
      40             :                 /// </summary>
      41           1 :                 public MongoDbDataStore(ILogger logger, IMongoCollection<TData> mongoCollection)
      42             :                 {
      43             :                         Logger = logger;
      44             :                         MongoCollection = mongoCollection;
      45             :                         // MongoCollection.Database.RequestStart();
      46             :                 }
      47             : 
      48             :                 #region Implementation of IEnumerable
      49             : 
      50             :                 /// <summary>
      51             :                 /// Returns an enumerator that iterates through the collection.
      52             :                 /// </summary>
      53             :                 /// <returns>
      54             :                 /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
      55             :                 /// </returns>
      56             :                 /// <filterpriority>1</filterpriority>
      57           1 :                 public IEnumerator<TData> GetEnumerator()
      58             :                 {
      59             :                         return MongoCollection.AsQueryable().GetEnumerator();
      60             :                 }
      61             : 
      62             :                 /// <summary>
      63             :                 /// Returns an enumerator that iterates through a collection.
      64             :                 /// </summary>
      65             :                 /// <returns>
      66             :                 /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
      67             :                 /// </returns>
      68             :                 /// <filterpriority>2</filterpriority>
      69             :                 IEnumerator IEnumerable.GetEnumerator()
      70             :                 {
      71             :                         return GetEnumerator();
      72             :                 }
      73             : 
      74             :                 #endregion
      75             : 
      76             :                 #region Implementation of IQueryable
      77             : 
      78             :                 /// <summary>
      79             :                 /// Gets the expression tree that is associated with the instance of <see cref="T:System.Linq.IQueryable"/>.
      80             :                 /// </summary>
      81             :                 /// <returns>
      82             :                 /// The <see cref="T:System.Linq.Expressions.Expression"/> that is associated with this instance of <see cref="T:System.Linq.IQueryable"/>.
      83             :                 /// </returns>
      84             :                 public Expression Expression
      85             :                 {
      86             :                         get { return MongoCollection.AsQueryable().Expression; }
      87             :                 }
      88             : 
      89             :                 /// <summary>
      90             :                 /// 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.
      91             :                 /// </summary>
      92             :                 /// <returns>
      93             :                 /// 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.
      94             :                 /// </returns>
      95             :                 public Type ElementType
      96             :                 {
      97             :                         get { return MongoCollection.AsQueryable().ElementType; }
      98             :                 }
      99             : 
     100             :                 /// <summary>
     101             :                 /// Gets the singleResultQuery provider that is associated with this data source.
     102             :                 /// </summary>
     103             :                 /// <returns>
     104             :                 /// The <see cref="T:System.Linq.IQueryProvider"/> that is associated with this data source.
     105             :                 /// </returns>
     106             :                 public IQueryProvider Provider
     107             :                 {
     108             :                         get { return MongoCollection.AsQueryable().Provider; }
     109             :                 }
     110             : 
     111             :                 #endregion
     112             : 
     113             :                 #region Implementation of IDataStore<TData>
     114             : 
     115             :                 /// <summary>
     116             :                 /// Add the provided <paramref name="data"/> to the data store and persist the change.
     117             :                 /// </summary>
     118           1 :                 public virtual void Add(TData data)
     119             :                 {
     120             :                         Logger.LogDebug("Adding data to the Mongo database", "MongoDbDataStore\\Add");
     121             :                         try
     122             :                         {
     123             :                                 DateTime start = DateTime.Now;
     124             :                                 MongoCollection.InsertOne(data);
     125             :                                 DateTime end = DateTime.Now;
     126             :                                 Logger.LogDebug(string.Format("Adding data in the Mongo database took {0}.", end - start), "MongoDbDataStore\\Add");
     127             :                         }
     128             :                         finally
     129             :                         {
     130             :                                 Logger.LogDebug("Adding data to the Mongo database... Done", "MongoDbDataStore\\Add");
     131             :                         }
     132             :                 }
     133             : 
     134             :                 /// <summary>
     135             :                 /// Add the provided <paramref name="data"/> to the data store and persist the change.
     136             :                 /// </summary>
     137           1 :                 public virtual void Add(IEnumerable<TData> data)
     138             :                 {
     139             :                         Logger.LogDebug("Adding data collection to the Mongo database", "MongoDbDataStore\\Add");
     140             :                         try
     141             :                         {
     142             :                                 MongoCollection.InsertMany(data);
     143             :                         }
     144             :                         finally
     145             :                         {
     146             :                                 Logger.LogDebug("Adding data collection to the Mongo database... Done", "MongoDbDataStore\\Add");
     147             :                         }
     148             :                 }
     149             : 
     150             :                 /// <summary>
     151             :                 /// Will mark the <paramref name="data"/> as logically (or soft) by setting <see cref="Entity.IsLogicallyDeleted"/> to true
     152             :                 /// </summary>
     153           1 :                 public virtual void Remove(TData data)
     154             :                 {
     155             :                         Logger.LogDebug("Removing data from the Mongo database", "MongoDbDataStore\\Remove");
     156             :                         try
     157             :                         {
     158             :                                 data.IsLogicallyDeleted = true;
     159             :                                 Update(data);
     160             :                         }
     161             :                         finally
     162             :                         {
     163             :                                 Logger.LogDebug("Removing data from the Mongo database... Done", "MongoDbDataStore\\Remove");
     164             :                         }
     165             :                 }
     166             : 
     167             :                 /// <summary>
     168             :                 /// Remove the provided <paramref name="data"/> (normally by <see cref="IEntity.Rsn"/>) from the data store and persist the change.
     169             :                 /// </summary>
     170           1 :                 public virtual void Destroy(TData data)
     171             :                 {
     172             :                         Logger.LogDebug("Removing data from the Mongo database", "MongoDbDataStore\\Destroy");
     173             :                         try
     174             :                         {
     175             :                                 DateTime start = DateTime.Now;
     176             :                                 MongoCollection.DeleteOne(x => x.Rsn == data.Rsn);
     177             :                                 DateTime end = DateTime.Now;
     178             :                                 Logger.LogDebug(string.Format("Updating data in the Mongo database took {0}.", end - start), "MongoDbDataStore\\Update");
     179             :                         }
     180             :                         finally
     181             :                         {
     182             :                                 Logger.LogDebug("Removing data from the Mongo database... Done", "MongoDbDataStore\\Destroy");
     183             :                         }
     184             :                 }
     185             : 
     186             :                 /// <summary>
     187             :                 /// Remove all contents (normally by use of a truncate operation) from the data store and persist the change.
     188             :                 /// </summary>
     189           1 :                 public virtual void RemoveAll()
     190             :                 {
     191             :                         Logger.LogDebug("Removing all from the Mongo database", "MongoDbDataStore\\RemoveAll");
     192             :                         try
     193             :                         {
     194             :                                 MongoCollection.DeleteMany(x => true);
     195             :                         }
     196             :                         finally
     197             :                         {
     198             :                                 Logger.LogDebug("Removing all from the Mongo database... Done", "MongoDbDataStore\\RemoveAll");
     199             :                         }
     200             :                 }
     201             : 
     202             :                 /// <summary>
     203             :                 /// Update the provided <paramref name="data"/> in the data store and persist the change.
     204             :                 /// </summary>
     205           1 :                 public virtual void Update(TData data)
     206             :                 {
     207             :                         Logger.LogDebug("Updating data in the Mongo database", "MongoDbDataStore\\Update");
     208             :                         try
     209             :                         {
     210             :                                 DateTime start = DateTime.Now;
     211             :                                 MongoCollection.ReplaceOne(x => x.Rsn == data.Rsn, data);
     212             :                                 DateTime end = DateTime.Now;
     213             :                                 Logger.LogDebug(string.Format("Updating data in the Mongo database took {0}.", end - start), "MongoDbDataStore\\Update");
     214             :                         }
     215             :                         finally
     216             :                         {
     217             :                                 Logger.LogDebug("Updating data to the Mongo database... Done", "MongoDbDataStore\\Update");
     218             :                         }
     219             :                 }
     220             : 
     221             :                 #endregion
     222             : 
     223             :                 #region Implementation of IDisposable
     224             : 
     225             :                 /// <summary>
     226             :                 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
     227             :                 /// </summary>
     228           1 :                 public virtual void Dispose()
     229             :                 {
     230             :                         // MongoCollection.Database.RequestDone();
     231             :                 }
     232             : 
     233             :                 #endregion
     234             : 
     235             :                 /// <summary>
     236             :                 /// Executes the "repairDatabase" command on the current database.
     237             :                 /// </summary>
     238           1 :                 public void Repair()
     239             :                 {
     240             :                         Logger.LogDebug("Repairing the Mongo database", "MongoDbDataStore\\Repair");
     241             :                         try
     242             :                         {
     243             :                                 DateTime start = DateTime.Now;
     244             :                                 MongoCollection.Database.RunCommand(new JsonCommand<object>("{ repairDatabase: 1 }"));
     245             :                                 DateTime end = DateTime.Now;
     246             :                                 Logger.LogDebug(string.Format("Repairing the Mongo database took {0}.", end - start), "MongoDbDataStore\\Repair");
     247             :                         }
     248             :                         finally
     249             :                         {
     250             :                                 Logger.LogDebug("Repairing the Mongo database... Done", "MongoDbDataStore\\Repair");
     251             :                         }
     252             :                 }
     253             :         }
     254             : }

Generated by: LCOV version 1.10