LCOV - code coverage report
Current view: top level - Cqrs/Repositories - InMemoryDatabase.cs Hit Total Coverage
Test: doc-coverage.info Lines: 10 13 76.9 %
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.Concurrent;
      12             : using System.Collections.Generic;
      13             : using System.Linq;
      14             : using Cqrs.Entities;
      15             : 
      16             : namespace Cqrs.Repositories
      17             : {
      18             :         /// <summary>
      19             :         /// Uses a static <see cref="ConcurrentDictionary{TKey,TValue}"/> to store data accessible by all threads.
      20             :         /// </summary>
      21             :         public class InMemoryDatabase
      22           1 :         {
      23             :                 private static IDictionary<Type, object> Database { get; set; }
      24             : 
      25             :                 static InMemoryDatabase()
      26             :                 {
      27             :                         Database = new ConcurrentDictionary<Type, object>();
      28             :                 }
      29             : 
      30           0 :                 public IDictionary<Guid, TEntity> Get<TEntity>()
      31             :                         where TEntity : Entity
      32             :                 {
      33             :                         IDictionary<Guid, TEntity> result;
      34             :                         if (!Database.ContainsKey(typeof(TEntity)))
      35             :                         {
      36             :                                 result = new Dictionary<Guid, TEntity>();
      37             :                                 Database.Add(typeof(TEntity), result);
      38             :                         }
      39             :                         else
      40             :                         {
      41             :                                 object rawResult = Database[typeof(TEntity)];
      42             :                                 result = (IDictionary<Guid, TEntity>)rawResult;
      43             :                         }
      44             :                         return result;
      45             :                 }
      46             : 
      47           0 :                 public IList<TEntity> GetAll<TEntity>()
      48             :                         where TEntity : Entity
      49             :                 {
      50             :                         IDictionary<Guid, TEntity> result = Get<TEntity>();
      51             : 
      52             :                         return new CollectionWrapper<TEntity>(result);
      53             :                 }
      54             : 
      55             :                 class CollectionWrapper<TEntity> : IList<TEntity>
      56             :                         where TEntity : Entity
      57             :                 {
      58             :                         IDictionary<Guid, TEntity> Source { get; set; }
      59             : 
      60           0 :                         public CollectionWrapper(IDictionary<Guid, TEntity> source)
      61             :                         {
      62             :                                 Source = source;
      63             :                         }
      64             : 
      65             :                         #region Implementation of IEnumerable
      66             : 
      67             :                         /// <summary>
      68             :                         /// Returns an enumerator that iterates through the collection.
      69             :                         /// </summary>
      70             :                         /// <returns>
      71             :                         /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
      72             :                         /// </returns>
      73           1 :                         public IEnumerator<TEntity> GetEnumerator()
      74             :                         {
      75             :                                 return Source.Values.GetEnumerator();
      76             :                         }
      77             : 
      78             :                         /// <summary>
      79             :                         /// Returns an enumerator that iterates through a collection.
      80             :                         /// </summary>
      81             :                         /// <returns>
      82             :                         /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
      83             :                         /// </returns>
      84             :                         IEnumerator IEnumerable.GetEnumerator()
      85             :                         {
      86             :                                 return GetEnumerator();
      87             :                         }
      88             : 
      89             :                         #endregion
      90             : 
      91             :                         #region Implementation of ICollection<T>
      92             : 
      93             :                         /// <summary>
      94             :                         /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
      95             :                         /// </summary>
      96             :                         /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
      97           1 :                         public void Add(TEntity item)
      98             :                         {
      99             :                                 Source.Add(item.Rsn, item);
     100             :                         }
     101             : 
     102             :                         /// <summary>
     103             :                         /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
     104             :                         /// </summary>
     105             :                         /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
     106           1 :                         public void Clear()
     107             :                         {
     108             :                                 Source.Clear();
     109             :                         }
     110             : 
     111             :                         /// <summary>
     112             :                         /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
     113             :                         /// </summary>
     114             :                         /// <returns>
     115             :                         /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
     116             :                         /// </returns>
     117             :                         /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
     118           1 :                         public bool Contains(TEntity item)
     119             :                         {
     120             :                                 return Source.Values.Contains(item);
     121             :                         }
     122             : 
     123             :                         /// <summary>
     124             :                         /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
     125             :                         /// </summary>
     126             :                         /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param><param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is null.</exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception><exception cref="T:System.ArgumentException"><paramref name="array"/> is multidimensional.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.-or-Type <paramref name="TEntity"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.</exception>
     127           1 :                         public void CopyTo(TEntity[] array, int arrayIndex)
     128             :                         {
     129             :                                 Source.Values.CopyTo(array, arrayIndex);
     130             :                         }
     131             : 
     132             :                         /// <summary>
     133             :                         /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
     134             :                         /// </summary>
     135             :                         /// <returns>
     136             :                         /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
     137             :                         /// </returns>
     138             :                         /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
     139           1 :                         public bool Remove(TEntity item)
     140             :                         {
     141             :                                 return Source.Remove(item.Rsn);
     142             :                         }
     143             : 
     144             :                         /// <summary>
     145             :                         /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
     146             :                         /// </summary>
     147             :                         /// <returns>
     148             :                         /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
     149             :                         /// </returns>
     150             :                         public int Count
     151             :                         {
     152             :                                 get { return Source.Count; }
     153             :                         }
     154             : 
     155             :                         /// <summary>
     156             :                         /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
     157             :                         /// </summary>
     158             :                         /// <returns>
     159             :                         /// true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
     160             :                         /// </returns>
     161             :                         public bool IsReadOnly
     162             :                         {
     163             :                                 get { return Source.IsReadOnly; }
     164             :                         }
     165             : 
     166             :                         #endregion
     167             : 
     168             :                         #region Implementation of IList<T>
     169             : 
     170             :                         /// <summary>
     171             :                         /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
     172             :                         /// </summary>
     173             :                         /// <returns>
     174             :                         /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
     175             :                         /// </returns>
     176             :                         /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
     177           1 :                         public int IndexOf(TEntity item)
     178             :                         {
     179             :                                 return Source.Values.ToList().IndexOf(item);
     180             :                         }
     181             : 
     182             :                         /// <summary>
     183             :                         /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
     184             :                         /// </summary>
     185             :                         /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param><param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"/>.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
     186           1 :                         public void Insert(int index, TEntity item)
     187             :                         {
     188             :                                 Add(item);
     189             :                         }
     190             : 
     191             :                         /// <summary>
     192             :                         /// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index.
     193             :                         /// </summary>
     194             :                         /// <param name="index">The zero-based index of the item to remove.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
     195           1 :                         public void RemoveAt(int index)
     196             :                         {
     197             :                                 Remove(Source.Values.ToList()[index]);
     198             :                         }
     199             : 
     200             :                         /// <summary>
     201             :                         /// Gets or sets the element at the specified index.
     202             :                         /// </summary>
     203             :                         /// <returns>
     204             :                         /// The element at the specified index.
     205             :                         /// </returns>
     206             :                         /// <param name="index">The zero-based index of the element to get or set.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception><exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
     207             :                         public TEntity this[int index]
     208             :                         {
     209             :                                 get { return Source.Values.ToList()[index]; }
     210             :                                 set { Source[this[index].Rsn] = value; }
     211             :                         }
     212             : 
     213             :                         #endregion
     214             :                 }
     215             :         }
     216             : }

Generated by: LCOV version 1.10