protected new static TSingleton GetSingleton([NotNull] TKey key)
        {
            TSingleton singleton = Singletons.GetOrAdd(key, k => Constructor(k));

            Debug.Assert(singleton != null);

            // Use optimistic locking to see if we're initialised
            // Most of the time we will be initialised so we won't get a lock.
            if (!singleton.IsInitialised)
            {
                lock (singleton._initialisationLock)
                {
                    // Once we have a lock, we need to re-check, incase a different thread
                    // won the race to initialise.
                    if (!singleton.IsInitialised)
                    {
                        // We're the first thread with a lock so initialise.
                        singleton.Initialise();
                    }

                    // We are now initialised (so long as an exception isn't thrown).
                    singleton.IsInitialised = true;
                }
            }
            return(singleton);
        }
Beispiel #2
0
        public static IServiceCollection AddMyDataProtectection(this IServiceCollection services, IConfiguration config)
        {
            var dataProtection = services.AddDataProtection().SetApplicationName("Squidex");

            config.ConfigureByOption("identity:keysStore:type", new Options
            {
                ["Redis"] = () =>
                {
                    var redisConfiguration = config.GetRequiredValue("identity:keysStore:redis:configuration");

                    var connectionMultiplexer = Singletons <ConnectionMultiplexer> .GetOrAdd(redisConfiguration, s => ConnectionMultiplexer.Connect(s));

                    dataProtection.PersistKeysToRedis(connectionMultiplexer);
                },
                ["Folder"] = () =>
                {
                    var folderPath = config.GetRequiredValue("identity:keysStore:folder:path");

                    dataProtection.PersistKeysToFileSystem(new DirectoryInfo(folderPath));
                },
                ["InMemory"] = () => { }
            });

            return(services);
        }
