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.Generic;
11 : using System.Reflection;
12 : using cdmdotnet.Logging;
13 : using cdmdotnet.Logging.Configuration;
14 : using cdmdotnet.StateManagement;
15 : using cdmdotnet.StateManagement.Threaded;
16 : using Cqrs.Authentication;
17 : using Cqrs.Bus;
18 : using Cqrs.Commands;
19 : using Cqrs.Domain;
20 : using Cqrs.Domain.Factories;
21 : using Cqrs.Events;
22 : using Cqrs.Repositories.Queries;
23 : using Cqrs.Snapshots;
24 :
25 : namespace Cqrs.Configuration
26 : {
27 : /// <summary>
28 : /// A sample runtime to use in proof of concept projects to get something running very quickly. Doesn't save anything. All data is lost when recycled and may cause terrible memory usage.
29 : /// </summary>
30 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of authentication token.</typeparam>
31 : /// <typeparam name="TCommandHanderOrEventHandler">The <see cref="Type"/> of any <see cref="ICommandHandle"/> or <see cref="IEventHandler"/>.</typeparam>
32 : public class SampleRuntime<TAuthenticationToken, TCommandHanderOrEventHandler>
33 : : IDisposable
34 1 : {
35 : /// <summary>
36 : /// The <see cref="Func{TResult}"/> used to create the <see cref="IEventStore{TAuthenticationToken}"/>
37 : /// </summary>
38 : protected static Func<IDependencyResolver, IEventStore<TAuthenticationToken>> EventStoreCreator { get; set; }
39 :
40 : /// <summary>
41 : /// A custom dependency resolver.
42 : /// </summary>
43 : public static Func<IDependencyResolver, Type, object> CustomResolver { get; set; }
44 :
45 : /// <summary>
46 : /// Instaiance a new instance of the <see cref="SampleRuntime{TAuthenticationToken,TCommandHanderOrEventHandler}"/>
47 : /// </summary>
48 1 : public SampleRuntime()
49 : {
50 : SetEventStoreCreator();
51 : StartDependencyResolver();
52 : RegisterHandlers();
53 : }
54 :
55 : /// <summary>
56 : /// Sets the <see cref="EventStoreCreator"/> to use <see cref="InProcessEventStore{TAuthenticationToken}"/>
57 : /// </summary>
58 1 : protected virtual void SetEventStoreCreator()
59 : {
60 : EventStoreCreator = dependencyResolver => new InProcessEventStore<TAuthenticationToken>();
61 : }
62 :
63 : /// <summary>
64 : /// Starts the <see cref="IDependencyResolver"/>.
65 : /// </summary>
66 1 : protected virtual void StartDependencyResolver()
67 : {
68 : MockDependencyResolver.Start();
69 : }
70 :
71 : /// <summary>
72 : /// Registers the all <see cref="IEventHandler"/> and <see cref="ICommandHandle"/>.
73 : /// </summary>
74 1 : protected virtual void RegisterHandlers()
75 : {
76 : new BusRegistrar(DependencyResolver.Current)
77 : .Register(typeof(TCommandHanderOrEventHandler));
78 : }
79 :
80 : /// <summary>
81 : /// Prints out the statistics of this run such as the number of event raised to the <see cref="Console"/>.
82 : /// </summary>
83 1 : public virtual void PrintStatsticsToConsole()
84 : {
85 : var inProcStore = DependencyResolver.Current.Resolve<IEventStore<TAuthenticationToken>>() as InProcessEventStore<TAuthenticationToken>;
86 : if (inProcStore != null)
87 : {
88 : var inMemoryDb = typeof(InProcessEventStore<TAuthenticationToken>).GetProperty("InMemoryDb", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(inProcStore, null) as IDictionary<Guid, IList<IEvent<TAuthenticationToken>>>;
89 : Console.WriteLine("{0:N0} event{1} {2} raised.", inMemoryDb.Count, inMemoryDb.Count == 1 ? null : "s", inMemoryDb.Count == 1 ? "was" : "were");
90 : }
91 : }
92 :
93 : #region Implementation of IDisposable
94 :
95 : /// <summary>
96 : /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
97 : /// </summary>
98 1 : public void Dispose()
99 : {
100 : var mockDependencyResolver = (MockDependencyResolver)DependencyResolver.Current;
101 : mockDependencyResolver.Dispose();
102 : }
103 :
104 : #endregion
105 :
106 : /// <summary>
107 : /// Provides an ability to resolve a minimum known set of objects.
108 : /// </summary>
109 : protected class MockDependencyResolver
110 : : DependencyResolver
111 : , IDisposable
112 1 : {
113 : private IContextItemCollectionFactory ContextFactory { get; set; }
114 :
115 : private ICorrelationIdHelper CorrelationIdHelper { get; set; }
116 :
117 : private IConfigurationManager ConfigurationManager { get; set; }
118 :
119 : private ILogger Logger { get; set; }
120 :
121 : private IEventStore<TAuthenticationToken> EventStore { get; set; }
122 :
123 : private InProcessBus<TAuthenticationToken> Bus { get; set; }
124 :
125 : private IAggregateRepository<TAuthenticationToken> AggregateRepository { get; set; }
126 :
127 : private ISnapshotAggregateRepository<TAuthenticationToken> SnapshotAggregateRepository { get; set; }
128 :
129 : static MockDependencyResolver()
130 : {
131 : Current = new MockDependencyResolver();
132 : }
133 :
134 : /// <summary>
135 : /// Starts the <see cref="IDependencyResolver"/>.
136 : /// </summary>
137 1 : public static void Start() { }
138 :
139 : private MockDependencyResolver()
140 : {
141 : ContextFactory = new ThreadedContextItemCollectionFactory();
142 : CorrelationIdHelper = new CorrelationIdHelper((ThreadedContextItemCollectionFactory)ContextFactory);
143 : ConfigurationManager = new ConfigurationManager();
144 : Logger = new TraceLogger(new LoggerSettings(), CorrelationIdHelper);
145 : EventStore = EventStoreCreator(this);
146 :
147 : Bus = new InProcessBus<TAuthenticationToken>
148 : (
149 : (IAuthenticationTokenHelper<TAuthenticationToken>)new DefaultAuthenticationTokenHelper(ContextFactory),
150 : CorrelationIdHelper,
151 : this,
152 : Logger,
153 : ConfigurationManager,
154 : new BusHelper(ConfigurationManager, ContextFactory)
155 : );
156 :
157 : AggregateRepository = new AggregateRepository<TAuthenticationToken>
158 : (
159 : new AggregateFactory(this, Logger),
160 : EventStore,
161 : Bus,
162 : CorrelationIdHelper,
163 : ConfigurationManager
164 : );
165 : SnapshotAggregateRepository = new SnapshotRepository<TAuthenticationToken>
166 : (
167 : new SqlSnapshotStore(ConfigurationManager, new SnapshotDeserialiser(), Logger, CorrelationIdHelper, new DefaultSnapshotBuilder()),
168 : new DefaultSnapshotStrategy<TAuthenticationToken>(),
169 : AggregateRepository,
170 : EventStore,
171 : new AggregateFactory(this, Logger)
172 : );
173 : }
174 :
175 : #region Implementation of IDependencyResolver
176 :
177 : /// <summary>
178 : /// Resolves a single instance for the specified <typeparamref name="T"/>.
179 : /// Different implementations may return the first or last instance found or may return an exception.
180 : /// </summary>
181 : /// <typeparam name="T">The <see cref="T:System.Type"/> of object you want to resolve.</typeparam>
182 : /// <returns>
183 : /// An instance of type <typeparamref name="T"/>.
184 : /// </returns>
185 1 : public override T Resolve<T>()
186 : {
187 : return (T)Resolve(typeof(T));
188 : }
189 :
190 : /// <summary>
191 : /// Resolves a single instance for the specified <paramref name="type"/>.
192 : /// Different implementations may return the first or last instance found or may return an exception.
193 : /// </summary>
194 : /// <param name="type">The <see cref="T:System.Type"/> of object you want to resolve.</param>
195 : /// <returns>
196 : /// An instance of type <paramref name="type"/>.
197 : /// </returns>
198 1 : public override object Resolve(Type type)
199 : {
200 : if (type == typeof(IContextItemCollectionFactory))
201 : return ContextFactory;
202 : if (type == typeof(ICorrelationIdHelper))
203 : return CorrelationIdHelper;
204 : if (type == typeof(IConfigurationManager))
205 : return ConfigurationManager;
206 : if (type == typeof(IEventHandlerRegistrar))
207 : return Bus;
208 : if (type == typeof(ICommandHandlerRegistrar))
209 : return Bus;
210 : if (type == typeof(IEventPublisher<TAuthenticationToken>))
211 : return Bus;
212 : if (type == typeof(ICommandPublisher<TAuthenticationToken>))
213 : return Bus;
214 : if (type == typeof(IEventReceiver<TAuthenticationToken>))
215 : return Bus;
216 : if (type == typeof(ICommandReceiver<TAuthenticationToken>))
217 : return Bus;
218 : if (type == typeof(IEventReceiver))
219 : return Bus;
220 : if (type == typeof(ICommandReceiver))
221 : return Bus;
222 : if (type == typeof(ILogger))
223 : return Logger;
224 : if (type == typeof(IUnitOfWork<TAuthenticationToken>))
225 : return new UnitOfWork<TAuthenticationToken>(SnapshotAggregateRepository, AggregateRepository);
226 : if (type == typeof(IAggregateRepository<TAuthenticationToken>))
227 : return AggregateRepository;
228 : if (type == typeof(IEventStore<TAuthenticationToken>))
229 : return EventStore;
230 : if (type == typeof(IEventBuilder<TAuthenticationToken>))
231 : return new DefaultEventBuilder<TAuthenticationToken>();
232 : if (type == typeof(IEventDeserialiser<TAuthenticationToken>))
233 : return new EventDeserialiser<TAuthenticationToken>();
234 : if (type == typeof(IAuthenticationTokenHelper<TAuthenticationToken>))
235 : return new AuthenticationTokenHelper<TAuthenticationToken>(ContextFactory);
236 : if (type == typeof(IQueryFactory))
237 : return new QueryFactory(this);
238 :
239 : if (typeof(ICommandHandle).IsAssignableFrom(type))
240 : return Activator.CreateInstance(type, Resolve<IUnitOfWork<TAuthenticationToken>>());
241 : if (typeof(IEventHandler).IsAssignableFrom(type))
242 : return Activator.CreateInstance(type);
243 :
244 : if (CustomResolver != null)
245 : {
246 : try
247 : {
248 : object result = CustomResolver(this, type);
249 : if (result != null)
250 : return result;
251 : }
252 : catch { /* */ }
253 : }
254 :
255 : return Activator.CreateInstance(type);
256 : }
257 :
258 : #endregion
259 :
260 : #region Implementation of IDisposable
261 :
262 : /// <summary>
263 : /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
264 : /// </summary>
265 1 : public void Dispose()
266 : {
267 : ContextFactory = null;
268 : CorrelationIdHelper = null;
269 : ConfigurationManager = null;
270 : Logger = null;
271 : EventStore = null;
272 : Bus = null;
273 : AggregateRepository = null;
274 : }
275 :
276 : #endregion
277 : }
278 : }
279 : }
|