Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="cdmdotnet Limited">
4 : // // Copyright cdmdotnet 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 Ninject.Modules;
18 : using Ninject.Parameters;
19 :
20 : namespace Cqrs.Ninject.Akka.Configuration
21 : {
22 : public class AkkaModule<TAuthenticationToken> : NinjectModule
23 0 : {
24 : #region Overrides of NinjectModule
25 :
26 : /// <summary>
27 : /// Loads the module into the kernel.
28 : /// </summary>
29 1 : public override void Load()
30 : {
31 : Bind<IAkkaAggregateRepository<TAuthenticationToken>>().To<AkkaAggregateRepository<TAuthenticationToken>>().InSingletonScope();
32 : Bind<IAkkaSagaRepository<TAuthenticationToken>>().To<AkkaSagaRepository<TAuthenticationToken>>().InSingletonScope();
33 : Bind<IAkkaEventPublisher<TAuthenticationToken>>().To<AkkaEventBus<TAuthenticationToken>>().InSingletonScope();
34 : Bind<IAkkaEventPublisherProxy<TAuthenticationToken>>().To<AkkaEventBusProxy<TAuthenticationToken>>().InSingletonScope();
35 : Bind<IAkkaCommandSender<TAuthenticationToken>>().To<AkkaCommandBus<TAuthenticationToken>>().InSingletonScope();
36 : Bind<IAkkaCommandSenderProxy<TAuthenticationToken>>().To<AkkaCommandBusProxy<TAuthenticationToken>>().InSingletonScope();
37 :
38 : BusRegistrar.GetEventHandlerRegistrar = (messageType, handlerDelegateTargetedType) =>
39 : {
40 : bool isAnActor = messageType != null && messageType.GetNestedTypes().Any(type => type.Name == "Actor");
41 : IEventHandlerRegistrar eventHandlerRegistrar = null;
42 : if (isAnActor)
43 : eventHandlerRegistrar = Resolve<IAkkaEventPublisher<TAuthenticationToken>>() as IEventHandlerRegistrar;
44 : return eventHandlerRegistrar ?? Resolve<IEventHandlerRegistrar>();
45 : };
46 :
47 : BusRegistrar.GetCommandHandlerRegistrar = (messageType, handlerDelegateTargetedType) =>
48 : {
49 : bool isAnActor = handlerDelegateTargetedType != null && handlerDelegateTargetedType.GetProperty("AggregateResolver", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public) != null;
50 : ICommandHandlerRegistrar commandHandlerRegistrar = null;
51 : if (isAnActor)
52 : commandHandlerRegistrar = Resolve<IAkkaCommandSender<TAuthenticationToken>>() as ICommandHandlerRegistrar;
53 : return commandHandlerRegistrar ?? Resolve<ICommandHandlerRegistrar>();
54 : };
55 : }
56 :
57 : #endregion
58 :
59 0 : protected T Resolve<T>()
60 : {
61 : return (T)Resolve(typeof(T));
62 : }
63 :
64 0 : protected object Resolve(Type serviceType)
65 : {
66 : return Kernel.Resolve(Kernel.CreateRequest(serviceType, null, new Parameter[0], true, true)).SingleOrDefault();
67 : }
68 : }
69 : }
|