Beispiel #1
0
        /// <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 ICommandingDependencyResolver UseEventHubCommandAuditing(this ICommandingDependencyResolver 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)
            {
                resolver.UsePreDispatchCommandingAuditor <AzureEventHubCommandAuditor>(options.AuditPreDispatchRootOnly);
            }
            if (options.UsePostDispatchAuditor)
            {
                resolver.UsePostDispatchCommandingAuditor <AzureEventHubCommandAuditor>(options.AuditPostDispatchRootOnly);
            }
            if (options.UseExecutionAuditor)
            {
                resolver.UseExecutionCommandingAuditor <AzureEventHubCommandAuditor>(options.AuditExecuteDispatchRootOnly);
            }
            return(resolver);
        }
 private static void EnsureRuntimeIsAssociated(ICommandingDependencyResolver resolver)
 {
     if (resolver.AssociatedCommandingRuntime == null)
     {
         throw new CommandFrameworkConfigurationException("The core commanding framework must be registered first.");
     }
 }
 private static void EnsureCommandingRuntime(ICommandingDependencyResolver dependencyResolver)
 {
     if (dependencyResolver.AssociatedCommandingRuntime == null)
     {
         throw new CommandFrameworkConfigurationException("The commanding package should be configured first");
     }
 }
Beispiel #4
0
        private static async Task <ICommandDispatcher> ConfigureEnqueue()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
            CloudQueue          auditQueue     = storageAccount.CreateCloudQueueClient().GetQueueReference("auditqueue");
            await auditQueue.CreateIfNotExistsAsync();

            IServiceCollection            serviceCollection  = new ServiceCollection();
            ICommandingDependencyResolver dependencyResolver = serviceCollection.GetCommandingDependencyResolver(() => _serviceProvider);

            IReadOnlyDictionary <string, object> Enricher(IReadOnlyDictionary <string, object> existing) => new Dictionary <string, object>
            {
                { "ExampleEnrichedCounter", Interlocked.Increment(ref _counter) }
            };
            Options options = new Options
            {
                Reset     = true, // we reset the registry because we allow repeat runs, in a normal app this isn't required
                Enrichers = new[] { new FunctionWrapperCommandDispatchContextEnricher(Enricher) }
            };

            dependencyResolver.UseCommanding(options)
            .Register <ChainCommandHandler>()
            .Register <OutputWorldToConsoleCommandHandler>();
            dependencyResolver.UseAzureStorageCommandAuditing(auditQueue);
            _serviceProvider = serviceCollection.BuildServiceProvider();
            return(_serviceProvider.GetService <ICommandDispatcher>());
        }
Beispiel #5
0
        public static ICommandingDependencyResolver UseCommandRedisCache(this ICommandingDependencyResolver resolver, string connectionString)
        {
            ICacheAdapter adapter = new RedisCacheAdapter(new Lazy <ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(connectionString)));

            resolver.RegisterInstance(adapter);
            return(resolver);
        }
Beispiel #6
0
        public static ICommandingDependencyResolver UseCommandRedisCache(this ICommandingDependencyResolver resolver, Lazy <ConnectionMultiplexer> multiplexer)
        {
            ICacheAdapter adapter = new RedisCacheAdapter(multiplexer);

            resolver.RegisterInstance(adapter);
            return(resolver);
        }
 private static ICommandingDependencyResolver Register <TSerializer>(this ICommandingDependencyResolver dependencyResolver) where TSerializer : IAzureStorageQueueSerializer
 {
     dependencyResolver.TypeMapping <IAzureStorageQueueSerializer, TSerializer>();
     dependencyResolver.TypeMapping <IAzureStorageCommandQueueProcessorFactory, AzureStorageCommandQueueProcessorFactory>();
     dependencyResolver.TypeMapping <IAzureStorageQueueDispatcherFactory, AzureStorageQueueDispatcherFactory>();
     return(dependencyResolver);
 }
        public static ICommandingDependencyResolver UseAzureStorageCommandAuditing(this ICommandingDependencyResolver 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.UsePreDispatchCommandingAuditor <AzureStorageQueueCommandAuditor>(dependencyResolver, options.AuditPreDispatchRootOnly);
            }
            if (options.UsePostDispatchAuditor)
            {
                EnsureCommandingRuntime(dependencyResolver);
                dependencyResolver.AssociatedCommandingRuntime.UsePostDispatchCommandingAuditor <AzureStorageQueueCommandAuditor>(dependencyResolver, options.AuditPostDispatchRootOnly);
            }
            if (options.UseExecutionAuditor)
            {
                EnsureCommandingRuntime(dependencyResolver);
                dependencyResolver.AssociatedCommandingRuntime.UseExecutionCommandingAuditor <AzureStorageQueueCommandAuditor>(dependencyResolver, options.AuditExecuteDispatchRootOnly);
            }
            return(dependencyResolver);
        }
