From dda52c894ca191ee6e8a874105a503454f4b5979 Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Mon, 22 Jan 2024 01:20:19 +0800 Subject: [PATCH] Add RepositoryServiceRegister class for freesql and mongodb; --- src/AgileConfig.Server.Common/TinyEventBus.cs | 17 ++- .../IRepositoryServiceRegister.cs | 32 +++++ .../FreeSQL.cs | 4 +- .../FreesqlRepositoryServiceRegister.cs | 62 ++++++++++ .../ServiceCollectionExt.cs | 30 ----- .../MongodbRepositoryServiceRegister.cs | 64 ++++++++++ ...fig.Server.Data.Repository.Selector.csproj | 2 +- .../RepositoryExtension.cs | 109 +++++------------- 8 files changed, 199 insertions(+), 121 deletions(-) create mode 100644 src/AgileConfig.Server.Data.Abstraction/IRepositoryServiceRegister.cs create mode 100644 src/AgileConfig.Server.Data.Repository.Freesql/FreesqlRepositoryServiceRegister.cs delete mode 100644 src/AgileConfig.Server.Data.Repository.Freesql/ServiceCollectionExt.cs create mode 100644 src/AgileConfig.Server.Data.Repository.Mongodb/MongodbRepositoryServiceRegister.cs diff --git a/src/AgileConfig.Server.Common/TinyEventBus.cs b/src/AgileConfig.Server.Common/TinyEventBus.cs index 7768a47c..0b1bd119 100644 --- a/src/AgileConfig.Server.Common/TinyEventBus.cs +++ b/src/AgileConfig.Server.Common/TinyEventBus.cs @@ -94,17 +94,14 @@ public void Fire(string eventKey, object param = null) { foreach (var act in actions) { - Task.Run(() => + try { - try - { - act(param); - } - catch (Exception e) - { - _logger?.LogError(e, $"fire event {eventKey} error"); - } - }); + act(param); + } + catch (Exception e) + { + _logger?.LogError(e, $"fire event {eventKey} error"); + } } } } diff --git a/src/AgileConfig.Server.Data.Abstraction/IRepositoryServiceRegister.cs b/src/AgileConfig.Server.Data.Abstraction/IRepositoryServiceRegister.cs new file mode 100644 index 00000000..ef864d2c --- /dev/null +++ b/src/AgileConfig.Server.Data.Abstraction/IRepositoryServiceRegister.cs @@ -0,0 +1,32 @@ +using Microsoft.Extensions.DependencyInjection; + +namespace AgileConfig.Server.Data.Abstraction +{ + /// + /// 如果新对接一种存储,需要实现此接口 + /// + public interface IRepositoryServiceRegister + { + /// + /// 根据 provider name 判断是否适合当前注册器 + /// + /// + /// + bool IsSuit4Provider(string provider); + + /// + /// 注册固定的仓储 + /// + /// + void AddFixedRepositories(IServiceCollection sc); + + /// + /// 根据环境获取仓储 + /// + /// + /// + /// + /// + T GetServiceByEnv(IServiceProvider sp, string env) where T : class; + } +} diff --git a/src/AgileConfig.Server.Data.Freesql/FreeSQL.cs b/src/AgileConfig.Server.Data.Freesql/FreeSQL.cs index 224fe9f3..9ad87a50 100644 --- a/src/AgileConfig.Server.Data.Freesql/FreeSQL.cs +++ b/src/AgileConfig.Server.Data.Freesql/FreeSQL.cs @@ -55,7 +55,7 @@ private static void ApplyDatabaseStructrue(IFreeSql sql) EnsureTables.Ensure(sql); } - private static FreeSql.DataType? ProviderToFreesqlDbType(string provider) + public static FreeSql.DataType? ProviderToFreesqlDbType(string provider) { switch (provider.ToLower()) { @@ -69,6 +69,8 @@ private static void ApplyDatabaseStructrue(IFreeSql sql) return FreeSql.DataType.PostgreSQL; case "postgresql": return FreeSql.DataType.PostgreSQL; + case "pg": + return FreeSql.DataType.PostgreSQL; case "oracle": return FreeSql.DataType.Oracle; default: diff --git a/src/AgileConfig.Server.Data.Repository.Freesql/FreesqlRepositoryServiceRegister.cs b/src/AgileConfig.Server.Data.Repository.Freesql/FreesqlRepositoryServiceRegister.cs new file mode 100644 index 00000000..d226a962 --- /dev/null +++ b/src/AgileConfig.Server.Data.Repository.Freesql/FreesqlRepositoryServiceRegister.cs @@ -0,0 +1,62 @@ +using AgileConfig.Server.Data.Abstraction; +using AgileConfig.Server.Data.Freesql; +using Microsoft.Extensions.DependencyInjection; + +namespace AgileConfig.Server.Data.Repository.Freesql +{ + public class FreesqlRepositoryServiceRegister : IRepositoryServiceRegister + { + public void AddFixedRepositories(IServiceCollection sc) + { + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddSingleton(); + } + + public T GetServiceByEnv(IServiceProvider sp, string env) where T : class + { + if (typeof(T) == typeof(IUow)) + { + var factory = sp.GetService(); + var fsq = factory.Create(env); + return (new FreeSqlUow(fsq)) as T; + } + if (typeof(T) == typeof(IConfigPublishedRepository)) + { + var factory = sp.GetService(); + return new ConfigPublishedRepository(factory.Create(env)) as T; + } + if (typeof(T) == typeof(IConfigRepository)) + { + var factory = sp.GetService(); + return new ConfigRepository(factory.Create(env)) as T; + } + if (typeof(T) == typeof(IPublishDetailRepository)) + { + var factory = sp.GetService(); + return new PublishDetailRepository(factory.Create(env)) as T; + } + if (typeof(T) == typeof(IPublishTimelineRepository)) + { + var factory = sp.GetService(); + return new PublishTimelineRepository(factory.Create(env)) as T; + } + + return default(T); + } + + public bool IsSuit4Provider(string provider) + { + var freesqlType = FreeSQL.ProviderToFreesqlDbType(provider); + + return freesqlType.HasValue; + } + } +} diff --git a/src/AgileConfig.Server.Data.Repository.Freesql/ServiceCollectionExt.cs b/src/AgileConfig.Server.Data.Repository.Freesql/ServiceCollectionExt.cs deleted file mode 100644 index 9a6c4e79..00000000 --- a/src/AgileConfig.Server.Data.Repository.Freesql/ServiceCollectionExt.cs +++ /dev/null @@ -1,30 +0,0 @@ -using AgileConfig.Server.Data.Abstraction; -using AgileConfig.Server.Data.Freesql; -using Microsoft.Extensions.DependencyInjection; - -namespace AgileConfig.Server.Data.Repository.Freesql -{ - public static class ServiceCollectionExt - { - public static void AddFreeSqlRepository(this IServiceCollection sc) - { - sc.AddFreeRepository(); - - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - - sc.AddSingleton(); - } - } -} diff --git a/src/AgileConfig.Server.Data.Repository.Mongodb/MongodbRepositoryServiceRegister.cs b/src/AgileConfig.Server.Data.Repository.Mongodb/MongodbRepositoryServiceRegister.cs new file mode 100644 index 00000000..4f8428da --- /dev/null +++ b/src/AgileConfig.Server.Data.Repository.Mongodb/MongodbRepositoryServiceRegister.cs @@ -0,0 +1,64 @@ +using AgileConfig.Server.Data.Abstraction.DbProvider; + +namespace AgileConfig.Server.Data.Repository.Mongodb +{ + public class MongodbRepositoryServiceRegister : IRepositoryServiceRegister + { + public void AddFixedRepositories(IServiceCollection sc) + { + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddScoped(); + sc.AddSingleton(); + } + + public T GetServiceByEnv(IServiceProvider sp, string env) where T : class + { + if (typeof(T) == typeof(IUow)) + { + return new MongodbUow() as T; + } + if (typeof(T) == typeof(IConfigPublishedRepository)) + { + var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env); + return new ConfigPublishedRepository(envDbConfig.ConnectionString) as T; + } + if (typeof(T) == typeof(IConfigRepository)) + { + var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env); + return new ConfigRepository(envDbConfig.ConnectionString) as T; + } + if (typeof(T) == typeof(IPublishDetailRepository)) + { + var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env); + return new PublishDetailRepository(envDbConfig.ConnectionString) as T; + } + if (typeof(T) == typeof(IPublishTimelineRepository)) + { + var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env); + return new PublishTimelineRepository(envDbConfig.ConnectionString) as T; + } + + return default(T); + } + + public bool IsSuit4Provider(string provider) + { + switch (provider.ToLower()) + { + case "mongodb": + return true; + default: + break; + } + + return false; + } + } +} diff --git a/src/AgileConfig.Server.Data.Repository.Selector/AgileConfig.Server.Data.Repository.Selector.csproj b/src/AgileConfig.Server.Data.Repository.Selector/AgileConfig.Server.Data.Repository.Selector.csproj index 9aad88be..7f76c28c 100644 --- a/src/AgileConfig.Server.Data.Repository.Selector/AgileConfig.Server.Data.Repository.Selector.csproj +++ b/src/AgileConfig.Server.Data.Repository.Selector/AgileConfig.Server.Data.Repository.Selector.csproj @@ -3,7 +3,7 @@ net8.0 enable - enable + disable diff --git a/src/AgileConfig.Server.Data.Repository.Selector/RepositoryExtension.cs b/src/AgileConfig.Server.Data.Repository.Selector/RepositoryExtension.cs index 246bb6c7..d763f71d 100644 --- a/src/AgileConfig.Server.Data.Repository.Selector/RepositoryExtension.cs +++ b/src/AgileConfig.Server.Data.Repository.Selector/RepositoryExtension.cs @@ -1,9 +1,7 @@ -using AgileConfig.Server.Common; -using AgileConfig.Server.Data.Abstraction; +using AgileConfig.Server.Data.Abstraction; using AgileConfig.Server.Data.Abstraction.DbProvider; -using AgileConfig.Server.Data.Freesql; -using AgileConfig.Server.Data.Mongodb; -using Microsoft.Extensions.Configuration; +using AgileConfig.Server.Data.Repository.Freesql; +using AgileConfig.Server.Data.Repository.Mongodb; using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Data.Repository.Selector @@ -21,33 +19,10 @@ public static IServiceCollection AddRepositories(this IServiceCollection sc) throw new ArgumentNullException(nameof(defaultProvider)); } - #region these repository will use default provider - if (defaultProvider.Provider.Equals("mongodb", StringComparison.OrdinalIgnoreCase)) - { - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddSingleton(); - } - else - { - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddScoped(); - sc.AddSingleton(); - } + #region add default fixed repositories + + GetRepositoryServiceRegister(defaultProvider.Provider).AddFixedRepositories(sc); + #endregion #region these repositories genereated dependency env provider, if no env provider use default provider @@ -55,82 +30,58 @@ public static IServiceCollection AddRepositories(this IServiceCollection sc) { var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env); - if (envDbConfig.Provider.Equals("mongodb", StringComparison.OrdinalIgnoreCase)) - { - return new MongodbUow(); // currently this is an empty uow - } - else - { - var factory = sp.GetService(); - var fsq = factory.Create(env); - return new FreeSqlUow(fsq); - } + return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv(sp, env); }); sc.AddScoped>(sp => env => { var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env); - if (envDbConfig.Provider.Equals("mongodb", StringComparison.OrdinalIgnoreCase)) - { - return new Mongodb.ConfigPublishedRepository(envDbConfig.ConnectionString); - } - else - { - var factory = sp.GetService(); - return new Freesql.ConfigPublishedRepository(factory.Create(env)); - } + return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv(sp, env); }); sc.AddScoped>(sp => env => { var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env); - if (envDbConfig.Provider.Equals("mongodb", StringComparison.OrdinalIgnoreCase)) - { - return new Mongodb.ConfigRepository(envDbConfig.ConnectionString); - } - else - { - var factory = sp.GetService(); - - return new Freesql.ConfigRepository(factory.Create(env)); - } + return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv(sp, env); }); sc.AddScoped>(sp => env => { var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env); - if (envDbConfig.Provider.Equals("mongodb", StringComparison.OrdinalIgnoreCase)) - { - return new Mongodb.PublishDetailRepository(envDbConfig.ConnectionString); - } - else - { - var factory = sp.GetService(); - return new Freesql.PublishDetailRepository(factory.Create(env)); - } + return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv(sp, env); }); sc.AddScoped>(sp => env => { var envDbConfig = DbConfigInfoFactory.GetConfigInfo(env); - if (envDbConfig.Provider.Equals("mongodb", StringComparison.OrdinalIgnoreCase)) - { - return new Mongodb.PublishTimelineRepository(envDbConfig.ConnectionString); - } - else - { - var factory = sp.GetService(); - return new Freesql.PublishTimelineRepository(factory.Create(env)); - } + return GetRepositoryServiceRegister(envDbConfig.Provider).GetServiceByEnv(sp, env); }); #endregion return sc; } + // if add new type of repository service, add it here + private static List _repositoryServiceRegisters = new List() { + new FreesqlRepositoryServiceRegister(), + new MongodbRepositoryServiceRegister() + }; + + private static IRepositoryServiceRegister GetRepositoryServiceRegister(string provider) + { + foreach (var register in _repositoryServiceRegisters) + { + if (register.IsSuit4Provider(provider)) + { + return register; + } + } + + throw new ArgumentException($"[{provider}] is not a supported provider."); + } } } \ No newline at end of file