From c650403516b7fe14b539696656d5984af172a982 Mon Sep 17 00:00:00 2001 From: dingsongjie Date: Thu, 20 Feb 2020 15:37:25 +0800 Subject: [PATCH] 完成全部 依赖注入 与启动 --- samples/apis/Backet.Api/Controllers/BacketController.cs | 3 +-- samples/apis/Backet.Api/Startup.cs | 10 ++++++++++ src/Pole.Core/EventBus/ObserverUnitContainer.cs | 1 - src/Pole.Core/Extensions/IServiceCollectionExtensions.cs | 36 ------------------------------------ src/Pole.Core/Extensions/IServiceProviderExtensions.cs | 16 ---------------- src/Pole.Core/Extensions/PoleApplicationBuilderExtensions.cs | 17 +++++++++++++++++ src/Pole.Core/Extensions/PoleServiceCollectionExtensions.cs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/Pole.Core/Processor/Server/BackgroundServiceBasedProcessorServer.cs | 3 +++ src/Pole.Core/Startup.cs | 12 +++++++++++- src/Pole.EventBus.Rabbitmq/Extensions.cs | 31 ------------------------------- src/Pole.EventBus.Rabbitmq/PoleRabbitmqStartupConfigExtensions.cs | 32 ++++++++++++++++++++++++++++++++ src/Pole.EventStorage.PostgreSql/CapOptionsExtensions.cs | 32 +------------------------------- src/Pole.EventStorage.PostgreSql/PolePostgreSqlStartupConfigExtensions.cs | 42 ++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 160 insertions(+), 118 deletions(-) delete mode 100644 src/Pole.Core/Extensions/IServiceCollectionExtensions.cs delete mode 100644 src/Pole.Core/Extensions/IServiceProviderExtensions.cs create mode 100644 src/Pole.Core/Extensions/PoleApplicationBuilderExtensions.cs create mode 100644 src/Pole.Core/Extensions/PoleServiceCollectionExtensions.cs delete mode 100644 src/Pole.EventBus.Rabbitmq/Extensions.cs create mode 100644 src/Pole.EventBus.Rabbitmq/PoleRabbitmqStartupConfigExtensions.cs create mode 100644 src/Pole.EventStorage.PostgreSql/PolePostgreSqlStartupConfigExtensions.cs diff --git a/samples/apis/Backet.Api/Controllers/BacketController.cs b/samples/apis/Backet.Api/Controllers/BacketController.cs index 405abc0..2b63024 100644 --- a/samples/apis/Backet.Api/Controllers/BacketController.cs +++ b/samples/apis/Backet.Api/Controllers/BacketController.cs @@ -44,8 +44,7 @@ namespace Backet.Api.Controllers public Task RemoveFirstItem() { var id = "da8a489fa7b4409294ee1b358fbbfba5"; - var grain = clusterClient.GetGrain(id); - clusterClient. + var grain = clusterClient.GetGrain(id); return grain.RemoveFirstItem(); } } diff --git a/samples/apis/Backet.Api/Startup.cs b/samples/apis/Backet.Api/Startup.cs index 9436c30..8ef5c57 100644 --- a/samples/apis/Backet.Api/Startup.cs +++ b/samples/apis/Backet.Api/Startup.cs @@ -27,6 +27,15 @@ namespace Backet.Api services.AddDbContextPool(options => options.UseNpgsql(Configuration["postgres:write"])); services.AddControllers(); + services.AddPole(config => { + config.AddRabbitMQ(option => + { + option.Hosts = new string[1] { Configuration["RabbitmqConfig:HostAddress"] }; + option.Password = Configuration["RabbitmqConfig:HostPassword"]; + option.UserName = Configuration["RabbitmqConfig:HostUserName"]; + }); + }); + services.ConfigureGrainStorageOptions( options => { @@ -44,6 +53,7 @@ namespace Backet.Api app.UseDeveloperExceptionPage(); } + app.UsePole(); app.UseRouting(); app.UseEndpoints(endpoints => diff --git a/src/Pole.Core/EventBus/ObserverUnitContainer.cs b/src/Pole.Core/EventBus/ObserverUnitContainer.cs index 3b5c0f7..e832ec4 100644 --- a/src/Pole.Core/EventBus/ObserverUnitContainer.cs +++ b/src/Pole.Core/EventBus/ObserverUnitContainer.cs @@ -1,5 +1,4 @@ using Microsoft.Extensions.Logging; -using Pole.Core.Observer; using Pole.Core.Utils; using System; using System.Collections.Concurrent; diff --git a/src/Pole.Core/Extensions/IServiceCollectionExtensions.cs b/src/Pole.Core/Extensions/IServiceCollectionExtensions.cs deleted file mode 100644 index 8464d7b..0000000 --- a/src/Pole.Core/Extensions/IServiceCollectionExtensions.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Pole.Core.Abstraction; -using Pole.Core.Channels; -using Pole.Core.EventBus; -using Pole.Core.Processor; -using Pole.Core.Processor.Server; -using Pole.Core.Serialization; -using Pole.Core.UnitOfWork; -using Pole.Core.Utils; -using Pole.Core.Utils.Abstraction; -using System; -using System.Collections.Generic; -using System.Text; - -namespace Pole.Core.Extensions -{ - public static class IServiceCollectionExtensions - { - public static IServiceCollection AddPole(this IServiceCollection services,Action config) - { - services.AddSingleton(); - services.AddTransient(typeof(IMpscChannel<>), typeof(MpscChannel<>)); - services.AddScoped(); - services.AddScoped(); - services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); - - - services.AddSingleton(); - services.AddSingleton(); - services.AddHostedService(); - return services; - } - } -} diff --git a/src/Pole.Core/Extensions/IServiceProviderExtensions.cs b/src/Pole.Core/Extensions/IServiceProviderExtensions.cs deleted file mode 100644 index 608a6ab..0000000 --- a/src/Pole.Core/Extensions/IServiceProviderExtensions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using System; -using System.Collections.Generic; -using System.Text; - -namespace Pole.Core.Extensions -{ - public static class IApplicationBuilderExtensions - { - public static IApplicationBuilder UsePole(this IApplicationBuilder applicationBuilder) - { - Startup.StartRay(applicationBuilder.ApplicationServices); - return applicationBuilder; - } - } -} diff --git a/src/Pole.Core/Extensions/PoleApplicationBuilderExtensions.cs b/src/Pole.Core/Extensions/PoleApplicationBuilderExtensions.cs new file mode 100644 index 0000000..45e5ec4 --- /dev/null +++ b/src/Pole.Core/Extensions/PoleApplicationBuilderExtensions.cs @@ -0,0 +1,17 @@ +using Microsoft.AspNetCore.Builder; +using Pole.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.AspNetCore.Builder +{ + public static class PoleApplicationBuilderExtensions + { + public static IApplicationBuilder UsePole(this IApplicationBuilder applicationBuilder) + { + Startup.StartRay(applicationBuilder.ApplicationServices); + return applicationBuilder; + } + } +} diff --git a/src/Pole.Core/Extensions/PoleServiceCollectionExtensions.cs b/src/Pole.Core/Extensions/PoleServiceCollectionExtensions.cs new file mode 100644 index 0000000..0ebe8ef --- /dev/null +++ b/src/Pole.Core/Extensions/PoleServiceCollectionExtensions.cs @@ -0,0 +1,43 @@ +using Microsoft.Extensions.DependencyInjection; +using Pole.Core; +using Pole.Core.Abstraction; +using Pole.Core.Channels; +using Pole.Core.EventBus; +using Pole.Core.Processor; +using Pole.Core.Processor.Server; +using Pole.Core.Serialization; +using Pole.Core.UnitOfWork; +using Pole.Core.Utils; +using Pole.Core.Utils.Abstraction; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class PoleServiceCollectionExtensions + { + public static IServiceCollection AddPole(this IServiceCollection services,Action config) + { + StartupConfig startupOption = new StartupConfig(services); + if (startupOption.PoleOptionsConfig == null) + { + services.Configure(option => { }); + } + services.AddSingleton(); + services.AddTransient(typeof(IMpscChannel<>), typeof(MpscChannel<>)); + services.AddScoped(); + services.AddScoped(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + + services.AddSingleton(); + services.AddSingleton(); + services.AddHostedService(); + + config(startupOption); + return services; + } + } +} diff --git a/src/Pole.Core/Processor/Server/BackgroundServiceBasedProcessorServer.cs b/src/Pole.Core/Processor/Server/BackgroundServiceBasedProcessorServer.cs index fe8a0b8..fcba568 100644 --- a/src/Pole.Core/Processor/Server/BackgroundServiceBasedProcessorServer.cs +++ b/src/Pole.Core/Processor/Server/BackgroundServiceBasedProcessorServer.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Pole.Core.EventBus.EventStorage; using System; using System.Collections.Generic; using System.Linq; @@ -21,6 +22,8 @@ namespace Pole.Core.Processor.Server } public async Task Start(CancellationToken stoppingToken) { + var eventStorageInitializer = _serviceProvider.GetService(); + await eventStorageInitializer.InitializeAsync(stoppingToken); ProcessingContext processingContext = new ProcessingContext(stoppingToken); List loopProcessors = new List(); diff --git a/src/Pole.Core/Startup.cs b/src/Pole.Core/Startup.cs index 8f98d93..e77a66a 100644 --- a/src/Pole.Core/Startup.cs +++ b/src/Pole.Core/Startup.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -28,4 +29,13 @@ namespace Pole.Core public Func Func { get; set; } } } + public class StartupConfig + { + public StartupConfig(IServiceCollection services) + { + Services = services; + } + public IServiceCollection Services { get; } + public Action PoleOptionsConfig { get; set; } + } } diff --git a/src/Pole.EventBus.Rabbitmq/Extensions.cs b/src/Pole.EventBus.Rabbitmq/Extensions.cs deleted file mode 100644 index 91bed51..0000000 --- a/src/Pole.EventBus.Rabbitmq/Extensions.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; -using Pole.Core; -using Pole.Core.EventBus; - -namespace Pole.EventBus.RabbitMQ -{ - public static class Extensions - { - public static void AddRabbitMQ( - this IServiceCollection serviceCollection, - Action rabbitConfigAction, - Func eventBusConfig = default) - { - serviceCollection.Configure(config => rabbitConfigAction(config)); - serviceCollection.AddSingleton(); - serviceCollection.AddHostedService(); - serviceCollection.AddSingleton(); - serviceCollection.AddSingleton(serviceProvider => serviceProvider.GetService() as IProducerContainer); - Startup.Register(async serviceProvider => - { - var container = serviceProvider.GetService(); - if (eventBusConfig != default) - await eventBusConfig(container); - else - await container.AutoRegister(); - }); - } - } -} diff --git a/src/Pole.EventBus.Rabbitmq/PoleRabbitmqStartupConfigExtensions.cs b/src/Pole.EventBus.Rabbitmq/PoleRabbitmqStartupConfigExtensions.cs new file mode 100644 index 0000000..837b8a7 --- /dev/null +++ b/src/Pole.EventBus.Rabbitmq/PoleRabbitmqStartupConfigExtensions.cs @@ -0,0 +1,32 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Pole.Core; +using Pole.Core.EventBus; +using Pole.EventBus.RabbitMQ; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class PoleRabbitmqStartupConfigExtensions + { + public static void AddRabbitMQ( + this StartupConfig startupOption, + Action rabbitConfigAction, + Func eventBusConfig = default) + { + startupOption.Services.Configure(config => rabbitConfigAction(config)); + startupOption.Services.AddSingleton(); + startupOption.Services.AddHostedService(); + startupOption.Services.AddSingleton(); + startupOption.Services.AddSingleton(serviceProvider => serviceProvider.GetService() as IProducerContainer); + Startup.Register(async serviceProvider => + { + var container = serviceProvider.GetService(); + if (eventBusConfig != default) + await eventBusConfig(container); + else + await container.AutoRegister(); + }); + } + } +} diff --git a/src/Pole.EventStorage.PostgreSql/CapOptionsExtensions.cs b/src/Pole.EventStorage.PostgreSql/CapOptionsExtensions.cs index b23cbd1..b061622 100644 --- a/src/Pole.EventStorage.PostgreSql/CapOptionsExtensions.cs +++ b/src/Pole.EventStorage.PostgreSql/CapOptionsExtensions.cs @@ -9,36 +9,6 @@ namespace Pole.EventStorage.PostgreSql { public static class CapOptionsExtensions { - public static PoleOptions UseEntityFrameworkEventStorage(this PoleOptions options) - where TContext : DbContext - { - return options.UseEntityFrameworkEventStorage(opt => { }); - } - public static PoleOptions UseEntityFrameworkEventStorage(this PoleOptions options, Action configure) - where TContext : DbContext - { - if (configure == null) throw new ArgumentNullException(nameof(configure)); - EFOptions eFOptions = new EFOptions(); - configure(eFOptions); - Action postgreSqlOptionsConfig = postgreSqlOptions => - { - postgreSqlOptions.DbContextType = typeof(TContext); - postgreSqlOptions.Schema = eFOptions.Schema; - using var scope = options.Services.BuildServiceProvider().CreateScope(); - var provider = scope.ServiceProvider; - using var dbContext = (DbContext)provider.GetRequiredService(typeof(TContext)); - postgreSqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; - }; - options.Services.Configure(postgreSqlOptionsConfig); - - return options; - } - public static PoleOptions UsePostgreSqlEventStorage(this PoleOptions options, Action configure) - where TContext : DbContext - { - if (configure == null) throw new ArgumentNullException(nameof(configure)); - options.Services.Configure(configure); - return options; - } + } } diff --git a/src/Pole.EventStorage.PostgreSql/PolePostgreSqlStartupConfigExtensions.cs b/src/Pole.EventStorage.PostgreSql/PolePostgreSqlStartupConfigExtensions.cs new file mode 100644 index 0000000..0b2e8ba --- /dev/null +++ b/src/Pole.EventStorage.PostgreSql/PolePostgreSqlStartupConfigExtensions.cs @@ -0,0 +1,42 @@ +using Microsoft.EntityFrameworkCore; +using Pole.Core; +using Pole.Core.EventBus.EventStorage; +using Pole.Core.EventBus.Transaction; +using Pole.EventStorage.PostgreSql; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class PolePostgreSqlStartupConfigExtensions + { + public static StartupConfig AddEntityFrameworkEventStorage(this StartupConfig config) + where TContext : DbContext + { + return config.AddEntityFrameworkEventStorage(opt => { }); + } + public static StartupConfig AddEntityFrameworkEventStorage(this StartupConfig config, Action configure) + where TContext : DbContext + { + if (configure == null) throw new ArgumentNullException(nameof(configure)); + EFOptions eFOptions = new EFOptions(); + configure(eFOptions); + Action postgreSqlOptionsConfig = postgreSqlOptions => + { + postgreSqlOptions.DbContextType = typeof(TContext); + postgreSqlOptions.Schema = eFOptions.Schema; + using var scope = config.Services.BuildServiceProvider().CreateScope(); + var provider = scope.ServiceProvider; + using var dbContext = (DbContext)provider.GetRequiredService(typeof(TContext)); + postgreSqlOptions.ConnectionString = dbContext.Database.GetDbConnection().ConnectionString; + }; + config.Services.Configure(postgreSqlOptionsConfig); + + config.Services.AddScoped(); + config.Services.AddSingleton(); + config.Services.AddSingleton(); + return config; + } + } +} -- libgit2 0.25.0