private static ICommandingDependencyResolverAdapter Register <TSerializer>(this ICommandingDependencyResolverAdapter dependencyResolver) where TSerializer : IAzureStorageQueueSerializer
 {
     dependencyResolver.TypeMapping <IAzureStorageQueueSerializer, TSerializer>();
     dependencyResolver.TypeMapping <IAzureStorageCommandQueueProcessorFactory, AzureStorageCommandQueueProcessorFactory>();
     dependencyResolver.TypeMapping <IAzureStorageQueueDispatcherFactory, AzureStorageQueueDispatcherFactory>();
     return(dependencyResolver);
 }
        /// <summary>
        /// Registers a command auditor that writes to an event hub
        /// </summary>
        /// <param name="resolver">Dependency resolver</param>
        /// <param name="eventHubClient">The event hub client</param>
        /// <param name="partitionKeyProvider">An optional partition key provider, if unspecified events will be sent unpartitioned</param>
        /// <param name="options">Options for the event hub auditor configuration</param>
        /// <returns>Dependency resolver</returns>
        public static ICommandingDependencyResolverAdapter AddEventHubCommandAuditing(this ICommandingDependencyResolverAdapter resolver,
                                                                                      Microsoft.Azure.EventHubs.EventHubClient eventHubClient,
                                                                                      IPartitionKeyProvider partitionKeyProvider = null,
                                                                                      AzureEventHubAuditorOptions options        = null)
        {
            options = options ?? new AzureEventHubAuditorOptions();
            IEventHubClient client = new EventHubClient(eventHubClient);

            if (partitionKeyProvider == null)
            {
                partitionKeyProvider = new NullPartitionKeyProvider();
            }

            resolver.RegisterInstance(client);
            resolver.RegisterInstance(partitionKeyProvider);
            resolver.TypeMapping <IAuditItemMapper, AuditItemMapper>();
            resolver.TypeMapping <IEventHubSerializer, EventHubSerializer>();
            if (options.UsePreDispatchAuditor)
            {
                EnsureRuntimeIsAssociated(resolver);
                resolver.AssociatedCommandingRuntime.AddPreDispatchCommandingAuditor <AzureEventHubCommandAuditor>(resolver, options.AuditPreDispatchRootOnly);
            }
            if (options.UsePostDispatchAuditor)
            {
                EnsureRuntimeIsAssociated(resolver);
                resolver.AssociatedCommandingRuntime.AddPostDispatchCommandingAuditor <AzureEventHubCommandAuditor>(resolver, options.AuditPostDispatchRootOnly);
            }
            if (options.UseExecutionAuditor)
            {
                EnsureRuntimeIsAssociated(resolver);
                resolver.AssociatedCommandingRuntime.AddExecutionCommandingAuditor <AzureEventHubCommandAuditor>(resolver, options.AuditExecuteDispatchRootOnly);
            }
            return(resolver);
        }
 private static ICommandingDependencyResolverAdapter Register <TSerializer>(ICommandingDependencyResolverAdapter dependencyResolver, HttpClient client) where TSerializer : IHttpCommandSerializer
 {
     HttpClientProvider = client == null ? new HttpClientProvider() : new HttpClientProvider(client);
     dependencyResolver.RegisterInstance(HttpClientProvider);
     dependencyResolver.TypeMapping <IHttpCommandSerializer, TSerializer>();
     dependencyResolver.TypeMapping <IUriCommandQueryBuilder, UriCommandQueryBuilder>();
     dependencyResolver.TypeMapping <IHttpCommandDispatcherFactory, HttpCommandDispatcherFactoryImpl>();
     return(dependencyResolver);
 }
        /// <summary>
        /// Registers the IAzureStorageCommandQueueProcessorFactory interface through which a audit queue processor task can be
        /// started
        /// </summary>
        /// <param name="dependencyResolver">The dependency resolver</param>
        /// <param name="queue">The queue to dequeue from</param>
        /// <param name="deadLetterQueue">An optional dead letter queue to place items in if errors repeatedly occur in item processing</param>
        /// <returns>The dependency resovler</returns>
        public static ICommandingDependencyResolverAdapter AddAzureStorageAuditQueueProcessor(this ICommandingDependencyResolverAdapter dependencyResolver,
                                                                                              CloudQueue queue, CloudQueue deadLetterQueue = null)
        {
            ICloudAuditQueueProvider cloudAuditQueueProvider = new CloudAuditQueueProvider(queue, deadLetterQueue);

            dependencyResolver.RegisterInstance(cloudAuditQueueProvider);
            dependencyResolver.TypeMapping <IAzureStorageQueueSerializer, AzureStorageQueueSerializer>();
            dependencyResolver.TypeMapping <IAzureStorageAuditQueueProcessorFactory, AzureStorageAuditQueueProcessorFactory>();
            return(dependencyResolver);
        }
        public static ICommandingDependencyResolverAdapter AddQueues(this ICommandingDependencyResolverAdapter dependencyResolver,
                                                                     Action <string, ICommand, Exception> logError   = null,
                                                                     Action <string, ICommand, Exception> logWarning = null,
                                                                     Action <string, ICommand, Exception> logInfo    = null)
        {
            ICommandQueueProcessorLogger logger = new CommandQueueProcessorLogger(logWarning, logError, logInfo);

            dependencyResolver.RegisterInstance(logger);
            dependencyResolver.TypeMapping <IAsynchronousBackoffPolicyFactory, AsynchronousBackoffPolicyFactory>();
            dependencyResolver.TypeMapping <ICommandQueueProcessor, CommandQueueProcessor>();
            return(dependencyResolver);
        }
        /// <summary>
        /// Sets up azure storage command auditing for output to a queue. This is best suited for scenarios
        /// where there are multiple auditors or storage mechanisms in the audit pipeline as it enables
        /// execution of the command dispatch pipeline to rapidly continue but still with a guarantee
        /// that the command will be audited.
        ///
        /// Generally when configuring this auditor no other auditors are configured - but you can.
        /// </summary>
        /// <param name="dependencyResolver">The dependency resolver</param>
        /// <param name="queue">The queue to audit via</param>
        /// <param name="blobContainer">The blob container to store the payload to. If this is set then the
        /// payload is stored before the item is queued, if left null then the payload will be serialized
        /// into the queue item. The default setting of null is the more performant and common case, setting
        /// the container here is only useful for very large command payloads that won't fit inside a queue
        /// item. If the payload is stored in the blob container specified here then there will be no way
        /// for downstream auditors to access it from the AuditItem model - it will be null.
        /// </param>
        /// <param name="storageStrategy"></param>
        /// <returns></returns>
        public static ICommandingDependencyResolverAdapter AddAzureStorageCommandAuditing(this ICommandingDependencyResolverAdapter dependencyResolver,
                                                                                          CloudQueue queue,
                                                                                          CloudBlobContainer blobContainer   = null,
                                                                                          IStorageStrategy storageStrategy   = null,
                                                                                          AzureStorageAuditorOptions options = null)
        {
            options = options ?? new AzureStorageAuditorOptions();
            ICloudAuditQueueProvider cloudAuditQueueProvider = new CloudAuditQueueProvider(queue, null);
            ICloudAuditQueueBlobContainerProvider cloudAuditQueueBlobContainerProvider = new CloudAuditQueueBlobContainerProvider(blobContainer);

            dependencyResolver.RegisterInstance(cloudAuditQueueProvider);
            dependencyResolver.RegisterInstance(cloudAuditQueueBlobContainerProvider);
            dependencyResolver.TypeMapping <IAzureStorageQueueSerializer, AzureStorageQueueSerializer>();
            if (options.UsePreDispatchAuditor)
            {
                EnsureCommandingRuntime(dependencyResolver);
                dependencyResolver.AssociatedCommandingRuntime.AddPreDispatchCommandingAuditor <AzureStorageQueueCommandAuditor>(dependencyResolver, options.AuditPreDispatchRootOnly);
            }
            if (options.UsePostDispatchAuditor)
            {
                EnsureCommandingRuntime(dependencyResolver);
                dependencyResolver.AssociatedCommandingRuntime.AddPostDispatchCommandingAuditor <AzureStorageQueueCommandAuditor>(dependencyResolver, options.AuditPostDispatchRootOnly);
            }
            if (options.UseExecutionAuditor)
            {
                EnsureCommandingRuntime(dependencyResolver);
                dependencyResolver.AssociatedCommandingRuntime.AddExecutionCommandingAuditor <AzureStorageQueueCommandAuditor>(dependencyResolver, options.AuditExecuteDispatchRootOnly);
            }
            return(dependencyResolver);
        }