Beispiel #9
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="commandHandlerContainerRegistration">
 /// Unless an alternative implementation of ICommandHandlerFactory is supplied then actors are created through the dependency resolver
 /// but not all IoC containers can resolve unregistered concrete types (for example the built in ASP.Net Core IServiceCollection
 /// and IServiceProvider IoC cannot). Where this is the case supply an implementation for the CommandHandlerContainerRegistration
 /// action that registers the actors in the container. For example using an IServiceCollection instance of serviceCollection:
 ///     resolver.UseCommanding(type => services.AddTransient(type, type));
 /// </param>
 /// <returns>The dependency resolver</returns>
 public static ICommandRegistry UseCommanding(this ICommandingDependencyResolver dependencyResolver,
                                              Action <Type> commandHandlerContainerRegistration)
 {
     return(Instance.UseCommanding(dependencyResolver,
                                   new Options {
         CommandHandlerContainerRegistration = commandHandlerContainerRegistration
     }));
 }
Beispiel #10
0
 /// <summary>
 /// Registers a command auditor that writes to an event hub
 /// </summary>
 /// <param name="resolver">Dependency resolver</param>
 /// <param name="connectionString">Connection string to an event hub. This needs to also supply the EntityPath e.g.:
 /// Endpoint=sb://myeventhub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=mysharedaccesskey;EntityPath=myeventhub
 /// </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 ICommandingDependencyResolver UseEventHubCommandAuditing(this ICommandingDependencyResolver resolver,
                                                                        string connectionString,
                                                                        IPartitionKeyProvider partitionKeyProvider = null,
                                                                        AzureEventHubAuditorOptions options        = null)
 {
     Microsoft.Azure.EventHubs.EventHubClient client = Microsoft.Azure.EventHubs.EventHubClient.CreateFromConnectionString(connectionString);
     return(UseEventHubCommandAuditing(resolver, client, partitionKeyProvider, options));
 }
Beispiel #11
0
 private static ICommandingDependencyResolver Register <TSerializer>(ICommandingDependencyResolver 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, HttpCommandDispatcherFactory>();
     return(dependencyResolver);
 }
 public static IServiceCollection UseCoreCommanding(this IServiceCollection serviceCollection,
                                                    ICommandingDependencyResolver commandingDependencyResolver)
 {
     serviceCollection.AddSingleton <IMetricCollector>(new MetricCollector());
     commandingDependencyResolver
     .UsePreDispatchCommandingAuditor <LoggingCommandPreDispatchAuditor>()
     .UseExecutionCommandingAuditor <LoggingCommandExecutionAuditor>()
     .UseAuditItemEnricher <AuditItemUserIdEnricher>();
     return(serviceCollection);
 }
        public static ICommandingDependencyResolver UseQueues(this ICommandingDependencyResolver 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);
        }
Beispiel #14
0
        /// <summary>
        /// Registers a command auditor that writes to an event hub
        /// </summary>
        /// <param name="resolver">Dependency resolver</param>
        /// <param name="connectionString">Connection string to an event hub e.g.:
        /// Endpoint=sb://myeventhub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=mysharedaccesskey
        /// </param>
        /// <param name="entityPath">The path to the event hub (usually just the event hub name</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 ICommandingDependencyResolver UseEventHubCommandAuditing(this ICommandingDependencyResolver resolver,
                                                                               string connectionString,
                                                                               string entityPath,
                                                                               IPartitionKeyProvider partitionKeyProvider = null,
                                                                               AzureEventHubAuditorOptions options        = null)
        {
            EventHubsConnectionStringBuilder builder = new EventHubsConnectionStringBuilder(connectionString);

            builder.EntityPath = entityPath;
            Microsoft.Azure.EventHubs.EventHubClient client = Microsoft.Azure.EventHubs.EventHubClient.CreateFromConnectionString(builder.ToString());
            return(UseEventHubCommandAuditing(resolver, client, partitionKeyProvider, options));
        }