Beispiel #3
0
        public static void AddMyEventStoreServices(this IServiceCollection services, IConfiguration config)
        {
            config.ConfigureByOption("eventStore:type", new Options
            {
                ["MongoDb"] = () =>
                {
                    var mongoConfiguration = config.GetRequiredValue("eventStore:mongoDb:configuration");
                    var mongoDatabaseName  = config.GetRequiredValue("eventStore:mongoDb:database");

                    services.AddSingletonAs(c =>
                    {
                        var mongoClient  = Singletons <IMongoClient> .GetOrAdd(mongoConfiguration, s => new MongoClient(s));
                        var mongDatabase = mongoClient.GetDatabase(mongoDatabaseName);

                        return(new MongoEventStore(mongDatabase, c.GetRequiredService <IEventNotifier>()));
                    })
                    .As <IExternalSystem>()
                    .As <IEventStore>();
                },
                ["GetEventStore"] = () =>
                {
                    var eventStoreConfiguration  = config.GetRequiredValue("eventStore:getEventStore:configuration");
                    var eventStoreProjectionHost = config.GetRequiredValue("eventStore:getEventStore:projectionHost");
                    var eventStorePrefix         = config.GetValue <string>("eventStore:getEventStore:prefix");

                    var connection = EventStoreConnection.Create(eventStoreConfiguration);

                    services.AddSingletonAs(c => new GetEventStore(connection, eventStorePrefix, eventStoreProjectionHost))
                    .As <IExternalSystem>()
                    .As <IEventStore>();
                }
            });
        }
        public static void AddMyAssetServices(this IServiceCollection services, IConfiguration config)
        {
            config.ConfigureByOption("assetStore:type", new Alternatives
            {
                ["Default"] = () =>
                {
                    services.AddSingletonAs <NoopAssetStore>()
                    .AsOptional <IAssetStore>();
                },
                ["Folder"] = () =>
                {
                    var path = config.GetRequiredValue("assetStore:folder:path");

                    services.AddSingletonAs(c => new FolderAssetStore(path, c.GetRequiredService <ISemanticLog>()))
                    .As <IAssetStore>();
                },
                ["GoogleCloud"] = () =>
                {
                    var bucketName = config.GetRequiredValue("assetStore:googleCloud:bucket");

                    services.AddSingletonAs(c => new GoogleCloudAssetStore(bucketName))
                    .As <IAssetStore>();
                },
                ["AzureBlob"] = () =>
                {
                    var connectionString = config.GetRequiredValue("assetStore:azureBlob:connectionString");
                    var containerName    = config.GetRequiredValue("assetStore:azureBlob:containerName");

                    services.AddSingletonAs(c => new AzureBlobAssetStore(connectionString, containerName))
                    .As <IAssetStore>();
                },
                ["MongoDb"] = () =>
                {
                    var mongoConfiguration    = config.GetRequiredValue("assetStore:mongoDb:configuration");
                    var mongoDatabaseName     = config.GetRequiredValue("assetStore:mongoDb:database");
                    var mongoGridFsBucketName = config.GetRequiredValue("assetStore:mongoDb:bucket");

                    services.AddSingletonAs(c =>
                    {
                        var mongoClient   = Singletons <IMongoClient> .GetOrAdd(mongoConfiguration, s => new MongoClient(s));
                        var mongoDatabase = mongoClient.GetDatabase(mongoDatabaseName);

                        var gridFsbucket = new GridFSBucket <string>(mongoDatabase, new GridFSBucketOptions
                        {
                            BucketName = mongoGridFsBucketName
                        });

                        return(new MongoGridFsAssetStore(gridFsbucket));
                    })
                    .As <IAssetStore>();
                }
            });

            services.AddSingletonAs <ImageSharpAssetThumbnailGenerator>()
            .As <IAssetThumbnailGenerator>();
        }
        public static void AddMyEventStoreServices(this IServiceCollection services, IConfiguration config)
        {
            config.ConfigureByOption("eventStore:type", new Options
            {
                ["MongoDb"] = () =>
                {
                    var mongoConfiguration = config.GetRequiredValue("eventStore:mongoDb:configuration");
                    var mongoDatabaseName  = config.GetRequiredValue("eventStore:mongoDb:database");

                    services.AddSingletonAs(c =>
                    {
                        var mongoClient  = Singletons <IMongoClient> .GetOrAdd(mongoConfiguration, s => new MongoClient(s));
                        var mongDatabase = mongoClient.GetDatabase(mongoDatabaseName);

                        return(new MongoEventStore(mongDatabase, c.GetRequiredService <IEventNotifier>()));
                    })
                    .As <IInitializable>()
                    .As <IEventStore>();
                },
                ["GetEventStore"] = () =>
                {
                    var eventStoreConfiguration  = config.GetRequiredValue("eventStore:getEventStore:configuration");
                    var eventStoreProjectionHost = config.GetRequiredValue("eventStore:getEventStore:projectionHost");
                    var eventStorePrefix         = config.GetValue <string>("eventStore:getEventStore:prefix");

                    var connection = EventStoreConnection.Create(eventStoreConfiguration);

                    services.AddSingletonAs(connection)
                    .As <IEventStoreConnection>();

                    services.AddSingletonAs(c => new GetEventStore(connection, c.GetRequiredService <IJsonSerializer>(), eventStorePrefix, eventStoreProjectionHost))
                    .As <IEventStore>();

                    services.AddHealthChecks()
                    .AddCheck <GetEventStoreHealthCheck>("EventStore", tags: new[] { "node" });
                }
            });

            services.AddSingletonAs <OrleansEventNotifier>()
            .As <IEventNotifier>();

            services.AddSingletonAs <DefaultStreamNameResolver>()
            .As <IStreamNameResolver>();

            services.AddSingletonAs <DefaultEventDataFormatter>()
            .As <IEventDataFormatter>();

            services.AddSingletonAs(c =>
            {
                var allEventConsumers = c.GetServices <IEventConsumer>();

                return(new EventConsumerFactory(n => allEventConsumers.FirstOrDefault(x => x.Name == n)));
            });
        }
