Documentation Coverage Report
Current view: top level - Ninject/Cqrs.Ninject/Configuration - CqrsModule.cs Hit Total Coverage
Version: 4.0 Artefacts: 11 11 100.0 %
Date: 2019-11-24 03:15:41

          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             : #if NET40
      12             : using cdmdotnet.AutoMapper;
      13             : #endif
      14             : using Cqrs.Authentication;
      15             : using Cqrs.Bus;
      16             : using Cqrs.Domain;
      17             : using Cqrs.Domain.Factories;
      18             : using Chinchilla.Logging;
      19             : using Chinchilla.Logging.Configuration;
      20             : using Chinchilla.StateManagement;
      21             : using Chinchilla.StateManagement.Threaded;
      22             : using Chinchilla.StateManagement.Web;
      23             : using Cqrs.Configuration;
      24             : using Cqrs.Events;
      25             : using Cqrs.Repositories.Queries;
      26             : using Cqrs.Snapshots;
      27             : using Ninject.Modules;
      28             : 
      29             : namespace Cqrs.Ninject.Configuration
      30             : {
      31             :         /// <summary>
      32             :         /// The main <see cref="INinjectModule"/> for use with the CQRS package that wires up many of the prerequisites for running CQRS.NET.
      33             :         /// </summary>
      34             :         /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
      35             :         /// <typeparam name="TAuthenticationTokenHelper">The <see cref="Type"/> of the authentication token helper.</typeparam>
      36             :         public class CqrsModule<TAuthenticationToken, TAuthenticationTokenHelper> : ResolvableModule
      37             :                 where TAuthenticationTokenHelper : class, IAuthenticationTokenHelper<TAuthenticationToken>
      38           1 :         {
      39             :                 /// <summary>
      40             :                 /// Indicates that web based wire-up is required rather than console, WPF or winforms based wire-up.s
      41             :                 /// </summary>
      42             :                 protected bool SetupForWeb { get; private set; }
      43             : 
      44             :                 /// <summary>
      45             :                 /// Indicates that logging should be configured for SQL Server rather than console.
      46             :                 /// </summary>
      47             :                 protected bool SetupForSqlLogging { get; private set; }
      48             : 
      49             :                 /// <summary>
      50             :                 /// Indicates that the <see cref="ConfigurationManager"/> should be registered automatically.
      51             :                 /// </summary>
      52             :                 protected bool RegisterDefaultConfigurationManager { get; private set; }
      53             : 
      54             :                 /// <summary>
      55             :                 /// Indicates that the <see cref="ISnapshotStrategy{TAuthenticationToken}"/> should be registered automatically.
      56             :                 /// </summary>
      57             :                 protected bool RegisterDefaultSnapshotStrategy { get; private set; }
      58             : 
      59             :                 /// <summary>
      60             :                 /// Indicates that the <see cref="ISnapshotAggregateRepository{TAuthenticationToken}"/> should be registered automatically.
      61             :                 /// </summary>
      62             :                 protected bool RegisterDefaultSnapshotAggregateRepository { get; private set; }
      63             : 
      64             :                 /// <summary>
      65             :                 /// Indicates that the <see cref="ISnapshotBuilder"/> should be registered automatically.
      66             :                 /// </summary>
      67             :                 protected bool RegisterDefaultSnapshotBuilder { get; private set; }
      68             : 
      69             :                 /// <summary>
      70             :                 /// Instantiate a new instance of the <see cref="CqrsModule{TAuthenticationToken,TAuthenticationTokenHelper}"/> that uses the provided <paramref name="configurationManager"/>
      71             :                 /// to read the following configuration settings:
      72             :                 /// "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.
      73             :                 /// "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.
      74             :                 /// "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.
      75             :                 /// "Cqrs.RegisterDefaultSnapshotStrategy": If set true the <see cref="DefaultSnapshotStrategy{TAuthenticationToken}"/> will be registered.
      76             :                 /// "Cqrs.RegisterDefaultSnapshotAggregateRepository": If set true the <see cref="SnapshotRepository{TAuthenticationToken}"/> will be registered.
      77             :                 /// "Cqrs.RegisterDefaultSnapshotBuilder": If set true the <see cref="DefaultSnapshotBuilder"/> will be registered.
      78             :                 /// </summary>
      79             :                 /// <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>
      80           1 :                 public CqrsModule(IConfigurationManager configurationManager = null)
      81             :                 {
      82             :                         configurationManager = configurationManager ?? new ConfigurationManager();
      83             :                         if (configurationManager.TryGetSetting("Cqrs.SetupForWeb", out bool setupForWeb))
      84             :                                 SetupForWeb = setupForWeb;
      85             :                         if (configurationManager.TryGetSetting("Cqrs.SetupForSqlLogging", out bool setupForSqlLogging))
      86             :                                 SetupForSqlLogging = setupForSqlLogging;
      87             :                         if (configurationManager.TryGetSetting("Cqrs.RegisterDefaultConfigurationManager", out bool registerDefaultConfigurationManager))
      88             :                                 RegisterDefaultConfigurationManager = registerDefaultConfigurationManager;
      89             :                         if (configurationManager.TryGetSetting("Cqrs.RegisterDefaultSnapshotAggregateRepository", out bool registerDefaultSnapshotAggregateRepository))
      90             :                                 RegisterDefaultSnapshotAggregateRepository = registerDefaultSnapshotAggregateRepository;
      91             :                         else
      92             :                                 RegisterDefaultSnapshotAggregateRepository = true;
      93             :                         if (configurationManager.TryGetSetting("Cqrs.RegisterDefaultSnapshotStrategy", out bool registerDefaultSnapshotStrategy))
      94             :                                 RegisterDefaultSnapshotStrategy = registerDefaultSnapshotStrategy;
      95             :                         else
      96             :                                 RegisterDefaultSnapshotStrategy = true;
      97             :                         if (configurationManager.TryGetSetting("Cqrs.RegisterDefaultSnapshotBuilder", out bool registerDefaultSnapshotBuilder))
      98             :                                 RegisterDefaultSnapshotBuilder = registerDefaultSnapshotBuilder;
      99             :                         else
     100             :                                 RegisterDefaultSnapshotBuilder = true;
     101             :                 }
     102             : 
     103             :                 /// <summary>
     104             :                 /// Instantiate a new instance of the <see cref="CqrsModule{TAuthenticationToken,TAuthenticationTokenHelper}"/>.
     105             :                 /// </summary>
     106             :                 /// <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>
     107             :                 /// <param name="setupForSqlLogging">Set this to true to use <see cref="SqlLogger"/> otherwise the <see cref="ConsoleLogger"/> will be bootstrapped by default.</param>
     108             :                 /// <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>
     109             :                 /// <param name="registerDefaultSnapshotAggregateRepository">If set true the <see cref="SnapshotRepository{TAuthenticationToken}"/> will be registered.</param>
     110             :                 /// <param name="registerDefaultSnapshotStrategy">If set true the <see cref="DefaultSnapshotStrategy{TAuthenticationToken}"/> will be registered.</param>
     111             :                 /// <param name="registerDefaultSnapshotBuilder">If set true the <see cref="DefaultSnapshotBuilder"/> will be registered.</param>
     112           1 :                 public CqrsModule(bool setupForWeb, bool setupForSqlLogging, bool registerDefaultConfigurationManager = false, bool registerDefaultSnapshotAggregateRepository = true, bool registerDefaultSnapshotStrategy = true, bool registerDefaultSnapshotBuilder = true)
     113             :                 {
     114             :                         SetupForWeb = setupForWeb;
     115             :                         SetupForSqlLogging = setupForSqlLogging;
     116             :                         RegisterDefaultConfigurationManager = registerDefaultConfigurationManager;
     117             :                         RegisterDefaultSnapshotAggregateRepository = registerDefaultSnapshotAggregateRepository;
     118             :                         RegisterDefaultSnapshotStrategy = registerDefaultSnapshotStrategy;
     119             :                         RegisterDefaultSnapshotBuilder = registerDefaultSnapshotBuilder;
     120             :                 }
     121             : 
     122             : #region Overrides of NinjectModule
     123             : 
     124             :                 /// <summary>
     125             :                 /// Loads the module into the kernel.
     126             :                 /// </summary>
     127           1 :                 public override void Load()
     128             :                 {
     129             :                         RegisterFactories();
     130             :                         RegisterRepositories();
     131             :                         RegisterQueryBuilders();
     132             :                         RegisterServices();
     133             :                         RegisterCqrsRequirements();
     134             : #if NET40
     135             :                         RegisterAutomapperComponents();
     136             : #endif
     137             :                         RegisterLoggerComponents();
     138             :                         RegisterCaching();
     139             :                 }
     140             : 
     141             : #endregion
     142             : 
     143             :                 /// <summary>
     144             :                 /// Register the all factories
     145             :                 /// </summary>
     146           1 :                 public virtual void RegisterFactories()
     147             :                 {
     148             :                         Bind<IQueryFactory>()
     149             :                                 .To<QueryFactory>()
     150             :                                 .InSingletonScope();
     151             :                         Bind<IHashAlgorithmFactory>()
     152             :                                 .To<BuiltInHashAlgorithmFactory>()
     153             :                                 .InSingletonScope();
     154             :                 }
     155             : 
     156             :                 /// <summary>
     157             :                 /// Register the all components for the <see cref="ILogger"/>
     158             :                 /// </summary>
     159           1 :                 public virtual void RegisterLoggerComponents()
     160             :                 {
     161             :                         bool isCorrelationIdHelperBound = Kernel.GetBindings(typeof(ICorrelationIdHelper)).Any();
     162             :                         if (!isCorrelationIdHelperBound)
     163             :                         {
     164             :                                 if (SetupForWeb)
     165             :                                         Bind<ICorrelationIdHelper>()
     166             :                                                 .To<WebCorrelationIdHelper>()
     167             :                                                 .InSingletonScope();
     168             :                                 else
     169             :                                         Bind<ICorrelationIdHelper>()
     170             :                                                 .To<CorrelationIdHelper>()
     171             :                                                 .InSingletonScope();
     172             :                         }
     173             : 
     174             :                         bool isLoggerBound = Kernel.GetBindings(typeof(ILogger)).Any();
     175             :                         if (!isLoggerBound)
     176             :                         {
     177             :                                 if (SetupForSqlLogging)
     178             :                                         Bind<ILogger>()
     179             :                                                 .To<SqlLogger>()
     180             :                                                 .InSingletonScope();
     181             :                                 else
     182             :                                         Bind<ILogger>()
     183             :                                                 .To<ConsoleLogger>()
     184             :                                                 .InSingletonScope();
     185             :                         }
     186             : 
     187             :                         bool isLoggerSettingsBound = Kernel.GetBindings(typeof(ILoggerSettings)).Any();
     188             :                         if (!isLoggerSettingsBound)
     189             :                         {
     190             :                                 Bind<ILoggerSettings>()
     191             :                                         .To<LoggerSettings>()
     192             :                                         .InSingletonScope();
     193             :                         }
     194             : 
     195             :                         bool isTelemetryHelperBound = Kernel.GetBindings(typeof(ITelemetryHelper)).Any();
     196             :                         if (!isTelemetryHelperBound)
     197             :                         {
     198             :                                 Bind<ITelemetryHelper>()
     199             :                                         .To<NullTelemetryHelper>()
     200             :                                         .InSingletonScope();
     201             :                         }
     202             :                 }
     203             : 
     204             : #if NET40
     205             :                 /// <summary>
     206             :                 /// Register the all <see cref="IAutomapHelper"/>
     207             :                 /// </summary>
     208             :                 public virtual void RegisterAutomapperComponents()
     209             :                 {
     210             :                         Bind<IAutomapHelper>()
     211             :                                 .To<AutomapHelper>()
     212             :                                 .InSingletonScope();
     213             :                 }
     214             : #endif
     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<ContextItemCollectionFactory>()
     259             :                                         .InSingletonScope();
     260             :                                 Bind<IContextItemCollection>()
     261             :                                         .To<Chinchilla.StateManagement.Threaded.ContextItemCollection>()
     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.13