/// <summary>
        /// Configures EntityFrameworkCore Sqlite with the supplied <see cref="SqliteDbContextOptionsBuilder">.
        /// Connection strings are taken from the IConfiguration, specifically `IConfiguration.GetConnectionString("Store")` and `IConfiguration.GetConnectionString("Projection")` respectively.
        /// To have more control over connection string configuration see <see cref="AddEntityFrameworkCoreSqlite(IOpenEventSourcingBuilder, Action{SqliteOptions})"/>
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="optionsAction"></param>
        /// <returns></returns>
        public static IOpenEventSourcingBuilder AddEntityFrameworkCoreSqlite(this IOpenEventSourcingBuilder builder, Action <SqliteDbContextOptionsBuilder> optionsAction = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.AddEntityFrameworkCore();

            builder.Services.AddDbContext <OpenEventSourcingDbContext>((sp, options) =>
            {
                var config           = sp.GetRequiredService <IConfiguration>();
                var connectionString = config.GetConnectionString("Store");

                options.UseSqlite(connectionString, optionsAction);
            });

            builder.Services.AddDbContext <OpenEventSourcingProjectionDbContext>((sp, options) =>
            {
                var config           = sp.GetRequiredService <IConfiguration>();
                var connectionString = config.GetConnectionString("Projection");

                options.UseSqlite(connectionString, optionsAction);
            });

            return(builder);
        }
        /// <summary>
        /// Configures EntityFrameworkCore SqlServer with the supplied <see cref="SqlServerOptions">.
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="optionsAction"></param>
        /// <returns></returns>
        public static IOpenEventSourcingBuilder AddEntityFrameworkCoreSqlServer(this IOpenEventSourcingBuilder builder, Action <SqlServerOptions> optionsAction = null)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.AddEntityFrameworkCore();

            builder.Services.Configure(optionsAction);

            builder.Services.AddDbContext <OpenEventSourcingDbContext>((sp, options) =>
            {
                var opts = sp.GetRequiredService <SqlServerOptions>();
                options.UseSqlServer(opts.StoreConnectionString, opts.SqlServerOptionsBuilder);
            });

            builder.Services.AddDbContext <OpenEventSourcingProjectionDbContext>((sp, options) =>
            {
                var opts = sp.GetRequiredService <SqlServerOptions>();
                options.UseSqlServer(opts.ProjectionConnectionString, opts.SqlServerOptionsBuilder);
            });

            return(builder);
        }
        public static IOpenEventSourcingBuilder AddAzureServiceBus(this IOpenEventSourcingBuilder builder, Action <ServiceBusOptions> optionsAction)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.Configure(optionsAction);

            builder.Services.AddScoped <IEventBusPublisher, AzureServiceBus>();
            builder.Services.AddScoped <IEventBusConsumer, AzureServiceBus>();
            builder.Services.AddScoped <IMessageFactory, DefaultMessageFactory>();
            builder.Services.AddScoped <ISubscriptionClientFactory, DefaultSubscriptionClientFactory>();
            builder.Services.AddScoped <ISubscriptionClientManager, DefaultSubscriptionClientManager>();
            builder.Services.AddScoped <ITopicClientFactory, DefaultTopicClientFactory>();
            builder.Services.AddSingleton <ITopicMessageReceiver, DefaultTopicMessageReceiver>();
            builder.Services.AddSingleton <IEventContextFactory, DefaultEventContextFactory>();
            builder.Services.AddScoped <ITopicMessageSender, DefaultTopicMessageSender>();
            builder.Services.AddScoped <IServiceBusManagementClient, ServiceBusManagementClient>();
            builder.Services.AddScoped <ServiceBusConnectionStringBuilder>(sp =>
            {
                var options = sp.GetRequiredService <IOptions <ServiceBusOptions> >();
                var connectionStringBuilder = new ServiceBusConnectionStringBuilder(options.Value.ConnectionString);

                if (string.IsNullOrEmpty(connectionStringBuilder.EntityPath) && options.Value.Topic == null)
                {
                    throw new InvalidOperationException($"Azure service bus connection string doesn't contain an entity path and 'UseTopic(...)' has not been called. Either include the entity path in the connection string or by calling 'UseTopic(...)' during startup when configuring Azure Service Bus.");
                }

                if (options.Value.Topic != null)
                {
                    connectionStringBuilder.EntityPath = options.Value.Topic.Name;
                }

                if (!string.IsNullOrEmpty(connectionStringBuilder.EntityPath) && options.Value.Topic == null)
                {
                    options.Value.UseTopic(t => t.WithName(connectionStringBuilder.EntityPath));
                }

                return(connectionStringBuilder);
            });
            builder.Services.AddScoped <ManagementClient>(sp =>
            {
                var connectionStringBuilder = sp.GetRequiredService <ServiceBusConnectionStringBuilder>();

                return(new ManagementClient(connectionStringBuilder));
            });

            return(builder);
        }
        public static IOpenEventSourcingBuilder AddJsonSerializers(this IOpenEventSourcingBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.AddSingleton <ICommandDeserializer, JsonCommandDeserializer>();
            builder.Services.AddSingleton <ICommandSerializer, JsonCommandSerializer>();
            builder.Services.AddSingleton <IEventDeserializer, JsonEventDeserializer>();
            builder.Services.AddSingleton <IEventSerializer, JsonEventSerializer>();
            builder.Services.AddSingleton <IQueryDeserializer, JsonQueryDeserializer>();
            builder.Services.AddSingleton <IQuerySerializer, JsonQuerySerializer>();

            return(builder);
        }
        public static IOpenEventSourcingBuilder AddEntityFrameworkCore(this IOpenEventSourcingBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.AddScoped <ICommandStore, EntityFrameworkCoreCommandStore>();
            builder.Services.AddScoped <IEventStore, EntityFrameworkCoreEventStore>();
            builder.Services.AddScoped <IQueryStore, EntityFrameworkCoreQueryStore>();
            builder.Services.AddScoped <IAggregateRepository, AggregateRepository>();
            builder.Services.AddScoped <IEventModelFactory, DefaultEventModelFactory>();
            builder.Services.AddScoped <IDbContextFactory, OpenEventSourcingDbContextFactory>();
            builder.Services.AddScoped <IProjectionDbContextFactory, OpenEventSourcingProjectionDbContextFactory>();
            builder.Services.AddScoped <IEventModelFactory, DefaultEventModelFactory>();
            builder.Services.AddScoped(typeof(IProjectionWriter <>), typeof(EntityFrameworkCoreProjectionWriter <>));
            builder.Services.AddScoped <IEventContextFactory, DefaultEventContextFactory>();

            return(builder);
        }
        public static IOpenEventSourcingBuilder AddEntityFrameworkCoreInMemory(this IOpenEventSourcingBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.AddEntityFrameworkCore();

            builder.Services.AddDbContext <OpenEventSourcingDbContext>((sp, options) =>
            {
                options.UseInMemoryDatabase($"Store_{Guid.NewGuid().ToString()}");
            });

            builder.Services.AddDbContext <OpenEventSourcingProjectionDbContext>((sp, options) =>
            {
                options.UseInMemoryDatabase($"Projection_{Guid.NewGuid().ToString()}");
            });

            return(builder);
        }
