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 : public class InMemoryDatabase
19 0 : {
20 : private static IDictionary<Type, object> Database { get; set; }
21 :
22 : static InMemoryDatabase()
23 : {
24 : Database = new ConcurrentDictionary<Type, object>();
25 : }
26 :
27 0 : public IDictionary<Guid, TEntity> Get<TEntity>()
28 : where TEntity : Entity
29 : {
30 : IDictionary<Guid, TEntity> result;
31 : if (!Database.ContainsKey(typeof(TEntity)))
32 : {
33 : result = new Dictionary<Guid, TEntity>();
34 : Database.Add(typeof(TEntity), result);
35 : }
36 : else
37 : {
38 : object rawResult = Database[typeof(TEntity)];
39 : result = (IDictionary<Guid, TEntity>)rawResult;
40 : }
41 : return result;
42 : }
43 :
44 0 : public IList<TEntity> GetAll<TEntity>()
45 : where TEntity : Entity
46 : {
47 : IDictionary<Guid, TEntity> result = Get<TEntity>();
48 :
49 : return new CollectionWrapper<TEntity>(result);
50 : }
51 :
52 : class CollectionWrapper<TEntity> : IList<TEntity>
53 : where TEntity : Entity
54 : {
55 : IDictionary<Guid, TEntity> Source { get; set; }
56 :
57 0 : public CollectionWrapper(IDictionary<Guid, TEntity> source)
58 : {
59 : Source = source;
60 : }
61 :
62 : #region Implementation of IEnumerable
63 :
64 : /// <summary>
65 : /// Returns an enumerator that iterates through the collection.
66 : /// </summary>
67 : /// <returns>
68 : /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
69 : /// </returns>
70 1 : public IEnumerator<TEntity> GetEnumerator()
71 : {
72 : return Source.Values.GetEnumerator();
73 : }
74 :
75 : /// <summary>
76 : /// Returns an enumerator that iterates through a collection.
77 : /// </summary>
78 : /// <returns>
79 : /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
80 : /// </returns>
81 : IEnumerator IEnumerable.GetEnumerator()
82 : {
83 : return GetEnumerator();
84 : }
85 :
86 : #endregion
87 :
88 : #region Implementation of ICollection<T>
89 :
90 : /// <summary>
91 : /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
92 : /// </summary>
93 : /// <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>
94 1 : public void Add(TEntity item)
95 : {
96 : Source.Add(item.Rsn, item);
97 : }
98 :
99 : /// <summary>
100 : /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
101 : /// </summary>
102 : /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
103 1 : public void Clear()
104 : {
105 : Source.Clear();
106 : }
107 :
108 : /// <summary>
109 : /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
110 : /// </summary>
111 : /// <returns>
112 : /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
113 : /// </returns>
114 : /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
115 1 : public bool Contains(TEntity item)
116 : {
117 : return Source.Values.Contains(item);
118 : }
119 :
120 : /// <summary>
121 : /// 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.
122 : /// </summary>
123 : /// <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>
124 1 : public void CopyTo(TEntity[] array, int arrayIndex)
125 : {
126 : Source.Values.CopyTo(array, arrayIndex);
127 : }
128 :
129 : /// <summary>
130 : /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
131 : /// </summary>
132 : /// <returns>
133 : /// 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"/>.
134 : /// </returns>
135 : /// <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>
136 1 : public bool Remove(TEntity item)
137 : {
138 : return Source.Remove(item.Rsn);
139 : }
140 :
141 : /// <summary>
142 : /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
143 : /// </summary>
144 : /// <returns>
145 : /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
146 : /// </returns>
147 : public int Count
148 : {
149 : get { return Source.Count; }
150 : }
151 :
152 : /// <summary>
153 : /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
154 : /// </summary>
155 : /// <returns>
156 : /// true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
157 : /// </returns>
158 : public bool IsReadOnly
159 : {
160 : get { return Source.IsReadOnly; }
161 : }
162 :
163 : #endregion
164 :
165 : #region Implementation of IList<T>
166 :
167 : /// <summary>
168 : /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
169 : /// </summary>
170 : /// <returns>
171 : /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
172 : /// </returns>
173 : /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
174 1 : public int IndexOf(TEntity item)
175 : {
176 : return Source.Values.ToList().IndexOf(item);
177 : }
178 :
179 : /// <summary>
180 : /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
181 : /// </summary>
182 : /// <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>
183 1 : public void Insert(int index, TEntity item)
184 : {
185 : Add(item);
186 : }
187 :
188 : /// <summary>
189 : /// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index.
190 : /// </summary>
191 : /// <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>
192 1 : public void RemoveAt(int index)
193 : {
194 : Remove(Source.Values.ToList()[index]);
195 : }
196 :
197 : /// <summary>
198 : /// Gets or sets the element at the specified index.
199 : /// </summary>
200 : /// <returns>
201 : /// The element at the specified index.
202 : /// </returns>
203 : /// <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>
204 : public TEntity this[int index]
205 : {
206 : get { return Source.Values.ToList()[index]; }
207 : set { Source[this[index].Rsn] = value; }
208 : }
209 :
210 : #endregion
211 : }
212 : }
213 : }
|