Skip to content

Commit

Permalink
实现 Unitofwork 模式来适配事务
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Jan 6, 2024
1 parent 7b453e5 commit 7057e50
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/AgileConfig.Server.Data.Abstraction/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ Task<List<T>> QueryPageAsync(Expression<Func<T, bool>> exp, int pageIndex, int p
string defaultSortField = "Id", string defaultSortType = "ASC");

Task<long> CountAsync(Expression<Func<T, bool>>? exp = null);

IUow Uow { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/AgileConfig.Server.Data.Abstraction/IUow.cs
Original file line number Diff line number Diff line change
@@ -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<bool> SaveChangesAsync();

}
}
39 changes: 39 additions & 0 deletions src/AgileConfig.Server.Data.Freesql/FreeSqlUow.cs
Original file line number Diff line number Diff line change
@@ -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<bool> SaveChangesAsync()
{
_unitOfWork.Commit();

return Task.FromResult(true);
}

public void Dispose()
{
_unitOfWork?.Dispose();
}
}
}
16 changes: 16 additions & 0 deletions src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@ namespace AgileConfig.Server.Data.Freesql
public abstract class FreesqlRepository<T, T1> : IRepository<T, T1> where T : class, IEntity<T1>
{
private readonly IBaseRepository<T> _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();

Check warning on line 26 in src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

Dereference of a possibly null reference.
}
}

public FreesqlRepository(IFreeSql freeSql)

Check warning on line 30 in src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs

View workflow job for this annotation

GitHub Actions / build-dotnet

Non-nullable field '_uow' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_repository = freeSql.GetRepository<T>();
_freeSql = freeSql;
}

public Task<List<T>> AllAsync()
Expand Down
4 changes: 3 additions & 1 deletion src/AgileConfig.Server.Data.Freesql/ServiceCollectionExt.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using AgileConfig.Server.Data.Abstraction;
using Microsoft.Extensions.DependencyInjection;

namespace AgileConfig.Server.Data.Freesql
{
public static class ServiceCollectionExt
{
public static void AddFreeSqlFactory(this IServiceCollection sc)
{
sc.AddScoped<IUow, FreeSqlUow>();
sc.AddSingleton<IFreeSql>(FreeSQL.Instance);
sc.AddSingleton<IFreeSqlFactory, EnvFreeSqlFactory>();
}
Expand Down
3 changes: 3 additions & 0 deletions src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +13,8 @@ public class MongodbRepository<TEntity, TId> : Abstraction.IRepository<TEntity,
{
private readonly MongodbAccess<TEntity> _access;

public IUow Uow { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public MongodbRepository(string? connectionString)
{
_access = new MongodbAccess<TEntity>(connectionString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace AgileConfig.Server.Data.Repository.Freesql
{
public class ConfigRepository : FreesqlRepository<Config, string>, IConfigRepository
{
private readonly IFreeSql freeSql;
private readonly IFreeSql _freeSql;

public ConfigRepository(IFreeSql freeSql) : base(freeSql)
{
this.freeSql = freeSql;
this._freeSql = freeSql;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Func<string, IUow>>(sp => env =>
{
string envProvider = GetEnvProvider(env, config, defaultProvider);

if (envProvider.Equals("mongodb", StringComparison.OrdinalIgnoreCase))
{
return null;
}
else
{
var factory = sp.GetService<IFreeSqlFactory>();
var fsq = factory.Create(env);
return new FreeSqlUow(fsq);
}
});

sc.AddScoped<Func<string, IConfigPublishedRepository>>(sp => env =>
{
string envProvider = GetEnvProvider(env, config, defaultProvider);
Expand All @@ -76,6 +92,7 @@ public static IServiceCollection AddRepositories(this IServiceCollection sc)
else
{
var factory = sp.GetService<IFreeSqlFactory>();

return new Freesql.ConfigRepository(factory.Create(env));
}
});
Expand Down
29 changes: 20 additions & 9 deletions src/AgileConfig.Server.Service/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ConfigService : IConfigService
private readonly IAppService _appService;
private readonly ISettingService _settingService;
private readonly IUserService _userService;
private readonly Func<string, IUow> _uowAccessor;
private readonly Func<string, IConfigRepository> _configRepositoryAccessor;
private readonly Func<string, IConfigPublishedRepository> _configPublishedRepositoryAccessor;
private readonly Func<string, IPublishDetailRepository> _publishDetailRepositoryAccessor;
Expand All @@ -29,19 +30,21 @@ public ConfigService(IMemoryCache memoryCache,
IAppService appService,
ISettingService settingService,
IUserService userService,
Func<string, IConfigRepository> configRepository,
Func<string, IConfigPublishedRepository> configPublishedRepository,
Func<string, IPublishDetailRepository> publishDetailRepository,
Func<string, IPublishTimelineRepository> publishTimelineRepository)
Func<string, IUow> uowAccessor,
Func<string, IConfigRepository> configRepositoryAccessor,
Func<string, IConfigPublishedRepository> configPublishedRepositoryAccessor,
Func<string, IPublishDetailRepository> publishDetailRepositoryAccessor,
Func<string, IPublishTimelineRepository> 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<string> IfEnvEmptySetDefaultAsync(string env)
Expand Down Expand Up @@ -349,7 +352,7 @@ public async Task<bool> AddRangeAsync(List<Config> 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);
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -617,6 +626,8 @@ public void Dispose()
await configPublishedRepository.UpdateAsync(publishedConfigs);
await configPublishedRepository.InsertAsync(publishedConfigsCopy);

await uow?.SaveChangesAsync();

ClearAppPublishedConfigsMd5Cache(appId, env);
ClearAppPublishedConfigsMd5CacheWithInheritanced(appId, env);

Expand Down

0 comments on commit 7057e50

Please sign in to comment.