예제 #7
0
        public static IOpenEventSourcingBuilder AddEvents(this IOpenEventSourcingBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.Scan(scan =>
            {
                scan.FromApplicationDependencies()
                .AddClasses(classes => classes.AssignableTo(typeof(IEventHandler <>)), publicOnly: false)
                .AsImplementedInterfaces()
                .WithScopedLifetime();

                scan.FromApplicationDependencies()
                .AddClasses(classes => classes.AssignableTo <IEventDispatcher>(), publicOnly: false)
                .AsSelfWithInterfaces()
                .WithScopedLifetime();
            });

            return(builder);
        }
예제 #8
0
        public static IOpenEventSourcingBuilder AddQueries(this IOpenEventSourcingBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.Services.AddScoped <IQueryStore, NoOpQueryStore>();

            builder.Services.Scan(scan =>
            {
                scan.FromApplicationDependencies()
                .AddClasses(classes => classes.AssignableTo(typeof(IQueryHandler <,>)))
                .AsImplementedInterfaces()
                .WithScopedLifetime()

                .AddClasses(classes => classes.AssignableTo <IQueryDispatcher>())
                .AsImplementedInterfaces()
                .WithScopedLifetime();
            });

            return(builder);
        }
예제 #9
0
        public static IOpenEventSourcingBuilder AddRabbitMq(this IOpenEventSourcingBuilder builder, Action <RabbitMqOptions> optionsAction)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }


            builder.Services.Configure(optionsAction);

            builder.Services.AddScoped <IMessageFactory, DefaultMessageFactory>();
            builder.Services.AddScoped <IEventBusPublisher, RabbitMqEventBus>();
            builder.Services.AddScoped <IEventBusConsumer, RabbitMqEventBus>();
            builder.Services.AddSingleton <RabbitMqConnectionPool>();
            builder.Services.AddSingleton <IRabbitMqConnectionFactory, RabbitMqConnectionFactory>();
            builder.Services.AddScoped <IQueueMessageSender, DefaultQueueMessageSender>();
            builder.Services.AddSingleton <IQueueMessageReceiver, DefaultQueueMessageReceiver>();
            builder.Services.AddScoped <ISubscriptionManager, DefaultSubscriptionManager>();
            builder.Services.AddScoped <IRabbitMqManagementClient, RabbitMqManagementClient>();
            builder.Services.AddHttpClient <IRabbitMqManagementApiClient, RabbitMqManagementApiClient>();
            builder.Services.AddSingleton <IEventContextFactory, DefaultEventContextFactory>();

            return(builder);
        }