Skip to content

Commit

Permalink
Merge pull request #3 from pengqian089/support-mongodb-repository
Browse files Browse the repository at this point in the history
Service 实现层移除 FreeSql 的依赖,同时适配新的 Repository 层
  • Loading branch information
pengqian089 authored Dec 26, 2023
2 parents bcbbd25 + a016c48 commit e2f8137
Show file tree
Hide file tree
Showing 31 changed files with 461 additions and 286 deletions.
7 changes: 7 additions & 0 deletions src/AgileConfig.Server.Data.Abstraction/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface IRepository<T, T1> : IDisposable where T : IEntity<T1>
Task UpdateAsync(T entity);
Task UpdateAsync(IList<T> entities);

Task DeleteAsync(T1 id);

Task DeleteAsync(T entity);

Task DeleteAsync(IList<T> entities);
Expand All @@ -20,5 +22,10 @@ public interface IRepository<T, T1> : IDisposable where T : IEntity<T1>
Task InsertAsync(IList<T> entities);

Task<List<T>> QueryAsync(Expression<Func<T, bool>> exp);

Task<List<T>> QueryPageAsync(Expression<Func<T, bool>> exp, int pageIndex, int pageSize,
string defaultSortField = "Id", string defaultSortType = "ASC");

Task<long> CountAsync(Expression<Func<T, bool>>? exp = null);
}
}
44 changes: 44 additions & 0 deletions src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public Task<List<T>> AllAsync()
return _repository.Select.ToListAsync();
}

public async Task DeleteAsync(T1 id)
{
await _repository.DeleteAsync(x => Equals(x.Id, id));
}

public Task DeleteAsync(T entity)
{
return _repository.DeleteAsync(entity);
Expand Down Expand Up @@ -52,6 +57,45 @@ public Task<List<T>> QueryAsync(Expression<Func<T, bool>> exp)
return _repository.Where(exp).ToListAsync();
}

public async Task<List<T>> QueryPageAsync(Expression<Func<T, bool>> exp, int pageIndex, int pageSize, string defaultSortField = "Id",
string defaultSortType = "ASC")
{
var query = _repository.Where(exp);
var sort = Sort(defaultSortField);
if (string.Equals(defaultSortField, "DESC", StringComparison.OrdinalIgnoreCase))
{
query.OrderByDescending(sort);
}
else
{
query.OrderBy(sort);
}
return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
}

private Expression<Func<T, object>> Sort(string defaultSortField)
{
Expression<Func<T, object>> defaultSort = x => x.Id;
if (!string.IsNullOrEmpty(defaultSortField) &&
!defaultSortField.Equals("Id", StringComparison.OrdinalIgnoreCase))
{
var property = typeof(T).GetProperty(defaultSortField);
if (property == null)
{
return defaultSort;
}
var parameter = Expression.Parameter(typeof(T), "__q");
var memberExpress = Expression.Property(parameter, property);
return Expression.Lambda<Func<T,object>>(memberExpress, parameter);
}
return defaultSort;
}

public async Task<long> CountAsync(Expression<Func<T, bool>> exp = null)
{
return await (exp == null ? _repository.Select.CountAsync() : _repository.Select.Where(exp).CountAsync());
}

public Task UpdateAsync(T entity)
{
return _repository.UpdateAsync(entity);
Expand Down
45 changes: 45 additions & 0 deletions src/AgileConfig.Server.Data.Mongodb/MongodbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public async Task UpdateAsync(IList<TEntity> entities)
await _access.Collection.BulkWriteAsync(writes);
}

public async Task DeleteAsync(TId id)
{
var filter = Builders<TEntity>.Filter.Eq(x => x.Id, id);
await _access.Collection.DeleteOneAsync(filter);
}

public async Task DeleteAsync(TEntity entity)
{
var filter = GetIdPropertyFilter(entity.Id);
Expand Down Expand Up @@ -88,4 +94,43 @@ public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> exp)
{
return await _access.MongoQueryable.Where(exp).ToListAsync();
}

public async Task<List<TEntity>> QueryPageAsync(Expression<Func<TEntity, bool>> exp, int pageIndex, int pageSize, string defaultSortField = "Id",
string defaultSortType = "ASC")
{
var query = _access.MongoQueryable.Where(exp);
var sort = Sort(defaultSortField);
if (string.Equals(defaultSortField, "DESC", StringComparison.OrdinalIgnoreCase))
{
query.OrderByDescending(sort);
}
else
{
query.OrderBy(sort);
}
return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
}

private Expression<Func<TEntity, object>> Sort(string defaultSortField)
{
Expression<Func<TEntity, object>> defaultSort = x => x.Id;
if (!string.IsNullOrEmpty(defaultSortField) &&
!defaultSortField.Equals("Id", StringComparison.OrdinalIgnoreCase))
{
var property = typeof(TEntity).GetProperty(defaultSortField);
if (property == null)
{
return defaultSort;
}
var parameter = Expression.Parameter(typeof(TEntity), "__q");
var memberExpress = Expression.Property(parameter, property);
return Expression.Lambda<Func<TEntity,object>>(memberExpress, parameter);
}
return defaultSort;
}