Beispiel #6
0
        protected override void Load(ContainerBuilder builder)
        {
            var consumeEvents = Configuration.GetValue <bool>("eventStore:consume");

            if (consumeEvents)
            {
                builder.RegisterType <EventReceiver>()
                .AsSelf()
                .InstancePerDependency();
            }

            var eventStoreType = Configuration.GetValue <string>("eventStore:type");

            if (string.IsNullOrWhiteSpace(eventStoreType))
            {
                throw new ConfigurationException("Configure EventStore type with 'eventStore:type'.");
            }

            if (string.Equals(eventStoreType, "MongoDb", StringComparison.OrdinalIgnoreCase))
            {
                var configuration = Configuration.GetValue <string>("eventStore:mongoDb:configuration");

                if (string.IsNullOrWhiteSpace(configuration))
                {
                    throw new ConfigurationException("Configure EventStore MongoDb configuration with 'eventStore:mongoDb:configuration'.");
                }

                var database = Configuration.GetValue <string>("eventStore:mongoDb:database");

                if (string.IsNullOrWhiteSpace(database))
                {
                    throw new ConfigurationException("Configure EventStore MongoDb Database name with 'eventStore:mongoDb:database'.");
                }

                builder.Register(c => Singletons <IMongoClient> .GetOrAdd(configuration, s => new MongoClient(s)))
                .Named <IMongoClient>(MongoClientRegistration)
                .SingleInstance();

                builder.Register(c => c.ResolveNamed <IMongoClient>(MongoClientRegistration).GetDatabase(database))
                .Named <IMongoDatabase>(MongoDatabaseRegistration)
                .SingleInstance();

                builder.RegisterType <MongoEventStore>()
                .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
                .As <IExternalSystem>()
                .As <IEventStore>()
                .SingleInstance();
            }
            else
            {
                throw new ConfigurationException($"Unsupported value '{eventStoreType}' for 'eventStore:type', supported: MongoDb.");
            }
        }
Beispiel #7
0
        public static void AddSquidexEventSourcing(this IServiceCollection services, IConfiguration config)
        {
            config.ConfigureByOption("eventStore:type", new Alternatives
            {
                ["MongoDb"] = () =>
                {
                    var mongoConfiguration = config.GetRequiredValue("eventStore:mongoDb:configuration");
                    var mongoDatabaseName  = config.GetRequiredValue("eventStore:mongoDb:database");

                    services.AddSingletonAs(c =>
                    {
                        var mongoClient  = Singletons <IMongoClient> .GetOrAdd(mongoConfiguration, s => new MongoClient(s));
                        var mongDatabase = mongoClient.GetDatabase(mongoDatabaseName);

                        return(new MongoEventStore(mongDatabase, c.GetRequiredService <IEventNotifier>()));
                    })
                    .As <IEventStore>();
                },
                ["GetEventStore"] = () =>
                {
                    var configuration = config.GetRequiredValue("eventStore:getEventStore:configuration");

                    services.AddSingletonAs(_ => EventStoreClientSettings.Create(configuration))
                    .AsSelf();

                    services.AddSingletonAs <GetEventStore>()
                    .As <IEventStore>();

                    services.AddHealthChecks()
                    .AddCheck <GetEventStoreHealthCheck>("EventStore", tags: new[] { "node" });
                }
            });

            services.AddSingletonAs <OrleansEventNotifier>()
            .As <IEventNotifier>();

            services.AddTransientAs <Rebuilder>()
            .AsSelf();

            services.AddSingletonAs <DefaultStreamNameResolver>()
            .As <IStreamNameResolver>();

            services.AddSingletonAs <DefaultEventDataFormatter>()
            .As <IEventDataFormatter>();

            services.AddSingletonAs(c =>
            {
                var allEventConsumers = c.GetServices <IEventConsumer>();

                return(new EventConsumerFactory(n => allEventConsumers.First(x => x.Name == n)));
            });
        }
Beispiel #8
0
        public static IServiceCollection AddMyDataProtectection(this IServiceCollection services, IConfiguration configuration)
        {
            var dataProtection = services.AddDataProtection().SetApplicationName("Squidex");

            var keyStoreType = configuration.GetValue <string>("identity:keysStore:type");

            if (string.IsNullOrWhiteSpace(keyStoreType))
            {
                throw new ConfigurationException("Configure KeyStore type with 'identity:keysStore:type'.");
            }

            if (string.Equals(keyStoreType, "Redis", StringComparison.OrdinalIgnoreCase))
            {
                var redisConfiguration = configuration.GetValue <string>("identity:keysStore:redis:configuration");

                if (string.IsNullOrWhiteSpace(redisConfiguration))
                {
                    throw new ConfigurationException("Configure KeyStore Redis configuration with 'identity:keysStore:redis:configuration'.");
                }

                var connectionMultiplexer = Singletons <ConnectionMultiplexer> .GetOrAdd(redisConfiguration, s => ConnectionMultiplexer.Connect(s));

                dataProtection.PersistKeysToRedis(connectionMultiplexer);
            }
            else if (string.Equals(keyStoreType, "Folder", StringComparison.OrdinalIgnoreCase))
            {
                var folderPath = configuration.GetValue <string>("identity:keysStore:folder:path");

                if (string.IsNullOrWhiteSpace(folderPath))
                {
                    throw new ConfigurationException("Configure KeyStore Folder path with 'identity:keysStore:folder:path'.");
                }

                dataProtection.PersistKeysToFileSystem(new DirectoryInfo(folderPath));
            }
            else if (!string.Equals(keyStoreType, "InMemory", StringComparison.OrdinalIgnoreCase))
            {
                throw new ConfigurationException($"Unsupported value '{keyStoreType}' for 'identity:keysStore:type', supported: Redis, Folder, InMemory.");
            }

            return(services);
        }
