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 Cqrs.Configuration;
12 : using Cqrs.Events;
13 : using Cqrs.MongoDB.Events;
14 : using Cqrs.MongoDB.Serialisers;
15 : using Cqrs.Snapshots;
16 : using Ninject.Modules;
17 :
18 : namespace Cqrs.Ninject.MongoDB.Configuration
19 : {
20 : /// <summary>
21 : /// A <see cref="INinjectModule"/> that wires up <see cref="MongoDbEventStore{TAuthenticationToken}"/> as the <see cref="IEventStore{TAuthenticationToken}"/>.
22 : /// </summary>
23 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
24 : public class MongoDbEventStoreModule<TAuthenticationToken> : NinjectModule
25 1 : {
26 : /// <summary>
27 : /// Indicates that the <see cref="ISnapshotStrategy{TAuthenticationToken}"/> should be registered automatically.
28 : /// </summary>
29 : protected bool RegisterMongoDbSnapshotDeserialiser { get; private set; }
30 :
31 : /// <summary>
32 : /// Indicates that the <see cref="ISnapshotBuilder"/> should be registered automatically.
33 : /// </summary>
34 : protected bool RegisterMongoDbSnapshotBuilder { get; private set; }
35 :
36 : /// <summary>
37 : /// Instantiate a new instance of the <see cref="MongoDbEventStoreModule{TAuthenticationToken}"/> that uses the provided <paramref name="configurationManager"/>
38 : /// to read the following configuration settings:
39 : /// "Cqrs.RegisterMongoDbSnapshotDeserialiser": If set true the <see cref="MongoDbSnapshotDeserialiser"/> will be registered.
40 : /// "Cqrs.RegisterMongoDbSnapshotBuilder": If set true the <see cref="MongoDbSnapshotBuilder"/> will be registered.
41 : /// </summary>
42 : /// <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>
43 1 : public MongoDbEventStoreModule(IConfigurationManager configurationManager = null)
44 : {
45 : configurationManager = configurationManager ?? new ConfigurationManager();
46 : bool registerMongoDbSnapshotDeserialiser;
47 : if (configurationManager.TryGetSetting("Cqrs.RegisterMongoDbSnapshotDeserialiser", out registerMongoDbSnapshotDeserialiser))
48 : RegisterMongoDbSnapshotDeserialiser = registerMongoDbSnapshotDeserialiser;
49 : else
50 : RegisterMongoDbSnapshotDeserialiser = true;
51 : bool registerMongoDbSnapshotBuilder;
52 : if (configurationManager.TryGetSetting("Cqrs.RegisterMongoDbSnapshotBuilder", out registerMongoDbSnapshotBuilder))
53 : RegisterMongoDbSnapshotBuilder = registerMongoDbSnapshotBuilder;
54 : else
55 : RegisterMongoDbSnapshotBuilder = true;
56 : }
57 :
58 : #region Overrides of NinjectModule
59 :
60 : /// <summary>
61 : /// Loads the module into the kernel.
62 : /// </summary>
63 1 : public override void Load()
64 : {
65 : RegisterFactories();
66 : RegisterEventSerialisationConfiguration();
67 : RegisterEventStore();
68 : }
69 :
70 : #endregion
71 :
72 : /// <summary>
73 : /// Register the all factories
74 : /// </summary>
75 1 : public virtual void RegisterFactories()
76 : {
77 : Bind<IMongoDbEventStoreConnectionStringFactory>()
78 : .To<MongoDbEventStoreConnectionStringFactory>()
79 : .InSingletonScope();
80 : Bind<IMongoDbSnapshotStoreConnectionStringFactory>()
81 : .To<MongoDbSnapshotStoreConnectionStringFactory>()
82 : .InSingletonScope();
83 : }
84 :
85 : /// <summary>
86 : /// Register the all event serialisation configurations
87 : /// </summary>
88 1 : public virtual void RegisterEventSerialisationConfiguration()
89 : {
90 : Bind<IEventBuilder<TAuthenticationToken>>()
91 : .To<MongoDbEventBuilder<TAuthenticationToken>>()
92 : .InSingletonScope();
93 : Bind<IEventDeserialiser<TAuthenticationToken>>()
94 : .To<MongoDbEventDeserialiser<TAuthenticationToken>>()
95 : .InSingletonScope();
96 : Bind<ISnapshotDeserialiser>()
97 : .To<MongoDbSnapshotDeserialiser>()
98 : .InSingletonScope();
99 :
100 : if (Kernel.GetBindings(typeof(ISnapshotBuilder)).Any())
101 : Kernel.Unbind<ISnapshotBuilder>();
102 : Bind<ISnapshotBuilder>()
103 : .To<MongoDbSnapshotBuilder>()
104 : .InSingletonScope();
105 : }
106 :
107 : /// <summary>
108 : /// Register the <see cref="IEventStore{TAuthenticationToken}"/>
109 : /// </summary>
110 1 : public virtual void RegisterEventStore()
111 : {
112 : Bind<IEventStore<TAuthenticationToken>>()
113 : .To<MongoDbEventStore<TAuthenticationToken>>()
114 : .InSingletonScope();
115 : Bind<ISnapshotStore>()
116 : .To<MongoDbSnapshotStore>()
117 : .InSingletonScope();
118 : }
119 :
120 : /// <summary>
121 : /// Register the all Cqrs requirements
122 : /// </summary>
123 1 : public virtual void RegisterCqrsRequirements()
124 : {
125 : if (RegisterMongoDbSnapshotDeserialiser)
126 : Rebind<ISnapshotDeserialiser>()
127 : .To<MongoDbSnapshotDeserialiser>()
128 : .InSingletonScope();
129 :
130 : if (RegisterMongoDbSnapshotBuilder)
131 : Rebind<ISnapshotBuilder>()
132 : .To<MongoDbSnapshotBuilder>()
133 : .InSingletonScope();
134 : }
135 : }
136 : }
|