Documentation Coverage Report
Current view: top level - Ninject/Azure/Cqrs.Ninject.Azure.ServiceBus.EventBus/Configuration - AzureEventBusReceiverModule.cs Hit Total Coverage
Version: 2.2 Artefacts: 6 6 100.0 %
Date: 2017-09-22

          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 Cqrs.Azure.ServiceBus;
      12             : using Cqrs.Bus;
      13             : using Cqrs.Events;
      14             : using Ninject;
      15             : using Ninject.Modules;
      16             : 
      17             : namespace Cqrs.Ninject.Azure.ServiceBus.EventBus.Configuration
      18             : {
      19             :         /// <summary>
      20             :         /// A <see cref="INinjectModule"/> that wires up <see cref="AzureEventBusReceiver{TAuthenticationToken}"/> as the <see cref="IEventReceiver"/> and other require components.
      21             :         /// </summary>
      22             :         /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
      23             :         public class AzureEventBusReceiverModule<TAuthenticationToken> : NinjectModule
      24           1 :         {
      25             :                 #region Overrides of NinjectModule
      26             : 
      27             :                 /// <summary>
      28             :                 /// Loads the module into the kernel.
      29             :                 /// </summary>
      30           1 :                 public override void Load()
      31             :                 {
      32             :                         bool isMessageSerialiserBound = Kernel.GetBindings(typeof(IAzureBusHelper<TAuthenticationToken>)).Any();
      33             :                         if (!isMessageSerialiserBound)
      34             :                         {
      35             :                                 Bind<IAzureBusHelper<TAuthenticationToken>>()
      36             :                                         .To<AzureBusHelper<TAuthenticationToken>>()
      37             :                                         .InSingletonScope();
      38             :                         }
      39             : 
      40             :                         RegisterEventMessageSerialiser();
      41             :                         var bus = GetOrCreateBus<AzureEventBusReceiver<TAuthenticationToken>>();
      42             : 
      43             :                         RegisterEventReceiver(bus);
      44             :                         RegisterEventHandlerRegistrar(bus);
      45             :                 }
      46             : 
      47             :                 #endregion
      48             : 
      49             :                 /// <summary>
      50             :                 /// Checks if an existing <typeparamref name="TBus"/> has already been registered, if not
      51             :                 /// it tries to instantiates a new instance via resolution and registers that instance.
      52             :                 /// </summary>
      53             :                 /// <typeparam name="TBus">The <see cref="Type"/> of bus to resolve. Best if a class not an interface.</typeparam>
      54           1 :                 public virtual TBus GetOrCreateBus<TBus>()
      55             :                         where TBus : IEventReceiver<TAuthenticationToken>, IEventHandlerRegistrar
      56             :                 {
      57             :                         bool isBusBound = Kernel.GetBindings(typeof(TBus)).Any();
      58             :                         TBus bus;
      59             :                         if (!isBusBound)
      60             :                         {
      61             :                                 bus = Kernel.Get<TBus>();
      62             :                                 Bind<TBus>()
      63             :                                         .ToConstant(bus)
      64             :                                         .InSingletonScope();
      65             :                         }
      66             :                         else
      67             :                                 bus = Kernel.Get<TBus>();
      68             : 
      69             :                         return bus;
      70             :                 }
      71             : 
      72             :                 /// <summary>
      73             :                 /// Register the CQRS event receiver
      74             :                 /// </summary>
      75           1 :                 public virtual void RegisterEventReceiver<TBus>(TBus bus)
      76             :                         where TBus : IEventReceiver<TAuthenticationToken>, IEventHandlerRegistrar
      77             :                 {
      78             :                         Bind<IEventReceiver<TAuthenticationToken>>()
      79             :                                 .ToConstant(bus)
      80             :                                 .InSingletonScope();
      81             :                 }
      82             : 
      83             :                 /// <summary>
      84             :                 /// Register the CQRS event handler registrar
      85             :                 /// </summary>
      86           1 :                 public virtual void RegisterEventHandlerRegistrar<TBus>(TBus bus)
      87             :                         where TBus : IEventReceiver<TAuthenticationToken>, IEventHandlerRegistrar
      88             :                 {
      89             :                         bool isHandlerRegistrationBound = Kernel.GetBindings(typeof(IEventHandlerRegistrar)).Any();
      90             :                         if (!isHandlerRegistrationBound)
      91             :                         {
      92             :                                 Bind<IEventHandlerRegistrar>()
      93             :                                         .ToConstant(bus)
      94             :                                         .InSingletonScope();
      95             :                         }
      96             :                 }
      97             : 
      98             :                 /// <summary>
      99             :                 /// Register the CQRS event handler message serialiser
     100             :                 /// </summary>
     101           1 :                 public virtual void RegisterEventMessageSerialiser()
     102             :                 {
     103             :                         bool isMessageSerialiserBound = Kernel.GetBindings(typeof(IMessageSerialiser<TAuthenticationToken>)).Any();
     104             :                         if (!isMessageSerialiserBound)
     105             :                         {
     106             :                                 Bind<IMessageSerialiser<TAuthenticationToken>>()
     107             :                                         .To<MessageSerialiser<TAuthenticationToken>>()
     108             :                                         .InSingletonScope();
     109             :                         }
     110             :                 }
     111             :         }
     112             : }

Generated by: LCOV version 1.10