From 53bcc6b5c2be71067c95cb596f4bf5b980f73235 Mon Sep 17 00:00:00 2001 From: Young Date: Sun, 13 Oct 2024 16:00:23 +0800 Subject: [PATCH] added default repository setup --- .../Extensions/RepositoryContextSetup.cs | 14 +++++ src/Infrastructure/Repository/IServiceBase.cs | 30 +++++++++ src/Infrastructure/Repository/ServiceBase.cs | 61 +++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 src/Infrastructure/Extensions/RepositoryContextSetup.cs create mode 100644 src/Infrastructure/Repository/IServiceBase.cs create mode 100644 src/Infrastructure/Repository/ServiceBase.cs diff --git a/src/Infrastructure/Extensions/RepositoryContextSetup.cs b/src/Infrastructure/Extensions/RepositoryContextSetup.cs new file mode 100644 index 0000000..8462c3f --- /dev/null +++ b/src/Infrastructure/Extensions/RepositoryContextSetup.cs @@ -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; + } +} \ No newline at end of file diff --git a/src/Infrastructure/Repository/IServiceBase.cs b/src/Infrastructure/Repository/IServiceBase.cs new file mode 100644 index 0000000..538ff81 --- /dev/null +++ b/src/Infrastructure/Repository/IServiceBase.cs @@ -0,0 +1,30 @@ +using System.Linq.Expressions; +using SqlSugar; + +namespace Infrastructure.Repository; + +public interface IServiceBase where T : class, new() +{ + IRepositoryBase DAL { get; } + + Task GetByIdAsync(long id); + + Task> GetAllAsync(); + + Task GetFirstByExpressionAsync(Expression> expression); + + Task AddSnowflakeAsync(T entity); + + Task> AddSnowflakesAsync(IList entities); + + Task> QueryPageAsync(Expression>? whereExpression, int pageIndex = 1, int pageSize = 20, + Expression>? orderExpression = null, OrderByType orderByType = OrderByType.Asc); + + Task UpdateColumnsAsync(T entity, Expression> expression); + + Task DeleteByIdAsync(long id); + + Task DeleteAsync(T entity); + + Task DeletedByExpressionAsync(Expression> expression); +} \ No newline at end of file diff --git a/src/Infrastructure/Repository/ServiceBase.cs b/src/Infrastructure/Repository/ServiceBase.cs new file mode 100644 index 0000000..93f9a18 --- /dev/null +++ b/src/Infrastructure/Repository/ServiceBase.cs @@ -0,0 +1,61 @@ +using System.Linq.Expressions; +using SqlSugar; + +namespace Infrastructure.Repository; + +public abstract class ServiceBase(IRepositoryBase dbContext) : IServiceBase where T : class, new() +{ + public IRepositoryBase DAL { get; } = dbContext; + + public async Task GetByIdAsync(long id) + { + return await DAL.GetByIdAsync(id); + } + + public async Task AddSnowflakeAsync(T entity) + { + return await DAL.AddSnowflakeAsync(entity); + } + + public async Task> AddSnowflakesAsync(IList entities) + { + return await DAL.AddSnowflakesAsync(entities); + } + + public async Task> QueryPageAsync(Expression>? whereExpression, int pageIndex = 1, + int pageSize = 20, + Expression>? orderExpression = null, OrderByType orderByType = OrderByType.Asc) + { + return await DAL.QueryPageAsync(whereExpression, pageIndex, pageSize, orderExpression, orderByType); + } + + public async Task> GetAllAsync() + { + return await DAL.GetAllAsync(); + } + + public async Task GetFirstByExpressionAsync(Expression> expression) + { + return await DAL.GetFirstByExpressionAsync(expression); + } + + public async Task UpdateColumnsAsync(T entity, Expression> expression) + { + return await DAL.UpdateColumnsAsync(entity, expression); + } + + public async Task DeleteByIdAsync(long id) + { + return await DAL.DeleteByIdAsync(id); + } + + public async Task DeleteAsync(T entity) + { + return await DAL.DeleteAsync(entity); + } + + public async Task DeletedByExpressionAsync(Expression> expression) + { + return await DAL.DeletedByExpressionAsync(expression); + } +} \ No newline at end of file