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