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.Linq;
11 : using cdmdotnet.AutoMapper;
12 : using Cqrs.Authentication;
13 : using Cqrs.Bus;
14 : using Cqrs.Domain;
15 : using Cqrs.Domain.Factories;
16 : using cdmdotnet.Logging;
17 : using cdmdotnet.Logging.Configuration;
18 : using cdmdotnet.StateManagement;
19 : using cdmdotnet.StateManagement.Threaded;
20 : using cdmdotnet.StateManagement.Web;
21 : using Cqrs.Configuration;
22 : using Cqrs.Repositories.Queries;
23 : using Ninject.Modules;
24 :
25 : namespace Cqrs.Ninject.Configuration
26 : {
27 : /// <summary>
28 : /// The main <see cref="INinjectModule"/> for use with the CQRS package that wires up many of the prerequisites for running CQRS.NET.
29 : /// </summary>
30 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
31 : /// <typeparam name="TAuthenticationTokenHelper">The <see cref="Type"/> of the authentication token helper.</typeparam>
32 : public class CqrsModule<TAuthenticationToken, TAuthenticationTokenHelper> : ResolvableModule
33 : where TAuthenticationTokenHelper : class, IAuthenticationTokenHelper<TAuthenticationToken>
34 1 : {
35 : /// <summary>
36 : /// Indicates that web based wire-up is required rather than console, WPF or winforms based wire-up.s
37 : /// </summary>
38 : protected bool SetupForWeb { get; private set; }
39 :
40 : /// <summary>
41 : /// Indicates that logging should be configured for SQL Server rather than console.
42 : /// </summary>
43 : protected bool SetupForSqlLogging { get; private set; }
44 :
45 : /// <summary>
46 : /// Indicates that the <see cref="ConfigurationManager"/> should be registered automatically.
47 : /// </summary>
48 : protected bool RegisterDefaultConfigurationManager { get; private set; }
49 :
50 : /// <summary>
51 : /// Instantiate a new instance of the <see cref="CqrsModule{TAuthenticationToken,TAuthenticationTokenHelper}"/> that uses the provided <paramref name="configurationManager"/>
52 : /// to read the following configuration settings:
53 : /// "Cqrs.SetupForWeb": If set to true the system will be configured for hosting in IIS or some other web-server that provides access to System.Web.HttpContext.Current.
54 : /// "Cqrs.SetupForSqlLogging": If set to true the <see cref="SqlLogger"/> will be bootstrapped by default, otherwise the <see cref="ConsoleLogger"/> will be bootstrapped by default.
55 : /// "Cqrs.RegisterDefaultConfigurationManager": If set true the <see cref="ConfigurationManager"/> will be registered. If you want to use the Azure one leave this as false (the default) and register it yourself.
56 : /// </summary>
57 : /// <param name="configurationManager">The <see cref="IConfigurationManager"/> to use, if one isn't provided then <see cref="ConfigurationManager"/> is instantiate, used and then disposed.</param>
58 1 : public CqrsModule(IConfigurationManager configurationManager = null)
59 : {
60 : configurationManager = configurationManager ?? new ConfigurationManager();
61 : bool setupForWeb;
62 : if (configurationManager.TryGetSetting("Cqrs.SetupForWeb", out setupForWeb))
63 : SetupForWeb = setupForWeb;
64 : bool setupForSqlLogging;
65 : if (configurationManager.TryGetSetting("Cqrs.SetupForSqlLogging", out setupForSqlLogging))
66 : SetupForSqlLogging = setupForSqlLogging;
67 : bool registerDefaultConfigurationManager;
68 : if (configurationManager.TryGetSetting("Cqrs.RegisterDefaultConfigurationManager", out registerDefaultConfigurationManager))
69 : RegisterDefaultConfigurationManager = registerDefaultConfigurationManager;
70 : }
71 :
72 : /// <summary>
73 : /// Instantiate a new instance of the <see cref="CqrsModule{TAuthenticationToken,TAuthenticationTokenHelper}"/>.
74 : /// </summary>
75 : /// <param name="setupForWeb">Set this to true if you will host this in IIS or some other web-server that provides access to System.Web.HttpContext.Current.</param>
76 : /// <param name="setupForSqlLogging">Set this to true to use <see cref="SqlLogger"/> otherwise the <see cref="ConsoleLogger"/> will be bootstrapped by default.</param>
77 : /// <param name="registerDefaultConfigurationManager">Set this to true to use <see cref="ConfigurationManager"/>. If you want to use the Azure one leave this as false (the default) and register it yourself.</param>
78 1 : public CqrsModule(bool setupForWeb, bool setupForSqlLogging, bool registerDefaultConfigurationManager = false)
79 : {
80 : SetupForWeb = setupForWeb;
81 : SetupForSqlLogging = setupForSqlLogging;
82 : RegisterDefaultConfigurationManager = registerDefaultConfigurationManager;
83 : }
84 :
85 : #region Overrides of NinjectModule
86 :
87 : /// <summary>
88 : /// Loads the module into the kernel.
89 : /// </summary>
90 1 : public override void Load()
91 : {
92 : RegisterFactories();
93 : RegisterRepositories();
94 : RegisterQueryBuilders();
95 : RegisterServices();
96 : RegisterCqrsRequirements();
97 : RegisterAutomapperComponents();
98 : RegisterLoggerComponents();
99 : RegisterCaching();
100 : }
101 :
102 : #endregion
103 :
104 : /// <summary>
105 : /// Register the all factories
106 : /// </summary>
107 1 : public virtual void RegisterFactories()
108 : {
109 : Bind<IQueryFactory>()
110 : .To<QueryFactory>()
111 : .InSingletonScope();
112 : }
113 :
114 : /// <summary>
115 : /// Register the all components for the <see cref="ILogger"/>
116 : /// </summary>
117 1 : public virtual void RegisterLoggerComponents()
118 : {
119 : bool isCorrelationIdHelperBound = Kernel.GetBindings(typeof(ICorrelationIdHelper)).Any();
120 : if (!isCorrelationIdHelperBound)
121 : {
122 : if (SetupForWeb)
123 : Bind<ICorrelationIdHelper>()
124 : .To<WebCorrelationIdHelper>()
125 : .InSingletonScope();
126 : else
127 : Bind<ICorrelationIdHelper>()
128 : .To<CorrelationIdHelper>()
129 : .InSingletonScope();
130 : }
131 :
132 : bool isLoggerBound = Kernel.GetBindings(typeof(ILogger)).Any();
133 : if (!isLoggerBound)
134 : {
135 : if (SetupForSqlLogging)
136 : Bind<ILogger>()
137 : .To<SqlLogger>()
138 : .InSingletonScope();
139 : else
140 : Bind<ILogger>()
141 : .To<ConsoleLogger>()
142 : .InSingletonScope();
143 : }
144 :
145 : bool isLoggerSettingsBound = Kernel.GetBindings(typeof(ILoggerSettings)).Any();
146 : if (!isLoggerSettingsBound)
147 : {
148 : Bind<ILoggerSettings>()
149 : .To<LoggerSettings>()
150 : .InSingletonScope();
151 : }
152 :
153 : bool isTelemetryHelperBound = Kernel.GetBindings(typeof(ITelemetryHelper)).Any();
154 : if (!isTelemetryHelperBound)
155 : {
156 : Bind<ITelemetryHelper>()
157 : .To<NullTelemetryHelper>()
158 : .InSingletonScope();
159 : }
160 : }
161 :
162 : /// <summary>
163 : /// Register the all <see cref="IAutomapHelper"/>
164 : /// </summary>
165 1 : public virtual void RegisterAutomapperComponents()
166 : {
167 : Bind<IAutomapHelper>()
168 : .To<AutomapHelper>()
169 : .InSingletonScope();
170 : }
171 :
172 : /// <summary>
173 : /// Register the all repositories
174 : /// </summary>
175 1 : public virtual void RegisterRepositories()
176 : {
177 : }
178 :
179 : /// <summary>
180 : /// Register the all query builders
181 : /// </summary>
182 1 : public virtual void RegisterQueryBuilders()
183 : {
184 : }
185 :
186 : /// <summary>
187 : /// Register the all services
188 : /// </summary>
189 1 : public virtual void RegisterServices()
190 : {
191 : }
192 :
193 : /// <summary>
194 : /// Register the all caching stuffs
195 : /// </summary>
196 1 : public virtual void RegisterCaching()
197 : {
198 : if (Kernel.GetBindings(typeof (IContextItemCollectionFactory)).Any())
199 : Kernel.Unbind<IContextItemCollectionFactory>();
200 : if (Kernel.GetBindings(typeof(IContextItemCollection)).Any())
201 : Kernel.Unbind<IContextItemCollection>();
202 : if (SetupForWeb)
203 : {
204 : Bind<IContextItemCollectionFactory>()
205 : .To<WebContextItemCollectionFactory>()
206 : .InSingletonScope();
207 : Bind<IContextItemCollection>()
208 : .To<WebContextItemCollection>()
209 : .InSingletonScope();
210 : }
211 : else
212 : {
213 : Bind<IContextItemCollectionFactory>()
214 : .To<ThreadedContextItemCollectionFactory>()
215 : .InSingletonScope();
216 : Bind<IContextItemCollection>()
217 : .To<ThreadedContextItemCollection>()
218 : .InSingletonScope();
219 : }
220 : }
221 :
222 : /// <summary>
223 : /// Register the all Cqrs requirements
224 : /// </summary>
225 1 : public virtual void RegisterCqrsRequirements()
226 : {
227 : Bind<IUnitOfWork<TAuthenticationToken>>()
228 : .To<UnitOfWork<TAuthenticationToken>>()
229 : .InTransientScope();
230 : Bind<ISagaUnitOfWork<TAuthenticationToken>>()
231 : .To<SagaUnitOfWork<TAuthenticationToken>>()
232 : .InTransientScope();
233 : Bind<IAggregateRepository<TAuthenticationToken>>()
234 : .To<AggregateRepository<TAuthenticationToken>>()
235 : .InSingletonScope();
236 : Bind<ISagaRepository<TAuthenticationToken>>()
237 : .To<SagaRepository<TAuthenticationToken>>()
238 : .InSingletonScope();
239 : Bind<IAggregateFactory>()
240 : .To<AggregateFactory>()
241 : .InSingletonScope();
242 :
243 : Bind<IAuthenticationTokenHelper<TAuthenticationToken>>()
244 : .To<TAuthenticationTokenHelper>()
245 : .InSingletonScope();
246 :
247 : Bind<IStoreLastEventProcessed>()
248 : .To<FileBasedLastEventProcessedStore>()
249 : .InSingletonScope();
250 :
251 : Bind<IBusHelper>()
252 : .To<BusHelper>()
253 : .InSingletonScope();
254 :
255 : if (RegisterDefaultConfigurationManager)
256 : Bind<IConfigurationManager>()
257 : .To<ConfigurationManager>()
258 : .InSingletonScope();
259 : }
260 : }
261 : }
|