Beispiel #9
0
 private static IMongoClient GetClient(string configuration)
 {
     return(Singletons <IMongoClient> .GetOrAdd(configuration, s => new MongoClient(s)));
 }
Beispiel #10
0
        public static void AddMyStoreServices(this IServiceCollection services, IConfiguration config)
        {
            config.ConfigureByOption("store:type", new Options
            {
                ["MongoDB"] = () =>
                {
                    BsonJsonConvention.Register(SerializationServices.DefaultJsonSerializer);

                    var mongoConfiguration       = config.GetRequiredValue("store:mongoDb:configuration");
                    var mongoDatabaseName        = config.GetRequiredValue("store:mongoDb:database");
                    var mongoContentDatabaseName = config.GetOptionalValue("store:mongoDb:contentDatabase", mongoDatabaseName);

                    var mongoClient          = Singletons <IMongoClient> .GetOrAdd(mongoConfiguration, s => new MongoClient(s));
                    var mongoDatabase        = mongoClient.GetDatabase(mongoDatabaseName);
                    var mongoContentDatabase = mongoClient.GetDatabase(mongoContentDatabaseName);

                    services.AddSingleton(typeof(ISnapshotStore <,>), typeof(MongoSnapshotStore <,>));

                    services.AddSingletonAs(mongoDatabase)
                    .As <IMongoDatabase>();

                    services.AddSingletonAs <MongoXmlRepository>()
                    .As <IXmlRepository>()
                    .As <IInitializable>();

                    services.AddSingletonAs <MongoMigrationStatus>()
                    .As <IMigrationStatus>()
                    .As <IInitializable>();

                    services.AddSingletonAs <MongoPersistedGrantStore>()
                    .As <IPersistedGrantStore>()
                    .As <IInitializable>();

                    services.AddSingletonAs <MongoUsageStore>()
                    .As <IUsageStore>()
                    .As <IInitializable>();

                    services.AddSingletonAs <MongoRuleEventRepository>()
                    .As <IRuleEventRepository>()
                    .As <IInitializable>();

                    services.AddSingletonAs <MongoUserStore>()
                    .As <IUserStore <IUser> >()
                    .As <IUserFactory>()
                    .As <IUserResolver>()
                    .As <IInitializable>();

                    services.AddSingletonAs <MongoRoleStore>()
                    .As <IRoleStore <IRole> >()
                    .As <IRoleFactory>()
                    .As <IInitializable>();

                    services.AddSingletonAs <MongoHistoryEventRepository>()
                    .As <IHistoryEventRepository>()
                    .As <IEventConsumer>()
                    .As <IInitializable>();

                    services.AddSingletonAs <MongoAssetStatsRepository>()
                    .As <IAssetStatsRepository>()
                    .As <IEventConsumer>()
                    .As <IInitializable>();

                    services.AddSingletonAs <MongoAssetRepository>()
                    .As <IAssetRepository>()
                    .As <ISnapshotStore <AssetState, Guid> >()
                    .As <IInitializable>();

                    services.AddSingletonAs(c => new MongoContentRepository(mongoContentDatabase, c.GetService <IAppProvider>()))
                    .As <IContentRepository>()
                    .As <ISnapshotStore <ContentState, Guid> >()
                    .As <IEventConsumer>()
                    .As <IInitializable>();

                    services.AddTransientAs <ConvertOldSnapshotStores>()
                    .As <IMigration>();

                    services.AddTransientAs(c => new DeleteContentCollections(mongoContentDatabase))
                    .As <IMigration>();
                }
            });

            services.AddSingleton(typeof(IStore <>), typeof(Store <>));
        }
        public static void AddSquidexStoreServices(this IServiceCollection services, IConfiguration config)
        {
            config.ConfigureByOption("store:type", new Alternatives
            {
                ["MongoDB"] = () =>
                {
                    var mongoConfiguration       = config.GetRequiredValue("store:mongoDb:configuration");
                    var mongoDatabaseName        = config.GetRequiredValue("store:mongoDb:database");
                    var mongoContentDatabaseName = config.GetOptionalValue("store:mongoDb:contentDatabase", mongoDatabaseName);

                    services.AddSingleton(typeof(ISnapshotStore <,>), typeof(MongoSnapshotStore <,>));

                    services.AddSingletonAs(_ => Singletons <IMongoClient> .GetOrAdd(mongoConfiguration, s => new MongoClient(s)))
                    .As <IMongoClient>();

                    services.AddSingletonAs(c => c.GetRequiredService <IMongoClient>().GetDatabase(mongoDatabaseName))
                    .As <IMongoDatabase>();

                    services.AddTransientAs(c => new DeleteContentCollections(c.GetRequiredService <IMongoClient>().GetDatabase(mongoContentDatabaseName)))
                    .As <IMigration>();

                    services.AddTransientAs(c => new RestructureContentCollection(c.GetRequiredService <IMongoClient>().GetDatabase(mongoContentDatabaseName)))
                    .As <IMigration>();

                    services.AddSingletonAs <MongoMigrationStatus>()
                    .As <IMigrationStatus>();

                    services.AddTransientAs <ConvertOldSnapshotStores>()
                    .As <IMigration>();

                    services.AddTransientAs <ConvertRuleEventsJson>()
                    .As <IMigration>();

                    services.AddTransientAs <RenameAssetSlugField>()
                    .As <IMigration>();

                    services.AddTransientAs <RenameAssetMetadata>()
                    .As <IMigration>();

                    services.AddHealthChecks()
                    .AddCheck <MongoDBHealthCheck>("MongoDB", tags: new[] { "node" });

                    services.AddSingletonAs <MongoRequestLogRepository>()
                    .As <IRequestLogRepository>();

                    services.AddSingletonAs <MongoUsageRepository>()
                    .As <IUsageRepository>();

                    services.AddSingletonAs <MongoRuleEventRepository>()
                    .As <IRuleEventRepository>();

                    services.AddSingletonAs <MongoHistoryEventRepository>()
                    .As <IHistoryEventRepository>();

                    services.AddSingletonAs <MongoRoleStore>()
                    .As <IRoleStore <IdentityRole> >();

                    services.AddSingletonAs <MongoUserStore>()
                    .As <IUserStore <IdentityUser> >().As <IUserFactory>();

                    services.AddSingletonAs <MongoAssetRepository>()
                    .As <IAssetRepository>().As <ISnapshotStore <AssetState, Guid> >();

                    services.AddSingletonAs <MongoAssetFolderRepository>()
                    .As <IAssetFolderRepository>().As <ISnapshotStore <AssetFolderState, Guid> >();

                    services.AddSingletonAs(c => new MongoContentRepository(
                                                c.GetRequiredService <IMongoClient>().GetDatabase(mongoContentDatabaseName),
                                                c.GetRequiredService <IAppProvider>(),
                                                c.GetRequiredService <ITextIndexer>(),
                                                c.GetRequiredService <IJsonSerializer>()))
                    .As <IContentRepository>().As <ISnapshotStore <ContentState, Guid> >();

                    var registration = services.FirstOrDefault(x => x.ServiceType == typeof(IPersistedGrantStore));

                    if (registration == null || registration.ImplementationType == typeof(InMemoryPersistedGrantStore))
                    {
                        services.AddSingletonAs <MongoPersistedGrantStore>()
                        .As <IPersistedGrantStore>();
                    }

                    services.AddSingletonAs(c =>
                    {
                        var database = c.GetRequiredService <IMongoDatabase>();

                        var mongoBucket = new GridFSBucket <string>(database, new GridFSBucketOptions
                        {
                            BucketName = "fullText"
                        });

                        return(new MongoIndexStorage(mongoBucket));
                    }).As <IIndexStorage>();
                }
            });

            services.AddSingleton(typeof(IStore <>), typeof(Store <>));
        }
