@@ -162,43 +162,48 @@ The library is built with DI as a first class concept. Wiring it up is easy. Thi
162
162
public static ServiceProvider GetContainer() {
163
163
var services = new ServiceCollection();
164
164
165
- // Wiring up the event storage provider. "https://eventstore.com/" in this example
165
+ // >> Step 1: Register the storage provider
166
166
167
- // Event Store specific settings etc
168
- services.AddScoped<IEventStoreSettings, EventStoreSettings>(
169
- (sp) => new EventStoreSettings(SnapshotFrequency, PageSize));
170
- services.AddScoped<IEventStoreStorageConnectionProvider, EventStoreStorageConnectionProvider>();
171
- services.AddScoped<IEventStoreStorageCore, EventStoreStorageCore>();
167
+ // Wiring up the event storage provider. "https://eventstore.com/" in this example
168
+ // Event Store specific settings etc
169
+ services.AddScoped<IEventStoreSettings, EventStoreSettings>(
170
+ (sp) => new EventStoreSettings(SnapshotFrequency, PageSize));
171
+ services.AddScoped<IEventStoreStorageConnectionProvider, EventStoreStorageConnectionProvider>();
172
+ services.AddScoped<IEventStoreStorageCore, EventStoreStorageCore>();
172
173
173
- // The storage provider implementations
174
- services.AddScoped<IEventStorageProvider<Guid>, EventStoreEventStorageProvider>();
175
- services.AddScoped<ISnapshotStorageProvider<Guid>, EventStoreSnapshotStorageProvider>();
174
+ // The storage provider implementations
175
+ services.AddScoped<IEventStorageProvider<Guid>, EventStoreEventStorageProvider>();
176
+ services.AddScoped<ISnapshotStorageProvider<Guid>, EventStoreSnapshotStorageProvider>();
176
177
177
- // Register the repository
178
- services.AddScoped<IRepository<Schedule, Guid, Guid>, Repository<Schedule, ScheduleSnapshot>>();
179
- // If you prefer to work without snapshots and use events only repository
180
- // services.AddScoped<IRepository<Schedule, Guid, Guid>, EventOnlyRepository<Schedule>>();
178
+ // >> Step 2: Register the Repository and Session
181
179
182
- // register the session implementation for the Aggregate
183
- services.AddScoped<ISession<Schedule>, Session<Schedule>>();
184
- // or if prefer to you use the more detailed interface
185
- // services.AddScoped<ISession<Schedule, Guid, Guid>, Session<Schedule>>();
180
+ // Use the extension method "ScanAndRegisterAggregates()" in the
181
+ // "NEventLite.Extensions.Microsoft.DependencyInjection" nuget library as shown below
186
182
187
- // Or
188
- // Instead of specifying each Aggregate and Snapshot type you can use the convenience
189
- // extension method "ScanAndRegisterAggregates()" in the
190
- // "NEventLite.Extensions.Microsoft.DependencyInjection" nuget library as shown below
183
+ services.ScanAndRegisterAggregates();
191
184
192
- // services.ScanAndRegisterAggregates();
185
+ // Or if you prefer to register the manually
193
186
194
- // Use the defaults
195
- services.AddSingleton<IClock, DefaultSystemClock>();
196
- services.AddSingleton<IEventPublisher, DefaultNoOpEventPublisher>();
187
+ // Register the repository
188
+ services.AddScoped<IRepository<Schedule, Guid, Guid>, Repository<Schedule, ScheduleSnapshot, Guid, Guid, Guid>>();
189
+ // If you prefer to work without snapshots and use events only repository
190
+ // services.AddScoped<IRepository<Schedule, Guid, Guid>, EventOnlyRepository<Schedule>>();
197
191
198
- // Or
199
- // use your own implementation
200
- // services.AddSingleton<IClock, MyClock>();
201
- // services.AddSingleton<IEventPublisher, MyEventPublisher>();
192
+ // register the session implementation for the Aggregate
193
+ services.AddScoped<ISession<Schedule>, Session<Schedule>>();
194
+ // or if prefer to you use the more detailed interface
195
+ // services.AddScoped<ISession<Schedule, Guid, Guid>, Session<Schedule>>();
196
+
197
+ // >> Step 3: Register the other required dependencies
198
+
199
+ // Use the defaults
200
+ services.AddSingleton<IClock, DefaultSystemClock>();
201
+ services.AddSingleton<IEventPublisher, DefaultNoOpEventPublisher>();
202
+
203
+ // Or
204
+ // use your own implementation
205
+ // services.AddSingleton<IClock, MyClock>();
206
+ // services.AddSingleton<IEventPublisher, MyEventPublisher>();
202
207
203
208
var container = services.BuildServiceProvider();
204
209
return container;
@@ -208,60 +213,33 @@ The library is built with DI as a first class concept. Wiring it up is easy. Thi
208
213
If you want to use it with a different dependency injection framework, you can look at how the assembly scanning and registration is implemented for ` Microsoft.Extensions.DependencyInjection ` as an example and come up with your own implementation. The file is [ located here] ( https://github.com/dasiths/NEventLite/blob/master/src/Extensions/NEventLite.Extensions.Microsoft.DependencyInjection/Extensions.cs ) .
209
214
210
215
``` csharp
211
- public static void ScanAndRegisterAggregates <TAggregateKey , TEventKey >(this ServiceCollection services , IList < Assembly > assemblies )
216
+ public static void ScanAndRegisterAggregates (this ServiceCollection services , IList < Assembly > assemblies )
217
+ {
218
+ foreach (var a in assemblies .GetAllAggregates ()) // Use the built in GetAllAggregates() extensions method to find aggregate information
212
219
{
213
- var allAggregates = assemblies
214
- .SelectMany (a =>
215
- a .GetTypes ()
216
- .Where (t => t .IsClass && ! t .IsAbstract & typeof (AggregateRoot <TAggregateKey , TEventKey >).IsAssignableFrom (t )))
217
- .ToList ();
218
-
219
-
220
- foreach (var aggregateType in allAggregates )
221
- {
222
- services .RegisterAggregate <TAggregateKey , TEventKey >(aggregateType );
223
- }
220
+ services .RegisterAggregate (a );
224
221
}
222
+ }
225
223
226
- public static void RegisterAggregate <TAggregateKey , TEventKey >(this ServiceCollection services , Type aggregateType )
224
+ public static void RegisterAggregate (this ServiceCollection services , AggregateInformation a )
225
+ {
226
+ // Register full generic types
227
+ services .AddScoped (typeof (IRepository <,,>).MakeGenericType (a .Aggregate , a .AggregateKey , a .EventKey ),
228
+ a .Snapshot != null
229
+ ? typeof (Repository <,,,,>).MakeGenericType (a .Aggregate , a .Snapshot , a .AggregateKey ,
230
+ a .EventKey , a .SnapshotKey )
231
+ : typeof (EventOnlyRepository <,,>).MakeGenericType (a .Aggregate , a .AggregateKey , a .EventKey ));
232
+
233
+ services .AddScoped (typeof (ISession <,,>).MakeGenericType (a .Aggregate , a .AggregateKey , a .EventKey ),
234
+ typeof (Session <,,>).MakeGenericType (a .Aggregate , a .AggregateKey , a .EventKey ));
235
+
236
+ // Register the convenience GUID scoped ISession interface as well
237
+ if (a .AggregateKey == typeof (Guid ) && a .EventKey == typeof (Guid ))
227
238
{
228
- var snapshottableSimple = aggregateType .GetInterfaces ().FirstOrDefault (i =>
229
- i .IsGenericType && (i .GetGenericTypeDefinition () == typeof (ISnapshottable <>)));
230
-
231
- var snapshottableComplex = aggregateType .GetInterfaces ().FirstOrDefault (i =>
232
- i .IsGenericType && (i .GetGenericTypeDefinition () == typeof (ISnapshottable <,,>)));
233
-
234
- Type snapshotType = null ;
235
- Type snapshotKeyType = null ;
236
-
237
- if (snapshottableSimple != null )
238
- {
239
- snapshotType = snapshottableSimple .GetGenericArguments ()[0 ];
240
- snapshotKeyType = typeof (Guid );
241
- }
242
- else if (snapshottableComplex != null )
243
- {
244
- snapshotType = snapshottableComplex .GetGenericArguments ()[0 ];
245
- snapshotKeyType = snapshottableComplex .GetGenericArguments ()[2 ];
246
- }
247
-
248
- // Register full generic types
249
- services .AddScoped (typeof (IRepository <,,>).MakeGenericType (aggregateType , typeof (TAggregateKey ), typeof (TEventKey )),
250
- snapshotType != null
251
- ? typeof (Repository <,,,,>).MakeGenericType (aggregateType , snapshotType , typeof (TAggregateKey ),
252
- typeof (TEventKey ), snapshotKeyType )
253
- : typeof (EventOnlyRepository <,,>).MakeGenericType (aggregateType , typeof (TAggregateKey ), typeof (TEventKey )));
254
-
255
- services .AddScoped (typeof (ISession <,,>).MakeGenericType (aggregateType , typeof (TAggregateKey ), typeof (TEventKey )),
256
- typeof (Session <,,>).MakeGenericType (aggregateType , typeof (TAggregateKey ), typeof (TEventKey )));
257
-
258
- // Register the convenience GUID scoped ISession interface as well
259
- if (typeof (TAggregateKey ) == typeof (Guid ) && typeof (TEventKey ) == typeof (Guid ))
260
- {
261
- services .AddScoped (typeof (ISession <>).MakeGenericType (aggregateType ),
262
- typeof (Session <>).MakeGenericType (aggregateType ));
263
- }
239
+ services .AddScoped (typeof (ISession <>).MakeGenericType (a .Aggregate ),
240
+ typeof (Session <>).MakeGenericType (a .Aggregate ));
264
241
}
242
+ }
265
243
```
266
244
267
245
## :ledger : Storage providers
0 commit comments