From 7057e5005edc86c6a830fddd7ef748b4854a1121 Mon Sep 17 00:00:00 2001 From: "agile.zhou" Date: Sat, 6 Jan 2024 17:22:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20Unitofwork=20=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E6=9D=A5=E9=80=82=E9=85=8D=E4=BA=8B=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IRepository.cs | 2 + .../IUow.cs | 14 +++++++ .../FreeSqlUow.cs | 39 +++++++++++++++++++ .../FreesqlRepository.cs | 16 ++++++++ .../ServiceCollectionExt.cs | 4 +- .../MongodbRepository.cs | 3 ++ .../ConfigRepository.cs | 4 +- .../RepositoryExtension.cs | 17 ++++++++ .../ConfigService.cs | 29 +++++++++----- 9 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 src/AgileConfig.Server.Data.Abstraction/IUow.cs create mode 100644 src/AgileConfig.Server.Data.Freesql/FreeSqlUow.cs diff --git a/src/AgileConfig.Server.Data.Abstraction/IRepository.cs b/src/AgileConfig.Server.Data.Abstraction/IRepository.cs index 584dc22a..ba324997 100644 --- a/src/AgileConfig.Server.Data.Abstraction/IRepository.cs +++ b/src/AgileConfig.Server.Data.Abstraction/IRepository.cs @@ -27,5 +27,7 @@ Task> QueryPageAsync(Expression> exp, int pageIndex, int p string defaultSortField = "Id", string defaultSortType = "ASC"); Task CountAsync(Expression>? exp = null); + + IUow Uow { get; set; } } } diff --git a/src/AgileConfig.Server.Data.Abstraction/IUow.cs b/src/AgileConfig.Server.Data.Abstraction/IUow.cs new file mode 100644 index 00000000..462cbe9d --- /dev/null +++ b/src/AgileConfig.Server.Data.Abstraction/IUow.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AgileConfig.Server.Data.Abstraction +{ + public interface IUow : IDisposable + { + Task SaveChangesAsync(); + + } +} diff --git a/src/AgileConfig.Server.Data.Freesql/FreeSqlUow.cs b/src/AgileConfig.Server.Data.Freesql/FreeSqlUow.cs new file mode 100644 index 00000000..14572802 --- /dev/null +++ b/src/AgileConfig.Server.Data.Freesql/FreeSqlUow.cs @@ -0,0 +1,39 @@ +using FreeSql; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AgileConfig.Server.Data.Freesql +{ + public class FreeSqlUow : Abstraction.IUow + { + private readonly IFreeSql _freeSql; + private IUnitOfWork _unitOfWork; + + + public FreeSqlUow(IFreeSql freeSql) + { + this._freeSql = freeSql; + _unitOfWork = _freeSql.CreateUnitOfWork(); + } + + public IUnitOfWork GetFreesqlUnitOfWork() + { + return _unitOfWork; + } + + public Task SaveChangesAsync() + { + _unitOfWork.Commit(); + + return Task.FromResult(true); + } + + public void Dispose() + { + _unitOfWork?.Dispose(); + } + } +} diff --git a/src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs b/src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs index 10c41034..ef43613f 100644 --- a/src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs +++ b/src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs @@ -11,10 +11,26 @@ namespace AgileConfig.Server.Data.Freesql public abstract class FreesqlRepository : IRepository where T : class, IEntity { private readonly IBaseRepository _repository; + private readonly IFreeSql _freeSql; + private IUow _uow; + public IUow Uow + { + get + { + return _uow; + } + set + { + _uow = value; + var freesqlUow = value as FreeSqlUow; + _repository.UnitOfWork = freesqlUow.GetFreesqlUnitOfWork(); + } + } public FreesqlRepository(IFreeSql freeSql) { _repository = freeSql.GetRepository(); + _freeSql = freeSql; } public Task> AllAsync() diff --git a/src/AgileConfig.Server.Data.Freesql/ServiceCollectionExt.cs b/src/AgileConfig.Server.Data.Freesql/ServiceCollectionExt.cs index 94c79d98..120e5e81 100644 --- a/src/AgileConfig.Server.Data.Freesql/ServiceCollectionExt.cs +++ b/src/AgileConfig.Server.Data.Freesql/ServiceCollectionExt.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using AgileConfig.Server.Data.Abstraction; +using Microsoft.Extensions.DependencyInjection; namespace AgileConfig.Server.Data.Freesql { @@ -6,6 +7,7 @@ public static class ServiceCollectionExt { public static void AddFreeSqlFactory(this IServiceCollection sc) { + sc.AddScoped(); sc.AddSingleton(FreeSQL.Instance); sc.AddSingleton(); } diff --git a/src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs b/src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs index 6dea867a..a37870d4 100644 --- a/src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs +++ b/src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs @@ -1,5 +1,6 @@ using System.Linq.Expressions; using AgileConfig.Server.Common; +using AgileConfig.Server.Data.Abstraction; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using MongoDB.Driver; @@ -12,6 +13,8 @@ public class MongodbRepository : Abstraction.IRepository _access; + public IUow Uow { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public MongodbRepository(string? connectionString) { _access = new MongodbAccess(connectionString); diff --git a/src/AgileConfig.Server.Data.Repository.Freesql/ConfigRepository.cs b/src/AgileConfig.Server.Data.Repository.Freesql/ConfigRepository.cs index 9917b27a..7569340d 100644 --- a/src/AgileConfig.Server.Data.Repository.Freesql/ConfigRepository.cs +++ b/src/AgileConfig.Server.Data.Repository.Freesql/ConfigRepository.cs @@ -6,11 +6,11 @@ namespace AgileConfig.Server.Data.Repository.Freesql { public class ConfigRepository : FreesqlRepository, IConfigRepository { - private readonly IFreeSql freeSql; + private readonly IFreeSql _freeSql; public ConfigRepository(IFreeSql freeSql) : base(freeSql) { - this.freeSql = freeSql; + this._freeSql = freeSql; } } } diff --git a/src/AgileConfig.Server.Data.Repository.Selector/RepositoryExtension.cs b/src/AgileConfig.Server.Data.Repository.Selector/RepositoryExtension.cs index 12ba4760..1fbbfad6 100644 --- a/src/AgileConfig.Server.Data.Repository.Selector/RepositoryExtension.cs +++ b/src/AgileConfig.Server.Data.Repository.Selector/RepositoryExtension.cs @@ -50,6 +50,22 @@ public static IServiceCollection AddRepositories(this IServiceCollection sc) #endregion #region these repositories genereated dependency env provider, if no env provider use default provider + sc.AddScoped>(sp => env => + { + string envProvider = GetEnvProvider(env, config, defaultProvider); + + if (envProvider.Equals("mongodb", StringComparison.OrdinalIgnoreCase)) + { + return null; + } + else + { + var factory = sp.GetService(); + var fsq = factory.Create(env); + return new FreeSqlUow(fsq); + } + }); + sc.AddScoped>(sp => env => { string envProvider = GetEnvProvider(env, config, defaultProvider); @@ -76,6 +92,7 @@ public static IServiceCollection AddRepositories(this IServiceCollection sc) else { var factory = sp.GetService(); + return new Freesql.ConfigRepository(factory.Create(env)); } }); diff --git a/src/AgileConfig.Server.Service/ConfigService.cs b/src/AgileConfig.Server.Service/ConfigService.cs index 03e62444..3c5b907a 100644 --- a/src/AgileConfig.Server.Service/ConfigService.cs +++ b/src/AgileConfig.Server.Service/ConfigService.cs @@ -20,6 +20,7 @@ public class ConfigService : IConfigService private readonly IAppService _appService; private readonly ISettingService _settingService; private readonly IUserService _userService; + private readonly Func _uowAccessor; private readonly Func _configRepositoryAccessor; private readonly Func _configPublishedRepositoryAccessor; private readonly Func _publishDetailRepositoryAccessor; @@ -29,19 +30,21 @@ public ConfigService(IMemoryCache memoryCache, IAppService appService, ISettingService settingService, IUserService userService, - Func configRepository, - Func configPublishedRepository, - Func publishDetailRepository, - Func publishTimelineRepository) + Func uowAccessor, + Func configRepositoryAccessor, + Func configPublishedRepositoryAccessor, + Func publishDetailRepositoryAccessor, + Func publishTimelineRepositoryAccessor) { _memoryCache = memoryCache; _appService = appService; _settingService = settingService; _userService = userService; - _configRepositoryAccessor = configRepository; - _configPublishedRepositoryAccessor = configPublishedRepository; - _publishDetailRepositoryAccessor = publishDetailRepository; - _publishTimelineRepositoryAccsssor = publishTimelineRepository; + _uowAccessor = uowAccessor; + _configRepositoryAccessor = configRepositoryAccessor; + _configPublishedRepositoryAccessor = configPublishedRepositoryAccessor; + _publishDetailRepositoryAccessor = publishDetailRepositoryAccessor; + _publishTimelineRepositoryAccsssor = publishTimelineRepositoryAccessor; } public async Task IfEnvEmptySetDefaultAsync(string env) @@ -349,7 +352,7 @@ public async Task AddRangeAsync(List configs, string env) }); using var repository = _configRepositoryAccessor(env); - await _configRepositoryAccessor(env).InsertAsync(configs); + await repository.InsertAsync(configs); ClearAppPublishedConfigsMd5Cache(configs.First().AppId, env); ClearAppPublishedConfigsMd5CacheWithInheritanced(configs.First().AppId, env); @@ -469,10 +472,16 @@ public void Dispose() await _lock.WaitAsync(); try { + using var uow = _uowAccessor(env); + using var configRepository = _configRepositoryAccessor(env); + configRepository.Uow = uow; using var publishTimelineRepository = _publishTimelineRepositoryAccsssor(env); + publishTimelineRepository.Uow = uow; using var configPublishedRepository = _configPublishedRepositoryAccessor(env); + configPublishedRepository.Uow = uow; using var publishDetailRepository = _publishDetailRepositoryAccessor(env); + publishDetailRepository.Uow = uow; var waitPublishConfigs = await configRepository.QueryAsync(x => x.AppId == appId && @@ -617,6 +626,8 @@ public void Dispose() await configPublishedRepository.UpdateAsync(publishedConfigs); await configPublishedRepository.InsertAsync(publishedConfigsCopy); + await uow?.SaveChangesAsync(); + ClearAppPublishedConfigsMd5Cache(appId, env); ClearAppPublishedConfigsMd5CacheWithInheritanced(appId, env);