Beispiel #12
0
        protected override void Load(ContainerBuilder builder)
        {
            var configuration = Configuration.GetValue <string>("store:mongoDb:configuration");

            if (string.IsNullOrWhiteSpace(configuration))
            {
                throw new ConfigurationException("Configure the Store MongoDb configuration with 'store:mongoDb:configuration'.");
            }

            var database = Configuration.GetValue <string>("store:mongoDb:database");

            if (string.IsNullOrWhiteSpace(database))
            {
                throw new ConfigurationException("Configure the Store MongoDb database with 'store:mongoDb:database'.");
            }

            var contentDatabase = Configuration.GetValue <string>("store:mongoDb:contentDatabase");

            if (string.IsNullOrWhiteSpace(contentDatabase))
            {
                contentDatabase = database;
            }

            builder.Register(c => Singletons <IMongoClient> .GetOrAdd(configuration, s => new MongoClient(s)))
            .Named <IMongoClient>(MongoClientRegistration)
            .SingleInstance();

            builder.Register(c => c.ResolveNamed <IMongoClient>(MongoClientRegistration).GetDatabase(database))
            .Named <IMongoDatabase>(MongoDatabaseRegistration)
            .SingleInstance();

            builder.Register(c => c.ResolveNamed <IMongoClient>(MongoClientRegistration).GetDatabase(contentDatabase))
            .Named <IMongoDatabase>(MongoContentDatabaseRegistration)
            .SingleInstance();

            builder.RegisterType <MongoUserStore>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IUserStore <IUser> >()
            .As <IUserFactory>()
            .As <IUserResolver>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoRoleStore>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IRoleStore <IRole> >()
            .As <IRoleFactory>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoPersistedGrantStore>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IPersistedGrantStore>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoUsageStore>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IUsageStore>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoHistoryEventRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IHistoryEventRepository>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoEventConsumerInfoRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IEventConsumerInfoRepository>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoContentRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoContentDatabaseRegistration))
            .As <IContentRepository>()
            .As <IEventConsumer>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoWebhookEventRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IWebhookEventRepository>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoAppRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IAppRepository>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoSchemaRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <ISchemaRepository>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoAssetStatsRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IAssetStatsRepository>()
            .As <IEventConsumer>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoAssetRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IAssetRepository>()
            .As <IEventConsumer>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();

            builder.RegisterType <MongoWebhookRepository>()
            .WithParameter(ResolvedParameter.ForNamed <IMongoDatabase>(MongoDatabaseRegistration))
            .As <IWebhookRepository>()
            .As <IEventConsumer>()
            .As <IExternalSystem>()
            .AsSelf()
            .SingleInstance();


            builder.Register(c =>
                             new CompoundEventConsumer(
                                 c.Resolve <MongoSchemaRepository>(),
                                 c.Resolve <CachingGraphQLService>(),
                                 c.Resolve <CachingSchemaProvider>()))
            .As <IEventConsumer>()
            .AsSelf()
            .SingleInstance();

            builder.Register(c =>
                             new CompoundEventConsumer(
                                 c.Resolve <MongoAppRepository>(),
                                 c.Resolve <CachingAppProvider>()))
            .As <IEventConsumer>()
            .AsSelf()
            .SingleInstance();
        }