Beispiel #15
0
 public ICommandingDependencyResolver UseExecutionCommandingAuditor <TExecutionAuditorImpl>(
     ICommandingDependencyResolver 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 ICommandingDependencyResolver UseAzureStorageCommandAuditing(this ICommandingDependencyResolver dependencyResolver,
                                                                                   CloudStorageAccount cloudStorageAccount,
                                                                                   CloudBlobContainer commandPayloadContainer = null,
                                                                                   IStorageStrategy storageStrategy           = null,
                                                                                   AzureStorageAuditorOptions options         = null)
        {
            options = options ?? new AzureStorageAuditorOptions();

            if (!options.UseExecutionAuditor && !options.UsePostDispatchAuditor && !options.UsePreDispatchAuditor)
            {
                throw new AzureStorageConfigurationException("At least one auditor type must be configured");
            }
            CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient();

            if (commandPayloadContainer == null)
            {
                commandPayloadContainer = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("commandauditpayload");
            }
            if (storageStrategy == null)
            {
                storageStrategy = new SingleTableStrategy();
            }

            ICloudStorageProvider cloudStorageProvider = new CloudStorageProvider(cloudTableClient, commandPayloadContainer);

            dependencyResolver.RegisterInstance(cloudStorageProvider);
            dependencyResolver.RegisterInstance(storageStrategy);
            if (options.UsePreDispatchAuditor)
            {
                EnsureCommandingRuntime(dependencyResolver);
                dependencyResolver.AssociatedCommandingRuntime.UsePreDispatchCommandingAuditor <AzureStorageTableCommandAuditor>(dependencyResolver, options.AuditPreDispatchRootOnly);
            }
            if (options.UsePostDispatchAuditor)
            {
                EnsureCommandingRuntime(dependencyResolver);
                dependencyResolver.AssociatedCommandingRuntime.UsePostDispatchCommandingAuditor <AzureStorageTableCommandAuditor>(dependencyResolver, options.AuditPostDispatchRootOnly);
            }
            if (options.UseExecutionAuditor)
            {
                EnsureCommandingRuntime(dependencyResolver);
                dependencyResolver.AssociatedCommandingRuntime.UseExecutionCommandingAuditor <AzureStorageTableCommandAuditor>(dependencyResolver, options.AuditExecuteDispatchRootOnly);
            }

            return(dependencyResolver);
        }
Beispiel #17
0
        private static async Task <IAzureStorageAuditQueueProcessorFactory> ConfigureDequeue(IStorageStrategy storageStrategy)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
            CloudQueue          auditQueue     = storageAccount.CreateCloudQueueClient().GetQueueReference("auditqueue");
            await auditQueue.CreateIfNotExistsAsync();

            IServiceCollection            serviceCollection = new ServiceCollection();
            ICommandingDependencyResolver resolver          = serviceCollection.GetCommandingDependencyResolver(() => _serviceProvider);
            Options options = new Options
            {
                Reset = true // we reset the registry because we allow repeat runs, in a normal app this isn't required
            };

            resolver.UseCommanding(options);
            resolver.UseQueues();
            resolver.UseAzureStorageCommandAuditing(storageAccount, storageStrategy: storageStrategy); // this sets up the table store auditors
            resolver.UseAzureStorageAuditQueueProcessor(auditQueue);                                   // this sets up queue listening
            _serviceProvider = serviceCollection.BuildServiceProvider();

            return(_serviceProvider.GetService <IAzureStorageAuditQueueProcessorFactory>());
        }
        /// <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 ICommandingDependencyResolver UseCommandCache(
            this ICommandingDependencyResolver 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);
        }
Beispiel #19
0
        private static ICommandDispatcher Configure()
        {
            IServiceCollection            serviceCollection  = new ServiceCollection();
            ICommandingDependencyResolver dependencyResolver = serviceCollection.GetCommandingDependencyResolver(() => _serviceProvider);

            IReadOnlyDictionary <string, object> Enricher(IReadOnlyDictionary <string, object> existing) => new Dictionary <string, object>
            {
                { "ExampleEnrichedCounter", Interlocked.Increment(ref _counter) }
            };
            Options options = new Options
            {
                Reset     = true, // we reset the registry because we allow repeat runs, in a normal app this isn't required
                Enrichers = new[] { new FunctionWrapperCommandDispatchContextEnricher(Enricher) }
            };

            dependencyResolver.UseCommanding(options)
            .Register <ChainCommandHandler>()
            .Register <OutputWorldToConsoleCommandHandler>();
            dependencyResolver.UseEventHubCommandAuditing(EventHubConnectionString, EventHubName);
            _serviceProvider = serviceCollection.BuildServiceProvider();
            return(_serviceProvider.GetService <ICommandDispatcher>());
        }
Beispiel #20
0
 public static ICommandingDependencyResolver UseAuditItemEnricher <TAuditItemEnricher>(this ICommandingDependencyResolver commandingDependencyResolver)
     where TAuditItemEnricher : IAuditItemEnricher
 {
     return(Instance.UseAuditItemEnricher <TAuditItemEnricher>(commandingDependencyResolver));
 }
