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 System.Reflection;
12 : using Cqrs.Akka.Commands;
13 : using Cqrs.Akka.Domain;
14 : using Cqrs.Akka.Events;
15 : using Cqrs.Akka.Snapshots;
16 : using Cqrs.Bus;
17 : using Cqrs.Configuration;
18 : using Cqrs.Ninject.Configuration;
19 : using Cqrs.Snapshots;
20 : using Ninject.Modules;
21 :
22 : namespace Cqrs.Ninject.Akka.Configuration
23 : {
24 : /// <summary>
25 : /// A <see cref="INinjectModule"/> that wires up many of the prerequisites for running CQRS.NET with Akka.NET
26 : /// </summary>
27 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
28 : public class AkkaModule<TAuthenticationToken> : ResolvableModule
29 1 : {
30 : #region Overrides of NinjectModule
31 :
32 : /// <summary>
33 : /// Loads the module into the kernel.
34 : /// </summary>
35 1 : public override void Load()
36 : {
37 : Bind<IAkkaAggregateRepository<TAuthenticationToken>>().To<AkkaAggregateRepository<TAuthenticationToken>>().InSingletonScope();
38 : Bind<IAkkaSagaRepository<TAuthenticationToken>>().To<AkkaSagaRepository<TAuthenticationToken>>().InSingletonScope();
39 : Bind<IAkkaEventPublisher<TAuthenticationToken>>().To<AkkaEventBus<TAuthenticationToken>>().InSingletonScope();
40 : Bind<IAkkaEventPublisherProxy<TAuthenticationToken>>().To<AkkaEventBusProxy<TAuthenticationToken>>().InSingletonScope();
41 : Bind<IAkkaCommandPublisher<TAuthenticationToken>>().To<AkkaCommandBus<TAuthenticationToken>>().InSingletonScope();
42 : Bind<IAkkaCommandPublisherProxy<TAuthenticationToken>>().To<AkkaCommandBusProxy<TAuthenticationToken>>().InSingletonScope();
43 :
44 : Bind<IAkkaSnapshotAggregateRepository<TAuthenticationToken>>().To<AkkaSnapshotRepository<TAuthenticationToken>>().InSingletonScope();
45 : Rebind<ISnapshotStrategy<TAuthenticationToken>>().To<DefaultAkkaSnapshotStrategy<TAuthenticationToken>>().InSingletonScope();
46 :
47 : BusRegistrar.GetEventHandlerRegistrar = (messageType, handlerDelegateTargetedType) =>
48 : {
49 : bool isAnActor = messageType != null && messageType.GetNestedTypes().Any(type => type.Name == "Actor");
50 : IEventHandlerRegistrar eventHandlerRegistrar = null;
51 : if (isAnActor)
52 : eventHandlerRegistrar = Resolve<IAkkaEventPublisher<TAuthenticationToken>>() as IEventHandlerRegistrar;
53 : return eventHandlerRegistrar ?? Resolve<IEventHandlerRegistrar>();
54 : };
55 :
56 : BusRegistrar.GetCommandHandlerRegistrar = (messageType, handlerDelegateTargetedType) =>
57 : {
58 : bool isAnActor = handlerDelegateTargetedType != null && handlerDelegateTargetedType.GetProperty("AggregateResolver", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public) != null;
59 : ICommandHandlerRegistrar commandHandlerRegistrar = null;
60 : if (isAnActor)
61 : commandHandlerRegistrar = Resolve<IAkkaCommandPublisher<TAuthenticationToken>>() as ICommandHandlerRegistrar;
62 : return commandHandlerRegistrar ?? Resolve<ICommandHandlerRegistrar>();
63 : };
64 : }
65 :
66 : #endregion
67 : }
68 : }
|