Beispiel #13
0
        public static void AddSquidexAssetInfrastructure(this IServiceCollection services, IConfiguration config)
        {
            config.ConfigureByOption("assetStore:type", new Alternatives
            {
                ["Default"] = () =>
                {
                    services.AddSingletonAs <NoopAssetStore>()
                    .AsOptional <IAssetStore>();
                },
                ["Folder"] = () =>
                {
                    var path = config.GetRequiredValue("assetStore:folder:path");

                    services.AddSingletonAs(c => new FolderAssetStore(path, c.GetRequiredService <ILogger <FolderAssetStore> >()))
                    .As <IAssetStore>();
                },
                ["GoogleCloud"] = () =>
                {
                    var bucketName = config.GetRequiredValue("assetStore:googleCloud:bucket");

                    services.AddSingletonAs(c => new GoogleCloudAssetStore(bucketName))
                    .As <IAssetStore>();
                },
                ["AzureBlob"] = () =>
                {
                    var connectionString = config.GetRequiredValue("assetStore:azureBlob:connectionString");
                    var containerName    = config.GetRequiredValue("assetStore:azureBlob:containerName");

                    services.AddSingletonAs(c => new AzureBlobAssetStore(connectionString, containerName))
                    .As <IAssetStore>();
                },
                ["AmazonS3"] = () =>
                {
                    var amazonS3Options = config.GetSection("assetStore:amazonS3").Get <AmazonS3Options>();

                    services.AddSingletonAs(c => new AmazonS3AssetStore(amazonS3Options))
                    .As <IAssetStore>();
                },
                ["MongoDb"] = () =>
                {
                    var mongoConfiguration    = config.GetRequiredValue("assetStore:mongoDb:configuration");
                    var mongoDatabaseName     = config.GetRequiredValue("assetStore:mongoDb:database");
                    var mongoGridFsBucketName = config.GetRequiredValue("assetStore:mongoDb:bucket");

                    services.AddSingletonAs(c =>
                    {
                        var mongoClient   = Singletons <IMongoClient> .GetOrAdd(mongoConfiguration, s => new MongoClient(s));
                        var mongoDatabase = mongoClient.GetDatabase(mongoDatabaseName);

                        var gridFsbucket = new GridFSBucket <string>(mongoDatabase, new GridFSBucketOptions
                        {
                            BucketName = mongoGridFsBucketName
                        });

                        return(new MongoGridFsAssetStore(gridFsbucket));
                    })
                    .As <IAssetStore>();
                },
                ["Ftp"] = () =>
                {
                    var serverHost = config.GetRequiredValue("assetStore:ftp:serverHost");
                    var serverPort = config.GetOptionalValue <int>("assetStore:ftp:serverPort", 21);

                    var username = config.GetRequiredValue("assetStore:ftp:username");
                    var password = config.GetRequiredValue("assetStore:ftp:password");

                    var path = config.GetOptionalValue("assetStore:ftp:path", "/");

                    services.AddSingletonAs(c =>
                    {
                        var factory = new Func <FtpClient>(() => new FtpClient(serverHost, serverPort, username, password));

                        return(new FTPAssetStore(factory, path, c.GetRequiredService <ILogger <FTPAssetStore> >()));
                    })
                    .As <IAssetStore>();
                }
            });

            services.AddSingletonAs <ImageSharpAssetThumbnailGenerator>()
            .As <IAssetThumbnailGenerator>();

            services.AddSingletonAs(c => new DelegateInitializer(
                                        c.GetRequiredService <IAssetStore>().GetType().Name,
                                        c.GetRequiredService <IAssetStore>().InitializeAsync))
            .As <IInitializable>();
        }
