Documentation Coverage Report
Current view: top level - Ninject/Azure/Cqrs.Ninject.Azure.ServiceBus.CommandBus/Configuration - AzureCommandBusReceiverModule.cs Hit Total Coverage
Version: 2.2 Artefacts: 6 6 100.0 %
Date: 2018-08-07 15:04:50

          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.Commands;
      14             : using Ninject;
      15             : using Ninject.Modules;
      16             : 
      17             : namespace Cqrs.Ninject.Azure.ServiceBus.CommandBus.Configuration
      18             : {
      19             :         /// <summary>
      20             :         /// A <see cref="INinjectModule"/> that wires up <see cref="AzureCommandBusReceiver{TAuthenticationToken}"/> as the <see cref="ICommandReceiver"/> and other require components.
      21             :         /// </summary>
      22             :         /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
      23             :         public class AzureCommandBusReceiverModule<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             :                         RegisterCommandMessageSerialiser();
      41             :                         var bus = GetOrCreateBus<AzureCommandBusReceiver<TAuthenticationToken>>();
      42             : 
      43             :                         RegisterCommandReceiver(bus);
      44             :                         RegisterCommandHandlerRegistrar(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 : ICommandReceiver<TAuthenticationToken>, ICommandHandlerRegistrar
      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 command receiver
      74             :                 /// </summary>
      75           1 :                 public virtual void RegisterCommandReceiver<TBus>(TBus bus)
      76             :                         where TBus : ICommandReceiver<TAuthenticationToken>, ICommandHandlerRegistrar
      77             :                 {
      78             :                         Bind<ICommandReceiver<TAuthenticationToken>>()
      79             :                                 .ToConstant(bus)
      80             :                                 .InSingletonScope();
      81             :                 }
      82             : 
      83             :                 /// <summary>
      84             :                 /// Register the CQRS command handler registrar
      85             :                 /// </summary>
      86           1 :                 public virtual void RegisterCommandHandlerRegistrar<TBus>(TBus bus)
      87             :                         where TBus : ICommandReceiver<TAuthenticationToken>, ICommandHandlerRegistrar
      88             :                 {
      89             :                         Bind<ICommandHandlerRegistrar>()
      90             :                                         .ToConstant(bus)
      91             :                                 .InSingletonScope();
      92             :                 }
      93             : 
      94             :                 /// <summary>
      95             :                 /// Register the CQRS command handler message serialiser
      96             :                 /// </summary>
      97           1 :                 public virtual void RegisterCommandMessageSerialiser()
      98             :                 {
      99             :                         bool isMessageSerialiserBound = Kernel.GetBindings(typeof(IMessageSerialiser<TAuthenticationToken>)).Any();
     100             :                         if (!isMessageSerialiserBound)
     101             :                         {
     102             :                                 Bind<IMessageSerialiser<TAuthenticationToken>>()
     103             :                                         .To<MessageSerialiser<TAuthenticationToken>>()
     104             :                                         .InSingletonScope();
     105             :                         }
     106             :                 }
     107             :         }
     108             : }

Generated by: LCOV version 1.12