private void RegisterSqlStore(ILogger logger)
        {
            var verifier = new ProviderVerifier();

            if (_configuration.SqlStore == null || string.IsNullOrEmpty(_configuration.SqlStore?.ConnectionString) || !verifier.IsSqlServiceRunning(_configuration.SqlStore?.ConnectionString, logger))
            {
                _autofacContainerBuilder.RegisterType <NullSqlStore>()
                .As <ISqlStore>()
                .SingleInstance();
                return;
            }

            _autofacContainerBuilder.Register(ctx =>
            {
                var transactionContext = AmbientTransactionContext.Current;
                var unitOfWork         = (UnitOfWork)transactionContext?.Items["UnitOfWork"];

                if (unitOfWork != null)
                {
                    return(unitOfWork.SqlStore);
                }

                return(new SqlStore(GetEntitiesInheritingFrom(typeof(IEntity <long>)), _configuration.SqlStore, false));
            })
            .As <ISqlStore>()
            .InstancePerLifetimeScope();
        }
        private void RegisterDocumentStore(ILogger logger)
        {
            var verifier = new ProviderVerifier();

            if (_configuration.DocumentStore == null || string.IsNullOrEmpty(_configuration.DocumentStore?.Url) || !verifier.IsNoSqlServiceRunning(_configuration.DocumentStore?.Url,
                                                                                                                                                   _configuration.DocumentStore?.Username,
                                                                                                                                                   _configuration.DocumentStore?.Password,
                                                                                                                                                   logger))
            {
                _autofacContainerBuilder.RegisterType <NullDocumentStore>()
                .As <IDocumentStore>()
                .SingleInstance();
                return;
            }

            var store = new Raven.Client.Documents.DocumentStore {
                Urls = new[] { _configuration.DocumentStore?.Url }
            };

            store.Initialize();

            _autofacContainerBuilder.RegisterInstance <Raven.Client.Documents.DocumentStore>(store)
            .As <Raven.Client.Documents.IDocumentStore>()
            .SingleInstance();

            _autofacContainerBuilder.Register(ctx =>
            {
                var transactionContext = AmbientTransactionContext.Current;
                var unitOfWork         = (UnitOfWork)transactionContext?.Items["UnitOfWork"];

                if (unitOfWork != null)
                {
                    return(unitOfWork.DocumentSessionFactory);
                }

                return(new DocumentSessionFactory(ctx.Resolve <Raven.Client.Documents.IDocumentStore>(), ctx.Resolve <ILogger>(), false));
            })
            .AsSelf()
            .InstancePerLifetimeScope();

            _autofacContainerBuilder.RegisterType <DocumentStore>()
            .As <IDocumentStore>()
            .InstancePerDependency();
        }
        private void RegisterGraphStore(ILogger logger)
        {
            var verifier = new ProviderVerifier();

            if (_configuration.GraphStore == null ||
                string.IsNullOrEmpty(_configuration.GraphStore?.Url) ||
                !verifier.IsGraphServiceRunning(_configuration.GraphStore?.Url,
                                                _configuration.GraphStore?.Username,
                                                _configuration.GraphStore?.Password,
                                                logger))
            {
                _autofacContainerBuilder.RegisterType <NullGraphStore>()
                .As <IGraphStore>()
                .SingleInstance();
                return;
            }

            _autofacContainerBuilder.Register(context => NeoServerConfiguration.GetConfiguration(new Uri($"{_configuration.GraphStore.Url}/db/data"),
                                                                                                 _configuration.GraphStore.Username,
                                                                                                 _configuration.GraphStore.Password))
            .SingleInstance();

            _autofacContainerBuilder.RegisterType <GraphClientFactory>()
            .As <IGraphClientFactory>()
            .SingleInstance();

            _autofacContainerBuilder.Register(ctx =>
            {
                var transactionContext = AmbientTransactionContext.Current;
                var unitOfWork         = (UnitOfWork)transactionContext?.Items["UnitOfWork"];

                return(unitOfWork != null ? unitOfWork.GraphStore :
                       new GraphStore(ctx.Resolve <IGraphClientFactory>().Create(), false));
            })
            .As <IGraphStore>()
            .InstancePerLifetimeScope();
        }
        private void RegisterBus(ILogger logger)
        {
            _autofacContainerBuilder.RegisterRebus((configurer, context) =>
            {
                var c = configurer
                        .Routing(r =>
                {
                    var busSettingsManager         = context.Resolve <IBusSettingsManager>();
                    var commands                   = GetEntitiesInheritingFrom(typeof(Command));
                    var registeredCommands         = new List <string>();
                    var routerConfigurationBuilder = r.TypeBased();

                    if (_configuration.Bus.AcceptableCommmandsOrEventsFromNamespacesStartingWith != null &&
                        _configuration.Bus.AcceptableCommmandsOrEventsFromNamespacesStartingWith.Any() &&
                        !_configuration.Bus.IsSendOnly)
                    {
                        var settings            = new CommandSettings();
                        var commandsInNamespace = GetEntitiesInheritingFrom(typeof(Command))
                                                  .Where(p => ContainsNamespace(p.FullName));

                        foreach (var command in commandsInNamespace)
                        {
                            settings.Settings.Add(new CommandSetting
                            {
                                CommandName = command.FullName,
                                Endpoint    = $"{_configuration.Bus.Name}"
                            });
                        }

                        busSettingsManager.UpdateCommandSettings(settings);
                    }

                    var commandSettings = busSettingsManager.GetCommandSettings() ?? new CommandSettings();

                    foreach (var command in commands)
                    {
                        if (registeredCommands.Contains(command.FullName))
                        {
                            continue;
                        }

                        registeredCommands.Add(command.FullName);
                        var endpoint =
                            commandSettings.Settings.FirstOrDefault(p => p.CommandName == command.FullName);

                        if (endpoint != null)
                        {
                            routerConfigurationBuilder = routerConfigurationBuilder.Map(command, endpoint.Endpoint);
                        }
                    }
                })
                        .Logging(l => l.Serilog(Log.Logger))
                        .Options(o =>
                {
                    var isDocumentStoreRegistered = context.IsRegistered <Raven.Client.Documents.IDocumentStore>();
                    var isSqlStoreRegistered      = context.IsRegistered <ISqlStore>() &&
                                                    !(context.Resolve <ISqlStore>() is NullSqlStore);
                    var isGraphStoreRegistered = context.IsRegistered <IGraphClientFactory>();

                    var unitOfWork = new UnitOfWorkFactory(GetEntitiesInheritingFrom(typeof(IEntity <long>)),
                                                           context.Resolve <ILogger>(),
                                                           isDocumentStoreRegistered ? context.Resolve <Raven.Client.Documents.IDocumentStore>() : null,
                                                           isSqlStoreRegistered ? _configuration.SqlStore : null,
                                                           isGraphStoreRegistered ? context.Resolve <IGraphClientFactory>() : null);

                    o.EnableUnitOfWork(unitOfWork.Create, unitOfWork.Commit, cleanupAction: unitOfWork.Cleanup);
                    o.EnableDataBus().StoreInFileSystem(_configuration.Bus.SharedFilePath);
                })
                        .Options(o =>
                {
                    o.SetNumberOfWorkers(2);
                    o.SetMaxParallelism(30);
                });

                var verifier = new ProviderVerifier();

                if (!string.IsNullOrEmpty(_configuration.Bus.Endpoint) && verifier.IsQueueRunning(_configuration.Bus.Endpoint, logger))
                {
                    c.Transport(t => t.UseRabbitMq(_configuration.Bus.Endpoint, _configuration.Bus.Name));
                }
                else
                {
                    c.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), _configuration.Bus.Name));
                }

                return(c);
            });


            _autofacContainerBuilder.RegisterInstance(_configuration.Bus);
            _autofacContainerBuilder.RegisterType <Bus>()
            .As <ICommandService>()
            .As <IEventBus>()
            .As <IQueryService>()
            .InstancePerLifetimeScope();

            _autofacContainerBuilder.RegisterType <BusSettingsManager>().As <IBusSettingsManager>().InstancePerDependency();
        }