Beispiel #14
0
        public static void AddMyStoreServices(this IServiceCollection services, IConfiguration config)
        {
            config.ConfigureByOption("store:type", new Options
            {
                ["MongoDB"] = () =>
                {
                    var mongoConfiguration       = config.GetRequiredValue("store:mongoDb:configuration");
                    var mongoDatabaseName        = config.GetRequiredValue("store:mongoDb:database");
                    var mongoContentDatabaseName = config.GetOptionalValue("store:mongoDb:contentDatabase", mongoDatabaseName);

                    var mongoClient          = Singletons <IMongoClient> .GetOrAdd(mongoConfiguration, s => new MongoClient(s));
                    var mongoDatabase        = mongoClient.GetDatabase(mongoDatabaseName);
                    var mongoContentDatabase = mongoClient.GetDatabase(mongoContentDatabaseName);

                    services.AddSingletonAs(c => new MongoXmlRepository(mongoDatabase))
                    .As <IXmlRepository>()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoMigrationStatus(mongoDatabase))
                    .As <IMigrationStatus>()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoSnapshotStore <EventConsumerState, string>(mongoDatabase, c.GetRequiredService <JsonSerializer>()))
                    .As <ISnapshotStore <EventConsumerState, string> >()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoUserStore(mongoDatabase))
                    .As <IUserStore <IUser> >()
                    .As <IUserFactory>()
                    .As <IUserResolver>()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoRoleStore(mongoDatabase))
                    .As <IRoleStore <IRole> >()
                    .As <IRoleFactory>()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoPersistedGrantStore(mongoDatabase))
                    .As <IPersistedGrantStore>()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoUsageStore(mongoDatabase))
                    .As <IUsageStore>()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoRuleEventRepository(mongoDatabase))
                    .As <IRuleEventRepository>()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoAppRepository(mongoDatabase))
                    .As <IAppRepository>()
                    .As <ISnapshotStore <AppState, Guid> >()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoAssetRepository(mongoDatabase))
                    .As <IAssetRepository>()
                    .As <ISnapshotStore <AssetState, Guid> >()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoRuleRepository(mongoDatabase))
                    .As <IRuleRepository>()
                    .As <ISnapshotStore <RuleState, Guid> >()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoSchemaRepository(mongoDatabase))
                    .As <ISchemaRepository>()
                    .As <ISnapshotStore <SchemaState, Guid> >()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoContentRepository(mongoContentDatabase, c.GetService <IAppProvider>()))
                    .As <IContentRepository>()
                    .As <ISnapshotStore <ContentState, Guid> >()
                    .As <IEventConsumer>()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoHistoryEventRepository(mongoDatabase, c.GetServices <IHistoryEventsCreator>()))
                    .As <IHistoryEventRepository>()
                    .As <IEventConsumer>()
                    .As <IExternalSystem>();

                    services.AddSingletonAs(c => new MongoAssetStatsRepository(mongoDatabase))
                    .As <IAssetStatsRepository>()
                    .As <IEventConsumer>()
                    .As <IExternalSystem>();
                }
            });
        }
