Documentation Coverage Report
Current view: top level - Ninject/Cqrs.Ninject/Configuration - CqrsModule.cs Hit Total Coverage
Version: 2.2 Artefacts: 12 12 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 cdmdotnet.AutoMapper;
      12             : using Cqrs.Authentication;
      13             : using Cqrs.Bus;
      14             : using Cqrs.Domain;
      15             : using Cqrs.Domain.Factories;
      16             : using cdmdotnet.Logging;
      17             : using cdmdotnet.Logging.Configuration;
      18             : using cdmdotnet.StateManagement;
      19             : using cdmdotnet.StateManagement.Threaded;
      20             : using cdmdotnet.StateManagement.Web;
      21             : using Cqrs.Configuration;
      22             : using Cqrs.Events;
      23             : using Cqrs.Repositories.Queries;
      24             : using Cqrs.Snapshots;
      25             : using Ninject.Modules;
      26             : 
      27             : namespace Cqrs.Ninject.Configuration
      28             : {
      29             :         /// <summary>
      30             :         /// The main <see cref="INinjectModule"/> for use with the CQRS package that wires up many of the prerequisites for running CQRS.NET.
      31             :         /// </summary>
      32             :         /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
      33             :         /// <typeparam name="TAuthenticationTokenHelper">The <see cref="Type"/> of the authentication token helper.</typeparam>
      34             :         public class CqrsModule<TAuthenticationToken, TAuthenticationTokenHelper> : ResolvableModule
      35             :                 where TAuthenticationTokenHelper : class, IAuthenticationTokenHelper<TAuthenticationToken>
      36           1 :         {
      37             :                 /// <summary>
      38             :                 /// Indicates that web based wire-up is required rather than console, WPF or winforms based wire-up.s
      39             :                 /// </summary>
      40             :                 protected bool SetupForWeb { get; private set; }
      41             : 
      42             :                 /// <summary>
      43             :                 /// Indicates that logging should be configured for SQL Server rather than console.
      44             :                 /// </summary>
      45             :                 protected bool SetupForSqlLogging { get; private set; }
      46             : 
      47             :                 /// <summary>
      48             :                 /// Indicates that the <see cref="ConfigurationManager"/> should be registered automatically.
      49             :                 /// </summary>
      50             :                 protected bool RegisterDefaultConfigurationManager { get; private set; }
      51             : 
      52             :                 /// <summary>
      53             :                 /// Indicates that the <see cref="ISnapshotStrategy{TAuthenticationToken}"/> should be registered automatically.
      54             :                 /// </summary>
      55             :                 protected bool RegisterDefaultSnapshotStrategy { get; private set; }
      56             : 
      57             :                 /// <summary>
      58             :                 /// Indicates that the <see cref="ISnapshotAggregateRepository{TAuthenticationToken}"/> should be registered automatically.
      59             :                 /// </summary>
      60             :                 protected bool RegisterDefaultSnapshotAggregateRepository { get; private set; }
      61             : 
      62             :                 /// <summary>
      63             :                 /// Indicates that the <see cref="ISnapshotBuilder"/> should be registered automatically.
      64             :                 /// </summary>
      65             :                 protected bool RegisterDefaultSnapshotBuilder { get; private set; }
      66             : 
      67             :                 /// <summary>
      68             :                 /// Instantiate a new instance of the <see cref="CqrsModule{TAuthenticationToken,TAuthenticationTokenHelper}"/> that uses the provided <paramref name="configurationManager"/>
      69             :                 /// to read the following configuration settings:
      70             :                 /// "Cqrs.SetupForWeb": If set to true the system will be configured for hosting in IIS or some other web-server that provides access to System.Web.HttpContext.Current.
      71             :                 /// "Cqrs.SetupForSqlLogging": If set to true the <see cref="SqlLogger"/> will be bootstrapped by default, otherwise the <see cref="ConsoleLogger"/> will be bootstrapped by default.
      72             :                 /// "Cqrs.RegisterDefaultConfigurationManager": If set true the <see cref="ConfigurationManager"/> will be registered. If you want to use the Azure one leave this as false (the default) and register it yourself.
      73             :                 /// "Cqrs.RegisterDefaultSnapshotStrategy": If set true the <see cref="DefaultSnapshotStrategy{TAuthenticationToken}"/> will be registered.
      74             :                 /// "Cqrs.RegisterDefaultSnapshotAggregateRepository": If set true the <see cref="SnapshotRepository{TAuthenticationToken}"/> will be registered.
      75             :                 /// "Cqrs.RegisterDefaultSnapshotBuilder": If set true the <see cref="DefaultSnapshotBuilder"/> will be registered.
      76             :                 /// </summary>
      77             :                 /// <param name="configurationManager">The <see cref="IConfigurationManager"/> to use, if one isn't provided then <see cref="ConfigurationManager"/> is instantiate, used and then disposed.</param>
      78           1 :                 public CqrsModule(IConfigurationManager configurationManager = null)
      79             :                 {
      80             :                         configurationManager = configurationManager ?? new ConfigurationManager();
      81             :                         bool setupForWeb;
      82             :                         if (configurationManager.TryGetSetting("Cqrs.SetupForWeb", out setupForWeb))
      83             :                                 SetupForWeb = setupForWeb;
      84             :                         bool setupForSqlLogging;
      85             :                         if (configurationManager.TryGetSetting("Cqrs.SetupForSqlLogging", out setupForSqlLogging))
      86             :                                 SetupForSqlLogging = setupForSqlLogging;
      87             :                         bool registerDefaultConfigurationManager;
      88             :                         if (configurationManager.TryGetSetting("Cqrs.RegisterDefaultConfigurationManager", out registerDefaultConfigurationManager))
      89             :                                 RegisterDefaultConfigurationManager = registerDefaultConfigurationManager;
      90             :                         bool registerDefaultSnapshotAggregateRepository;
      91             :                         if (configurationManager.TryGetSetting("Cqrs.RegisterDefaultSnapshotAggregateRepository", out registerDefaultSnapshotAggregateRepository))
      92             :                                 RegisterDefaultSnapshotAggregateRepository = registerDefaultSnapshotAggregateRepository;
      93             :                         else
      94             :                                 RegisterDefaultSnapshotAggregateRepository = true;
      95             :                         bool registerDefaultSnapshotStrategy;
      96             :                         if (configurationManager.TryGetSetting("Cqrs.RegisterDefaultSnapshotStrategy", out registerDefaultSnapshotStrategy))
      97             :                                 RegisterDefaultSnapshotStrategy = registerDefaultSnapshotStrategy;
      98             :                         else
      99             :                                 RegisterDefaultSnapshotStrategy = true;
     100             :                         bool registerDefaultSnapshotBuilder;
     101             :                         if (configurationManager.TryGetSetting("Cqrs.RegisterDefaultSnapshotBuilder", out registerDefaultSnapshotBuilder))
     102             :                                 RegisterDefaultSnapshotBuilder = registerDefaultSnapshotBuilder;
     103             :                         else
     104             :                                 RegisterDefaultSnapshotBuilder = true;
     105             :                 }
     106             : 
     107             :                 /// <summary>
     108             :                 /// Instantiate a new instance of the <see cref="CqrsModule{TAuthenticationToken,TAuthenticationTokenHelper}"/>.
     109             :                 /// </summary>
     110             :                 /// <param name="setupForWeb">Set this to true if you will host this in IIS or some other web-server that provides access to System.Web.HttpContext.Current.</param>
     111             :                 /// <param name="setupForSqlLogging">Set this to true to use <see cref="SqlLogger"/> otherwise the <see cref="ConsoleLogger"/> will be bootstrapped by default.</param>
     112             :                 /// <param name="registerDefaultConfigurationManager">Set this to true to use <see cref="ConfigurationManager"/>. If you want to use the Azure one leave this as false (the default) and register it yourself.</param>
     113             :                 /// <param name="registerDefaultSnapshotAggregateRepository">If set true the <see cref="SnapshotRepository{TAuthenticationToken}"/> will be registered.</param>
     114             :                 /// <param name="registerDefaultSnapshotStrategy">If set true the <see cref="DefaultSnapshotStrategy{TAuthenticationToken}"/> will be registered.</param>
     115             :                 /// <param name="registerDefaultSnapshotBuilder">If set true the <see cref="DefaultSnapshotBuilder"/> will be registered.</param>
     116           1 :                 public CqrsModule(bool setupForWeb, bool setupForSqlLogging, bool registerDefaultConfigurationManager = false, bool registerDefaultSnapshotAggregateRepository = true, bool registerDefaultSnapshotStrategy = true, bool registerDefaultSnapshotBuilder = true)
     117             :                 {
     118             :                         SetupForWeb = setupForWeb;
     119             :                         SetupForSqlLogging = setupForSqlLogging;
     120             :                         RegisterDefaultConfigurationManager = registerDefaultConfigurationManager;
     121             :                         RegisterDefaultSnapshotAggregateRepository = registerDefaultSnapshotAggregateRepository;
     122             :                         RegisterDefaultSnapshotStrategy = registerDefaultSnapshotStrategy;
     123             :                         RegisterDefaultSnapshotBuilder = registerDefaultSnapshotBuilder;
     124             :                 }
     125             : 
     126             :                 #region Overrides of NinjectModule
     127             : 
     128             :                 /// <summary>
     129             :                 /// Loads the module into the kernel.
     130             :                 /// </summary>
     131           1 :                 public override void Load()
     132             :                 {
     133             :                         RegisterFactories();
     134             :                         RegisterRepositories();
     135             :                         RegisterQueryBuilders();
     136             :                         RegisterServices();
     137             :                         RegisterCqrsRequirements();
     138             :                         RegisterAutomapperComponents();
     139             :                         RegisterLoggerComponents();
     140             :                         RegisterCaching();
     141             :                 }
     142             : 
     143             :                 #endregion
     144             : 
     145             :                 /// <summary>
     146             :                 /// Register the all factories
     147             :                 /// </summary>
     148           1 :                 public virtual void RegisterFactories()
     149             :                 {
     150             :                         Bind<IQueryFactory>()
     151             :                                 .To<QueryFactory>()
     152             :                                 .InSingletonScope();
     153             :                         Bind<IHashAlgorithmFactory>()
     154             :                                 .To<BuiltInHashAlgorithmFactory>()
     155             :                                 .InSingletonScope();
     156             :                 }
     157             : 
     158             :                 /// <summary>
     159             :                 /// Register the all components for the <see cref="ILogger"/>
     160             :                 /// </summary>
     161           1 :                 public virtual void RegisterLoggerComponents()
     162             :                 {
     163             :                         bool isCorrelationIdHelperBound = Kernel.GetBindings(typeof(ICorrelationIdHelper)).Any();
     164             :                         if (!isCorrelationIdHelperBound)
     165             :                         {
     166             :                                 if (SetupForWeb)
     167             :                                         Bind<ICorrelationIdHelper>()
     168             :                                                 .To<WebCorrelationIdHelper>()
     169             :                                                 .InSingletonScope();
     170             :                                 else
     171             :                                         Bind<ICorrelationIdHelper>()
     172             :                                                 .To<CorrelationIdHelper>()
     173             :                                                 .InSingletonScope();
     174             :                         }
     175             : 
     176             :                         bool isLoggerBound = Kernel.GetBindings(typeof(ILogger)).Any();
     177             :                         if (!isLoggerBound)
     178             :                         {
     179             :                                 if (SetupForSqlLogging)
     180             :                                         Bind<ILogger>()
     181             :                                                 .To<SqlLogger>()
     182             :                                                 .InSingletonScope();
     183             :                                 else
     184             :                                         Bind<ILogger>()
     185             :                                                 .To<ConsoleLogger>()
     186             :                                                 .InSingletonScope();
     187             :                         }
     188             : 
     189             :                         bool isLoggerSettingsBound = Kernel.GetBindings(typeof(ILoggerSettings)).Any();
     190             :                         if (!isLoggerSettingsBound)
     191             :                         {
     192             :                                 Bind<ILoggerSettings>()
     193             :                                         .To<LoggerSettings>()
     194             :                                         .InSingletonScope();
     195             :                         }
     196             : 
     197             :                         bool isTelemetryHelperBound = Kernel.GetBindings(typeof(ITelemetryHelper)).Any();
     198             :                         if (!isTelemetryHelperBound)
     199             :                         {
     200             :                                 Bind<ITelemetryHelper>()
     201             :                                         .To<NullTelemetryHelper>()
     202             :                                         .InSingletonScope();
     203             :                         }
     204             :                 }
     205             : 
     206             :                 /// <summary>
     207             :                 /// Register the all <see cref="IAutomapHelper"/>
     208             :                 /// </summary>
     209           1 :                 public virtual void RegisterAutomapperComponents()
     210             :                 {
     211             :                         Bind<IAutomapHelper>()
     212             :                                 .To<AutomapHelper>()
     213             :                                 .InSingletonScope();
     214             :                 }
     215             : 
     216             :                 /// <summary>
     217             :                 /// Register the all repositories
     218             :                 /// </summary>
     219           1 :                 public virtual void RegisterRepositories()
     220             :                 {
     221             :                 }
     222             : 
     223             :                 /// <summary>
     224             :                 /// Register the all query builders
     225             :                 /// </summary>
     226           1 :                 public virtual void RegisterQueryBuilders()
     227             :                 {
     228             :                 }
     229             : 
     230             :                 /// <summary>
     231             :                 /// Register the all services
     232             :                 /// </summary>
     233           1 :                 public virtual void RegisterServices()
     234             :                 {
     235             :                 }
     236             : 
     237             :                 /// <summary>
     238             :                 /// Register the all caching stuffs
     239             :                 /// </summary>
     240           1 :                 public virtual void RegisterCaching()
     241             :                 {
     242             :                         if (Kernel.GetBindings(typeof (IContextItemCollectionFactory)).Any())
     243             :                                 Kernel.Unbind<IContextItemCollectionFactory>();
     244             :                         if (Kernel.GetBindings(typeof(IContextItemCollection)).Any())
     245             :                                 Kernel.Unbind<IContextItemCollection>();
     246             :                         if (SetupForWeb)
     247             :                         {
     248             :                                 Bind<IContextItemCollectionFactory>()
     249             :                                         .To<WebContextItemCollectionFactory>()
     250             :                                         .InSingletonScope();
     251             :                                 Bind<IContextItemCollection>()
     252             :                                         .To<WebContextItemCollection>()
     253             :                                         .InSingletonScope();
     254             :                         }
     255             :                         else
     256             :                         {
     257             :                                 Bind<IContextItemCollectionFactory>()
     258             :                                         .To<ThreadedContextItemCollectionFactory>()
     259             :                                         .InSingletonScope();
     260             :                                 Bind<IContextItemCollection>()
     261             :                                         .To<ThreadedContextItemCollection>()
     262             :                                         .InSingletonScope();
     263             :                         }
     264             :                 }
     265             : 
     266             :                 /// <summary>
     267             :                 /// Register the all Cqrs requirements
     268             :                 /// </summary>
     269           1 :                 public virtual void RegisterCqrsRequirements()
     270             :                 {
     271             :                         Bind<IUnitOfWork<TAuthenticationToken>>()
     272             :                                 .To<UnitOfWork<TAuthenticationToken>>()
     273             :                                 .InTransientScope();
     274             :                         Bind<ISagaUnitOfWork<TAuthenticationToken>>()
     275             :                                 .To<SagaUnitOfWork<TAuthenticationToken>>()
     276             :                                 .InTransientScope();
     277             :                         Bind<IAggregateRepository<TAuthenticationToken>>()
     278             :                                 .To<AggregateRepository<TAuthenticationToken>>()
     279             :                                 .InSingletonScope();
     280             : 
     281             :                         if (RegisterDefaultSnapshotAggregateRepository)
     282             :                                 Bind<ISnapshotAggregateRepository<TAuthenticationToken>>()
     283             :                                         .To<SnapshotRepository<TAuthenticationToken>>()
     284             :                                         .InSingletonScope();
     285             : 
     286             :                         Bind<ISagaRepository<TAuthenticationToken>>()
     287             :                                 .To<SagaRepository<TAuthenticationToken>>()
     288             :                                 .InSingletonScope();
     289             :                         Bind<IAggregateFactory>()
     290             :                                 .To<AggregateFactory>()
     291             :                                 .InSingletonScope();
     292             : 
     293             :                         Bind<IAuthenticationTokenHelper<TAuthenticationToken>>()
     294             :                                 .To<TAuthenticationTokenHelper>()
     295             :                                 .InSingletonScope();
     296             : 
     297             :                         Bind<IStoreLastEventProcessed>()
     298             :                                 .To<FileBasedLastEventProcessedStore>()
     299             :                                 .InSingletonScope();
     300             : 
     301             :                         Bind<IBusHelper>()
     302             :                                 .To<BusHelper>()
     303             :                                 .InSingletonScope();
     304             : 
     305             :                         if (RegisterDefaultConfigurationManager)
     306             :                                 Bind<IConfigurationManager>()
     307             :                                         .To<ConfigurationManager>()
     308             :                                         .InSingletonScope();
     309             : 
     310             :                         if (RegisterDefaultSnapshotStrategy)
     311             :                                 Bind<ISnapshotStrategy<TAuthenticationToken>>()
     312             :                                         .To<DefaultSnapshotStrategy<TAuthenticationToken>>()
     313             :                                         .InSingletonScope();
     314             : 
     315             :                         if (RegisterDefaultSnapshotBuilder)
     316             :                                 Bind<ISnapshotBuilder>()
     317             :                                         .To<DefaultSnapshotBuilder>()
     318             :                                         .InSingletonScope();
     319             :                 }
     320             :         }
     321             : }

Generated by: LCOV version 1.12