Beispiel #21
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 static ICommandingDependencyResolver UseExecutionCommandingAuditor <TExecutionAuditorImpl>(
     this ICommandingDependencyResolver dependencyResolver, bool auditRootCommandOnly = true) where TExecutionAuditorImpl : ICommandAuditor
 {
     return(Instance.UseExecutionCommandingAuditor <TExecutionAuditorImpl>(dependencyResolver, auditRootCommandOnly));
 }
Beispiel #22
0
 /// <summary>
 /// Registers an auditor that will be invoked directly after a command has been dispatched.
 /// </summary>
 /// <typeparam name="TDispatchAuditorImpl">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 static ICommandingDependencyResolver UsePostDispatchCommandingAuditor <TDispatchAuditorImpl>(
     this ICommandingDependencyResolver dependencyResolver, bool auditRootCommandOnly = true) where TDispatchAuditorImpl : ICommandAuditor
 {
     return(Instance.UsePostDispatchCommandingAuditor <TDispatchAuditorImpl>(dependencyResolver, auditRootCommandOnly));
 }
Beispiel #23
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 static ICommandRegistry UseCommanding(this ICommandingDependencyResolver dependencyResolver,
                                              Options options = null)
 {
     return(Instance.UseCommanding(dependencyResolver, options));
 }
        /// <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="options">Cache options</param>
        /// <returns>The dependency resolver</returns>
        public static ICommandingDependencyResolver UseCommandCache(this ICommandingDependencyResolver resolver, ICacheKeyProvider cacheKeyProvider, params CacheOptions[] options)
        {
            return(UseCommandCache(resolver, cacheKeyProvider, false, options));

            return(resolver);
        }
 /// <summary>
 /// Sets up the cache with the default cache key provider that uses the command type, property names and property values to
 /// generate a hashable string
 /// </summary>
 /// <param name="resolver">The dependency resolver</param>
 /// <param name="options">Cache options</param>
 /// <param name="replaceDefaultCommandDispatcher">If true then the default ICommandDispatcher will be replaced with the caching variant</param>
 /// <returns>The dependency resolver</returns>
 public static ICommandingDependencyResolver UseCommandCache(this ICommandingDependencyResolver resolver, bool replaceDefaultCommandDispatcher, params CacheOptions[] options)
 {
     return(UseCommandCache(resolver, new PropertyCacheKeyProvider(new PropertyCacheKeyProviderCompiler(), new SimpleCacheKeyHash()), replaceDefaultCommandDispatcher, options));
 }
Beispiel #26
0
        public static ICommandingDependencyResolver UseCommandMemoryCache(this ICommandingDependencyResolver resolver)
        {
            resolver.TypeMapping <ICacheAdapter, MemoryCacheAdapter>();

            return(resolver);
        }
Beispiel #27
0
        public ICommandRegistry UseCommanding(ICommandingDependencyResolver 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);
        }
Beispiel #28
0
 public static ICommandingDependencyResolver UseHttpCommanding <TSerializer>(this ICommandingDependencyResolver dependencyResolver, HttpClient client = null) where TSerializer : IHttpCommandSerializer
 {
     Register <TSerializer>(dependencyResolver, client);
     return(dependencyResolver);
 }
        public static ICommandingDependencyResolver UseMicrosoftLoggingExtensionsAuditor(this ICommandingDependencyResolver resolver,
                                                                                         LogLevel normalLogLevel           = LogLevel.Trace,
                                                                                         LogLevel executionFailureLogLevel = LogLevel.Warning,
                                                                                         MicrosoftLoggingExtensionsAuditorOptions options = null)
        {
            options = options ?? new MicrosoftLoggingExtensionsAuditorOptions();
            ILogLevelProvider logLevelProvider = new LogLevelProvider(normalLogLevel, executionFailureLogLevel);

            resolver.RegisterInstance(logLevelProvider);

            if (options.UsePreDispatchAuditor)
            {
                resolver.UsePreDispatchCommandingAuditor <LoggerCommandAuditor>(options.AuditPreDispatchRootOnly);
            }
            if (options.UsePostDispatchAuditor)
            {
                resolver.UsePostDispatchCommandingAuditor <LoggerCommandAuditor>(options.AuditPostDispatchRootOnly);
            }
            if (options.UsePreDispatchAuditor)
            {
                resolver.UseExecutionCommandingAuditor <LoggerCommandAuditor>(options.AuditExecuteDispatchRootOnly);
            }

            return(resolver);
        }
Beispiel #30
0
 public static ICommandingDependencyResolver UseHttpCommanding(this ICommandingDependencyResolver dependencyResolver, HttpClient client = null)
 {
     Register <JsonCommandSerializer>(dependencyResolver, client);
     return(dependencyResolver);
 }