Beispiel #15
0
        public static void AddSquidexEventSourcing(this IServiceCollection services, IConfiguration config)
        {
            config.ConfigureByOption("eventStore:type", new Alternatives
            {
                ["MongoDb"] = () =>
                {
                    var mongoConfiguration = config.GetRequiredValue("eventStore:mongoDb:configuration");
                    var mongoDatabaseName  = config.GetRequiredValue("eventStore:mongoDb:database");

                    services.AddSingletonAs(c =>
                    {
                        var mongoClient  = Singletons <IMongoClient> .GetOrAdd(mongoConfiguration, s => new MongoClient(s));
                        var mongDatabase = mongoClient.GetDatabase(mongoDatabaseName);

                        return(new MongoEventStore(mongDatabase, c.GetRequiredService <IEventNotifier>()));
                    })
                    .As <IEventStore>();
                },
                ["CosmosDb"] = () =>
                {
                    var cosmosDbConfiguration = config.GetRequiredValue("eventStore:cosmosDB:configuration");
                    var cosmosDbMasterKey     = config.GetRequiredValue("eventStore:cosmosDB:masterKey");
                    var cosmosDbDatabase      = config.GetRequiredValue("eventStore:cosmosDB:database");

                    services.AddSingletonAs(c => new DocumentClient(new Uri(cosmosDbConfiguration), cosmosDbMasterKey, c.GetRequiredService <JsonSerializerSettings>()))
                    .AsSelf();

                    services.AddSingletonAs(c => new CosmosDbEventStore(
                                                c.GetRequiredService <DocumentClient>(),
                                                cosmosDbMasterKey,
                                                cosmosDbDatabase,
                                                c.GetRequiredService <JsonSerializerSettings>()))
                    .As <IEventStore>();

                    services.AddHealthChecks()
                    .AddCheck <CosmosDbHealthCheck>("CosmosDB", tags: new[] { "node" });
                },
                ["GetEventStore"] = () =>
                {
                    var eventStoreConfiguration  = config.GetRequiredValue("eventStore:getEventStore:configuration");
                    var eventStoreProjectionHost = config.GetRequiredValue("eventStore:getEventStore:projectionHost");
                    var eventStorePrefix         = config.GetValue <string>("eventStore:getEventStore:prefix");

                    services.AddSingletonAs(_ => EventStoreConnection.Create(eventStoreConfiguration))
                    .As <IEventStoreConnection>();

                    services.AddSingletonAs(c => new GetEventStore(
                                                c.GetRequiredService <IEventStoreConnection>(),
                                                c.GetRequiredService <IJsonSerializer>(),
                                                eventStorePrefix,
                                                eventStoreProjectionHost))
                    .As <IEventStore>();

                    services.AddHealthChecks()
                    .AddCheck <GetEventStoreHealthCheck>("EventStore", tags: new[] { "node" });
                }
            });

            services.AddSingletonAs <OrleansEventNotifier>()
            .As <IEventNotifier>();

            services.AddTransientAs <Rebuilder>()
            .AsSelf();

            services.AddSingletonAs <DefaultStreamNameResolver>()
            .As <IStreamNameResolver>();

            services.AddSingletonAs <DefaultEventDataFormatter>()
            .As <IEventDataFormatter>();

            services.AddSingletonAs(c =>
            {
                var allEventConsumers = c.GetServices <IEventConsumer>();

                return(new EventConsumerFactory(n => allEventConsumers.First(x => x.Name == n)));
            });
        }