optimise repository

master
Young 7 months ago
parent 45fda834b7
commit 3f31c53ba2

@ -12,7 +12,8 @@ public static class RepositoryContextSetup
public static IServiceCollection AddDefaultRepositoryContext(this IServiceCollection services) public static IServiceCollection AddDefaultRepositoryContext(this IServiceCollection services)
{ {
ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(services);
services.TryAddScoped(typeof(IRepositoryBase<>), typeof(RepositoryBase<>)); services.TryAddScoped<IUnitOfWork, UnitOfWork>();
services.TryAddScoped(typeof(IRepositoryBase<,>), typeof(RepositoryBase<,>));
return services; return services;
} }
} }

@ -33,8 +33,6 @@ public static class SqlSugarSetup
return services; return services;
} }
services.TryAddScoped(typeof(IRepositoryBase<>), typeof(RepositoryBase<>));
services.TryAddScoped<IUnitOfWork, UnitOfWork>();
if (sqlSugarOptions.SnowFlake?.IsEnable ?? false) if (sqlSugarOptions.SnowFlake?.IsEnable ?? false)
{ {
var workerId = configuration["SNOWFLAKES_WORKERID"]?.ObjToInt() ?? sqlSugarOptions.SnowFlake?.WorkerId; var workerId = configuration["SNOWFLAKES_WORKERID"]?.ObjToInt() ?? sqlSugarOptions.SnowFlake?.WorkerId;

@ -7,11 +7,12 @@ namespace Infrastructure.Repository;
/// 数据库ORM仓储 /// 数据库ORM仓储
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public interface IRepositoryBase<T> where T : class, new() /// <typeparam name="TId"></typeparam>
public interface IRepositoryBase<T, in TId> where T : class, new() where TId : IEquatable<TId>
{ {
ISqlSugarClient DbContext { get; } ISqlSugarClient DbContext { get; }
Task<T?> GetByIdAsync(long id); Task<T?> GetByIdAsync(TId id);
Task<List<T?>> GetAllAsync(); Task<List<T?>> GetAllAsync();

@ -7,11 +7,12 @@ namespace Infrastructure.Repository;
/// 数据库访问层 /// 数据库访问层
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public interface IServiceBase<T> where T : class, new() /// <typeparam name="TId"></typeparam>
public interface IServiceBase<T, in TId> where T : class, new() where TId : IEquatable<TId>
{ {
IRepositoryBase<T> DAL { get; } IRepositoryBase<T,TId> DAL { get; }
Task<T?> GetByIdAsync(long id); Task<T?> GetByIdAsync(TId id);
Task<List<T?>> GetAllAsync(); Task<List<T?>> GetAllAsync();

@ -9,11 +9,12 @@ namespace Infrastructure.Repository;
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public class RepositoryBase<T>(ISqlSugarClient context) : IRepositoryBase<T> where T : class, new() /// <typeparam name="TId"></typeparam>
public class RepositoryBase<T,TId>(ISqlSugarClient context) : IRepositoryBase<T,TId> where T : class, new() where TId : IEquatable<TId>
{ {
public ISqlSugarClient DbContext => context; public ISqlSugarClient DbContext => context;
public async Task<T?> GetByIdAsync(long id) public async Task<T?> GetByIdAsync(TId id)
{ {
return await context.Queryable<T>().InSingleAsync(id); return await context.Queryable<T>().InSingleAsync(id);
} }

@ -4,15 +4,17 @@ using SqlSugar;
namespace Infrastructure.Repository; namespace Infrastructure.Repository;
/// <summary> /// <summary>
/// <inheritdoc cref="IServiceBase{T}"/> /// <inheritdoc cref="IServiceBase{T,TId}"/>
/// </summary> /// </summary>
/// <param name="dbContext"></param> /// <param name="dbContext"></param>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
public abstract class ServiceBase<T>(IRepositoryBase<T> dbContext) : IServiceBase<T> where T : class, new() /// <typeparam name="TId"></typeparam>
public abstract class ServiceBase<T, TId>(IRepositoryBase<T, TId> dbContext)
: IServiceBase<T, TId> where T : class, new() where TId : IEquatable<TId>
{ {
public IRepositoryBase<T> DAL { get; } = dbContext; public IRepositoryBase<T, TId> DAL { get; } = dbContext;
public async Task<T?> GetByIdAsync(long id) public async Task<T?> GetByIdAsync(TId id)
{ {
return await DAL.GetByIdAsync(id); return await DAL.GetByIdAsync(id);
} }

Loading…
Cancel
Save