Example #7
0
        /// <summary>
        /// Sets up the cache with the specified cache key provider
        /// </summary>
        /// <param name="resolver">The dependency resolver</param>
        /// <param name="cacheKeyProvider">Instance of a cache key provider</param>
        /// <param name="replaceDefaultCommandDispatcher">If true then the default ICommandDispatcher will be replaced with the caching variant</param>
        /// <param name="options">Cache options</param>
        /// <returns>The dependency resolver</returns>
        public static ICommandingDependencyResolverAdapter AddCommandCache(
            this ICommandingDependencyResolverAdapter resolver,
            ICacheKeyProvider cacheKeyProvider,
            bool replaceDefaultCommandDispatcher,
            params CacheOptions[] options)
        {
            ICacheOptionsProvider cacheOptionsProvider = new CacheOptionsProvider(options);

            resolver.RegisterInstance(cacheOptionsProvider);
            if (replaceDefaultCommandDispatcher)
            {
                resolver.TypeMapping <ICommandDispatcher, CachedCommandDispatcher>();
            }
            else
            {
                resolver.TypeMapping <ICachedCommandDispatcher, CachedCommandDispatcher>();
            }
            resolver.RegisterInstance(cacheKeyProvider);

            return(resolver);
        }
Example #8
0
 /// <summary>
 /// Adds an audit item enricher
 /// </summary>
 /// <typeparam name="TAuditItemEnricher">The type of the enricher</typeparam>
 /// <param name="commandingDependencyResolver">The commanding dependency resolver</param>
 /// <returns>The commanding dependency resolver</returns>
 public ICommandingDependencyResolverAdapter AddAuditItemEnricher <TAuditItemEnricher>(ICommandingDependencyResolverAdapter commandingDependencyResolver)
     where TAuditItemEnricher : IAuditItemEnricher
 {
     lock (_auditItemEnricherPipelineLockObject)
     {
         if (_auditItemEnricherPipeline == null)
         {
             throw new AuditConfigurationException("The commanding system must be initialised with the UseCommanding method before any registering any audit item enrichers");
         }
         _auditItemEnricherPipeline.AddEnricher <TAuditItemEnricher>();
     }
     commandingDependencyResolver.TypeMapping <TAuditItemEnricher, TAuditItemEnricher>();
     return(commandingDependencyResolver);
 }