public async Task<long> CountAsync(Expression<Func<TEntity, bool>>? exp = null)
{
return await (exp == null ? _access.MongoQueryable.CountAsync() : _access.MongoQueryable.Where(exp).CountAsync());
}
}
1 change: 0 additions & 1 deletion src/AgileConfig.Server.IService/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public interface IUserService: IDisposable
{
Task<List<User>> GetAll();
Task<User> GetUserAsync(string userId);
User GetUser(string userId);

Task<List<User>> GetUsersByNameAsync(string userName);
Task<List<Role>> GetUserRolesAsync(string userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AgileConfig.Server.Common\AgileConfig.Server.Common.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Freesql\AgileConfig.Server.Data.Freesql.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Abstraction\AgileConfig.Server.Data.Abstraction.csproj" />
<ProjectReference Include="..\AgileConfig.Server.IService\AgileConfig.Server.IService.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/AgileConfig.Server.Service/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public void Dispose()
//这里默认admin console 实例只部署一个,如果部署多个同步操作,高并发的时候这个version会有问题
var versionMax = (await _publishTimelineRepository.QueryAsync(x => x.AppId == appId)).Max(x => x.Version);

var user = _userService.GetUser(operatorr);
var user = await _userService.GetUserAsync(operatorr);

var publishTimelineNode = new PublishTimeline();
publishTimelineNode.AppId = appId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Agile.Config.Protocol;
using AgileConfig.Server.Common;
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.Data.Freesql;
using AgileConfig.Server.IService;

namespace AgileConfig.Server.Service.EventRegisterService;
Expand All @@ -25,30 +24,6 @@ public ConfigStatusUpdateRegister(
_appService = appService;
}

// private IServerNodeService NewServerNodeService()
// {
// return new ServerNodeService(new FreeSqlContext(FreeSQL.Instance));
// }

private IAppService NewAppService()
{
// todo

throw new NotImplementedException();

//return new AppService(new FreeSqlContext(FreeSQL.Instance));
}

private IConfigService NewConfigService()
{
// todo

throw new NotImplementedException();

//return new ConfigService(null, NewAppService(), new SettingService(new FreeSqlContext(FreeSQL.Instance)),
// new UserService(new FreeSqlContext(FreeSQL.Instance)));
}

public void Register()
{
TinyEventBus.Instance.Register(EventKeys.PUBLISH_CONFIG_SUCCESS, (param) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using AgileConfig.Server.Common;
using AgileConfig.Server.Common.RestClient;
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.Data.Freesql;
using AgileConfig.Server.IService;
using Microsoft.Extensions.Logging;

Expand All @@ -17,36 +16,33 @@ internal class ServiceInfoStatusUpdateRegister : IEventRegister
{
private readonly IRemoteServerNodeProxy _remoteServerNodeProxy;
private readonly IRestClient _restClient;
private readonly EventRegisterTransient<IServerNodeService> _serverNodeService;
private readonly EventRegisterTransient<IServiceInfoService> _serviceInfoService;
private ILogger _logger;

public ServiceInfoStatusUpdateRegister(
IRemoteServerNodeProxy remoteServerNodeProxy,
ILoggerFactory loggerFactory,
IRestClient restClient
IRestClient restClient,
EventRegisterTransient<IServerNodeService> serverNodeService,
EventRegisterTransient<IServiceInfoService> serviceInfoService
)
{
_remoteServerNodeProxy = remoteServerNodeProxy;
_restClient = restClient;
_serverNodeService = serverNodeService;
_serviceInfoService = serviceInfoService;
_logger = loggerFactory.CreateLogger<ServiceInfoStatusUpdateRegister>();
}

private IServerNodeService NewServerNodeService()
{
return new ServerNodeService(new FreeSqlContext(FreeSQL.Instance));
}

private IServiceInfoService NewServiceInfoService()
{
return new ServiceInfoService(null);
}


public void Register()
{
TinyEventBus.Instance.Register(EventKeys.REGISTER_A_SERVICE, (param) =>
{
Task.Run(async () =>
{
using var serverNodeService = NewServerNodeService();
using var serverNodeService = _serverNodeService();
var serverNodes = await serverNodeService.GetAllNodesAsync();
foreach (var serverNode in serverNodes.Where(x => x.Status == NodeStatus.Online))
{
Expand All @@ -66,7 +62,7 @@ public void Register()
{
Task.Run(async () =>
{
using var serverNodeService = NewServerNodeService();
using var serverNodeService = _serverNodeService();
var serverNodes = await serverNodeService.GetAllNodesAsync();
foreach (var serverNode in serverNodes.Where(x => x.Status == NodeStatus.Online))
{
Expand All @@ -86,7 +82,7 @@ public void Register()
{
Task.Run(async () =>
{
using var serverNodeService = NewServerNodeService();
using var serverNodeService = _serverNodeService();
var serverNodes = await serverNodeService.GetAllNodesAsync();
foreach (var serverNode in serverNodes.Where(x => x.Status == NodeStatus.Online))
{
Expand All @@ -108,7 +104,7 @@ public void Register()
{
dynamic paramObj = param;
string id = paramObj.UniqueId;
using var serviceInfoService = NewServiceInfoService();
using var serviceInfoService = _serviceInfoService();
var service = await serviceInfoService.GetByUniqueIdAsync(id);
if (service != null && !string.IsNullOrWhiteSpace(service.AlarmUrl) &&
service.Status == ServiceStatus.Unhealthy)
Expand Down
Loading

0 comments on commit e2f8137

Please sign in to comment.