added default repository setup

master
Young 7 months ago
parent a82cb8f47d
commit 53bcc6b5c2

@ -0,0 +1,14 @@
using Infrastructure.Repository;
namespace Infrastructure.Extensions;
public static class RepositoryContextSetup
{
public static IServiceCollection AddDefaultRepositoryContext(this IServiceCollection services)
{
ArgumentNullException.ThrowIfNull(services);
services.TryAddScoped(typeof(IRepositoryBase<>), typeof(RepositoryBase<>));
services.TryAddScoped(typeof(IServiceBase<>), typeof(ServiceBase<>));
return services;
}
}

@ -0,0 +1,30 @@
using System.Linq.Expressions;
using SqlSugar;
namespace Infrastructure.Repository;
public interface IServiceBase<T> where T : class, new()
{
IRepositoryBase<T> DAL { get; }
Task<T?> GetByIdAsync(long id);
Task<List<T?>> GetAllAsync();
Task<T?> GetFirstByExpressionAsync(Expression<Func<T, bool>> expression);
Task<long> AddSnowflakeAsync(T entity);
Task<IList<long>> AddSnowflakesAsync(IList<T> entities);
Task<PageData<T?>> QueryPageAsync(Expression<Func<T, bool>>? whereExpression, int pageIndex = 1, int pageSize = 20,
Expression<Func<T, object>>? orderExpression = null, OrderByType orderByType = OrderByType.Asc);
Task<bool> UpdateColumnsAsync(T entity, Expression<Func<T, object>> expression);
Task<bool> DeleteByIdAsync(long id);
Task<bool> DeleteAsync(T entity);
Task<bool> DeletedByExpressionAsync(Expression<Func<T, bool>> expression);
}

@ -0,0 +1,61 @@
using System.Linq.Expressions;
using SqlSugar;
namespace Infrastructure.Repository;
public abstract class ServiceBase<T>(IRepositoryBase<T> dbContext) : IServiceBase<T> where T : class, new()
{
public IRepositoryBase<T> DAL { get; } = dbContext;
public async Task<T?> GetByIdAsync(long id)
{
return await DAL.GetByIdAsync(id);
}
public async Task<long> AddSnowflakeAsync(T entity)
{
return await DAL.AddSnowflakeAsync(entity);
}
public async Task<IList<long>> AddSnowflakesAsync(IList<T> entities)
{
return await DAL.AddSnowflakesAsync(entities);
}
public async Task<PageData<T?>> QueryPageAsync(Expression<Func<T, bool>>? whereExpression, int pageIndex = 1,
int pageSize = 20,
Expression<Func<T, object>>? orderExpression = null, OrderByType orderByType = OrderByType.Asc)
{
return await DAL.QueryPageAsync(whereExpression, pageIndex, pageSize, orderExpression, orderByType);
}
public async Task<List<T?>> GetAllAsync()
{
return await DAL.GetAllAsync();
}
public async Task<T?> GetFirstByExpressionAsync(Expression<Func<T, bool>> expression)
{
return await DAL.GetFirstByExpressionAsync(expression);
}
public async Task<bool> UpdateColumnsAsync(T entity, Expression<Func<T, object>> expression)
{
return await DAL.UpdateColumnsAsync(entity, expression);
}
public async Task<bool> DeleteByIdAsync(long id)
{
return await DAL.DeleteByIdAsync(id);
}
public async Task<bool> DeleteAsync(T entity)
{
return await DAL.DeleteAsync(entity);
}
public async Task<bool> DeletedByExpressionAsync(Expression<Func<T, bool>> expression)
{
return await DAL.DeletedByExpressionAsync(expression);
}
}
Loading…
Cancel
Save