Example #9
0
 /// <summary>
 /// Registers an auditor that will be invoked directly after a command has been executed.
 /// </summary>
 /// <typeparam name="TExecutionAuditorImpl">The type of the auditor</typeparam>
 /// <param name="dependencyResolver">The dependency resolver</param>
 /// <param name="auditRootCommandOnly">By default the built in auditor will audit every command that is dispatched however if using the audit as part of an
 /// event sourcing pipeline it can be useful to only audit the root command and exclude any commands dispatched as a result
 /// of that root command. Set this property to true to audit only the root commands, leave null or set to false to audit all
 /// commands.</param>
 /// <returns>The dependency resolver</returns>
 public ICommandingDependencyResolverAdapter AddExecutionCommandingAuditor <TExecutionAuditorImpl>(
     ICommandingDependencyResolverAdapter dependencyResolver, bool auditRootCommandOnly = true) where TExecutionAuditorImpl : ICommandAuditor
 {
     lock (_auditorPipelineLockObject)
     {
         if (_auditorPipeline == null)
         {
             throw new AuditConfigurationException("The commanding system must be initialised with the UseCommanding method before any registering any auditors");
         }
         IAuditorRegistration registration = (IAuditorRegistration)_auditorPipeline;
         registration.RegisterExecutionAuditor <TExecutionAuditorImpl>(auditRootCommandOnly);
     }
     dependencyResolver.TypeMapping <TExecutionAuditorImpl, TExecutionAuditorImpl>();
     return(dependencyResolver);
 }
 public static ICommandingDependencyResolverAdapter AddAzureServiceBus(this ICommandingDependencyResolverAdapter dependencyResolver)
 {
     dependencyResolver.TypeMapping <IServiceBusMessageSerializer, JsonServiceBusMessageSerializer>();
     dependencyResolver.TypeMapping <IServiceBusCommandQueueProcessorFactory, ServiceBusCommandQueueProcessorFactory>();
     return(dependencyResolver);
 }
