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<IAkkaRepository<TAuthenticationToken>>().To<AkkaRepository<TAuthenticationToken>>().InSingletonScope();
32 : Bind<IAkkaEventPublisher<TAuthenticationToken>>().To<AkkaEventBus<TAuthenticationToken>>().InSingletonScope();
33 : Bind<IAkkaEventPublisherProxy<TAuthenticationToken>>().To<AkkaEventBusProxy<TAuthenticationToken>>().InSingletonScope();
34 : Bind<IAkkaCommandSender<TAuthenticationToken>>().To<AkkaCommandBus<TAuthenticationToken>>().InSingletonScope();
35 : Bind<IAkkaCommandSenderProxy<TAuthenticationToken>>().To<AkkaCommandBusProxy<TAuthenticationToken>>().InSingletonScope();
36 :
37 : BusRegistrar.GetEventHandlerRegistrar = (messageType, handlerDelegateTargetedType) =>
38 : {
39 : bool isAnActor = messageType != null && messageType.GetNestedTypes().Any(type => type.Name == "Actor");
40 : IEventHandlerRegistrar eventHandlerRegistrar = null;
41 : if (isAnActor)
42 : eventHandlerRegistrar = Resolve<IAkkaEventPublisher<TAuthenticationToken>>() as IEventHandlerRegistrar;
43 : return eventHandlerRegistrar ?? Resolve<IEventHandlerRegistrar>();
44 : };
45 :
46 : BusRegistrar.GetCommandHandlerRegistrar = (messageType, handlerDelegateTargetedType) =>
47 : {
48 : bool isAnActor = handlerDelegateTargetedType != null && handlerDelegateTargetedType.GetProperty("AggregateResolver", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public) != null;
49 : ICommandHandlerRegistrar commandHandlerRegistrar = null;
50 : if (isAnActor)
51 : commandHandlerRegistrar = Resolve<IAkkaCommandSender<TAuthenticationToken>>() as ICommandHandlerRegistrar;
52 : return commandHandlerRegistrar ?? Resolve<ICommandHandlerRegistrar>();
53 : };
54 : }
55 :
56 : #endregion
57 :
58 0 : protected T Resolve<T>()
59 : {
60 : return (T)Resolve(typeof(T));
61 : }
62 :
63 0 : protected object Resolve(Type serviceType)
64 : {
65 : return Kernel.Resolve(Kernel.CreateRequest(serviceType, null, new Parameter[0], true, true)).SingleOrDefault();
66 : }
67 : }
68 : }
|