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.Entities;
15 : using Cqrs.Repositories;
16 :
17 : namespace Cqrs.DataStores
18 : {
19 : /// <summary>
20 : /// A <see cref="IDataStore{TData}"/> using an <see cref="InMemoryDatabase"/>.
21 : /// </summary>
22 : public class InProcessDataStore<TData> : IDataStore<TData>
23 : where TData : Entity
24 1 : {
25 : private InMemoryDatabase InMemoryDatabase { get; set; }
26 :
27 : /// <summary>
28 : /// Instantiates a new instance of the <see cref="InProcessDataStore{TData}"/> class
29 : /// </summary>
30 1 : public InProcessDataStore()
31 : {
32 : InMemoryDatabase = new InMemoryDatabase();
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 1 : public IEnumerator<TData> GetEnumerator()
44 : {
45 : return InMemoryDatabase.GetAll<TData>().GetEnumerator();
46 : }
47 :
48 : /// <summary>
49 : /// Returns an enumerator that iterates through a collection.
50 : /// </summary>
51 : /// <returns>
52 : /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
53 : /// </returns>
54 : IEnumerator IEnumerable.GetEnumerator()
55 : {
56 : return GetEnumerator();
57 : }
58 :
59 : #endregion
60 :
61 : #region Implementation of IQueryable
62 :
63 : /// <summary>
64 : /// Gets the expression tree that is associated with the instance of <see cref="T:System.Linq.IQueryable"/>.
65 : /// </summary>
66 : /// <returns>
67 : /// The <see cref="T:System.Linq.Expressions.Expression"/> that is associated with this instance of <see cref="T:System.Linq.IQueryable"/>.
68 : /// </returns>
69 : public Expression Expression
70 : {
71 : get { return InMemoryDatabase.GetAll<TData>().AsQueryable().Expression; }
72 : }
73 :
74 : /// <summary>
75 : /// 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.
76 : /// </summary>
77 : /// <returns>
78 : /// 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.
79 : /// </returns>
80 : public Type ElementType
81 : {
82 : get { return InMemoryDatabase.GetAll<TData>().AsQueryable().ElementType; }
83 : }
84 :
85 : /// <summary>
86 : /// Gets the singleResultQuery provider that is associated with this data source.
87 : /// </summary>
88 : /// <returns>
89 : /// The <see cref="T:System.Linq.IQueryProvider"/> that is associated with this data source.
90 : /// </returns>
91 : public IQueryProvider Provider
92 : {
93 : get { return InMemoryDatabase.GetAll<TData>().AsQueryable().Provider; }
94 : }
95 :
96 : #endregion
97 :
98 : #region Implementation of IDisposable
99 :
100 : /// <summary>
101 : /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
102 : /// </summary>
103 1 : public void Dispose()
104 : {
105 : }
106 :
107 : #endregion
108 :
109 : #region Implementation of IDataStore<TData>
110 :
111 : /// <summary>
112 : /// Add the provided <paramref name="data"/> to the data store and persist the change.
113 : /// </summary>
114 1 : public void Add(TData data)
115 : {
116 : InMemoryDatabase.Get<TData>().Add(data.Rsn, data);
117 : }
118 :
119 : /// <summary>
120 : /// Add the provided <paramref name="data"/> to the data store and persist the change.
121 : /// </summary>
122 1 : public void Add(IEnumerable<TData> data)
123 : {
124 : foreach (TData dataItem in data)
125 : Add(dataItem);
126 : }
127 :
128 : /// <summary>
129 : /// Will mark the <paramref name="data"/> as logically (or soft) deleted by setting <see cref="Entity.IsLogicallyDeleted"/> to true in the data store and persist the change.
130 : /// </summary>
131 1 : public void Remove(TData data)
132 : {
133 : InMemoryDatabase.Get<TData>()[data.Rsn].IsLogicallyDeleted = true;
134 : }
135 :
136 : /// <summary>
137 : /// Remove the provided <paramref name="data"/> (normally by <see cref="IEntity.Rsn"/>) from the data store and persist the change.
138 : /// </summary>
139 1 : public void Destroy(TData data)
140 : {
141 : InMemoryDatabase.Get<TData>().Remove(data.Rsn);
142 : }
143 :
144 : /// <summary>
145 : /// Remove all contents (normally by use of a truncate operation) from the data store and persist the change.
146 : /// </summary>
147 1 : public void RemoveAll()
148 : {
149 : InMemoryDatabase.Get<TData>().Clear();
150 : }
151 :
152 : /// <summary>
153 : /// Update the provided <paramref name="data"/> in the data store and persist the change.
154 : /// </summary>
155 1 : public void Update(TData data)
156 : {
157 : InMemoryDatabase.Get<TData>()[data.Rsn] = data;
158 : }
159 :
160 : #endregion
161 : }
162 : }
|