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