Example #11
0
        /// <summary>
        /// Registers the commanding system in an ioc container.
        /// If the container is not able to resolve unregistered types (for example the NetStandard Microsoft container) then
        /// the commandHandlerContainerRegistration should be used to perform the type registration for the handler
        /// </summary>
        /// <param name="dependencyResolver">The dependency resolver to register inside</param>
        /// <param name="options">Configuration options for the commanding system</param>
        /// <returns>The dependency resolver</returns>
        public ICommandRegistry AddCommanding(ICommandingDependencyResolverAdapter dependencyResolver,
                                              IOptions options = null)
        {
            options = options ?? new Options();

            dependencyResolver.AssociatedCommandingRuntime = this;

            ICommandHandlerExecuter commandHandlerExecuter = new CommandHandlerExecuter();

            dependencyResolver.RegisterInstance(commandHandlerExecuter);
            IOptionsProvider optionsProvider = new OptionsProvider(options);

            dependencyResolver.RegisterInstance(optionsProvider);

            // the registry is always shared, but vagaries of different IoC containers mean its dangerous to rely
            // on dependecy resolver checks for an existing registration
            lock (_registryLockObject)
            {
                if (_registry == null || options.Reset)
                {
                    Action <Type> resolverContainerRegistration = type => dependencyResolver.TypeMapping(type, type);
                    _registry = new CommandRegistry(commandHandlerExecuter, options.CommandHandlerContainerRegistration ?? resolverContainerRegistration);
                }
                dependencyResolver.RegisterInstance(_registry);
            }

            // the enricher is always shared, but vagaries of different IoC containers mean its dangerous to rely
            // on dependecy resolver checks for an existing registration
            lock (_enrichmentLockObject)
            {
                if (_dispatchContextEnrichment == null || options.Reset)
                {
                    _dispatchContextEnrichment = new CommandDispatchContextEnrichment(options.Enrichers ?? new List <ICommandDispatchContextEnricher>());
                }
                else if (options.Enrichers != null)
                {
                    _dispatchContextEnrichment.AddEnrichers(options.Enrichers);
                }
                dependencyResolver.RegisterInstance(_dispatchContextEnrichment);
            }

            lock (_auditItemEnricherPipelineLockObject)
            {
                if (_auditItemEnricherPipeline == null || options.Reset)
                {
                    _auditItemEnricherPipeline = new AuditItemEnricherPipeline(
                        options.AuditItemEnricherFactoryFunc ?? (type => (IAuditItemEnricher)dependencyResolver.Resolve(type)));
                }
                dependencyResolver.RegisterInstance(_auditItemEnricherPipeline);
            }

            lock (_auditorPipelineLockObject)
            {
                if (_auditorPipeline == null || options.Reset)
                {
                    _auditorPipeline = new CommandAuditPipeline(t => (ICommandAuditor)dependencyResolver.Resolve(t),
                                                                dependencyResolver.Resolve <ICommandAuditSerializer>,
                                                                _auditItemEnricherPipeline);
                }
                dependencyResolver.RegisterInstance(_auditorPipeline);
            }

            ICommandHandlerFactory commandHandlerFactory = new CommandHandlerFactory(options.CommandHandlerFactoryFunc ?? dependencyResolver.Resolve);

            IPipelineAwareCommandHandlerExecuter pipelineAwareCommandHandlerExecuter = new PipelineAwareCommandHandlerExecuter();

            dependencyResolver.RegisterInstance(commandHandlerFactory);
            dependencyResolver.RegisterInstance(pipelineAwareCommandHandlerExecuter);

            dependencyResolver.TypeMapping <ICommandAuditorFactory, NullCommandAuditorFactory>();
            dependencyResolver.TypeMapping <ICommandScopeManager, AsyncLocalCommandScopeManager>();
            dependencyResolver.TypeMapping <IFrameworkCommandDispatcher, CommandDispatcher>();
            dependencyResolver.TypeMapping <ICommandDispatcher, CommandDispatcher>();
            dependencyResolver.TypeMapping <IFrameworkCommandExecuter, CommandExecuter>();
            dependencyResolver.TypeMapping <ICommandExecuter, CommandExecuter>();
            dependencyResolver.TypeMapping <IDirectCommandExecuter, DirectCommandExecuter>();
            if (options.DisableCorrelationIds)
            {
                dependencyResolver.TypeMapping <ICommandCorrelationIdProvider, DisabledCorrelationIdProvider>();
            }
            else
            {
                if (options.UseLocallyUniqueCorrelationIds)
                {
                    dependencyResolver
                    .TypeMapping <ICommandCorrelationIdProvider, LocallyUniqueCommandCorrelationIdProvider>();
                }
                else
                {
                    dependencyResolver.TypeMapping <ICommandCorrelationIdProvider, CommandCorrelationIdProvider>();
                }
            }

            dependencyResolver.TypeMapping <ICommandAuditSerializer, CommandAuditSerializer>();
            dependencyResolver.TypeMapping(typeof(ICommandExecutionExceptionHandler), options.CommandExecutionExceptionHandler ?? typeof(DefaultCommandExecutionExceptionHandler));

            return(_registry);
        }
Example #12
0
        public static ICommandingDependencyResolverAdapter AddCommandMemoryCache(this ICommandingDependencyResolverAdapter resolver)
        {
            resolver.TypeMapping <ICacheAdapter, MemoryCacheAdapter>();

            return(resolver);
        }