diff --git a/src/Infrastructure/Extensions/RepositoryContextSetup.cs b/src/Infrastructure/Extensions/RepositoryContextSetup.cs index 721cc76..2592f28 100644 --- a/src/Infrastructure/Extensions/RepositoryContextSetup.cs +++ b/src/Infrastructure/Extensions/RepositoryContextSetup.cs @@ -12,7 +12,8 @@ public static class RepositoryContextSetup public static IServiceCollection AddDefaultRepositoryContext(this IServiceCollection services) { ArgumentNullException.ThrowIfNull(services); - services.TryAddScoped(typeof(IRepositoryBase<>), typeof(RepositoryBase<>)); + services.TryAddScoped(); + services.TryAddScoped(typeof(IRepositoryBase<,>), typeof(RepositoryBase<,>)); return services; } } \ No newline at end of file diff --git a/src/Infrastructure/Extensions/SqlSugarSetup.cs b/src/Infrastructure/Extensions/SqlSugarSetup.cs index 72231b0..000940e 100644 --- a/src/Infrastructure/Extensions/SqlSugarSetup.cs +++ b/src/Infrastructure/Extensions/SqlSugarSetup.cs @@ -33,8 +33,6 @@ public static class SqlSugarSetup return services; } - services.TryAddScoped(typeof(IRepositoryBase<>), typeof(RepositoryBase<>)); - services.TryAddScoped(); if (sqlSugarOptions.SnowFlake?.IsEnable ?? false) { var workerId = configuration["SNOWFLAKES_WORKERID"]?.ObjToInt() ?? sqlSugarOptions.SnowFlake?.WorkerId; diff --git a/src/Infrastructure/Repository/IRepositoryBase.cs b/src/Infrastructure/Repository/IRepositoryBase.cs index 7fc690b..c256c78 100644 --- a/src/Infrastructure/Repository/IRepositoryBase.cs +++ b/src/Infrastructure/Repository/IRepositoryBase.cs @@ -7,11 +7,12 @@ namespace Infrastructure.Repository; /// 数据库ORM仓储 /// /// -public interface IRepositoryBase where T : class, new() +/// +public interface IRepositoryBase where T : class, new() where TId : IEquatable { ISqlSugarClient DbContext { get; } - Task GetByIdAsync(long id); + Task GetByIdAsync(TId id); Task> GetAllAsync(); diff --git a/src/Infrastructure/Repository/IServiceBase.cs b/src/Infrastructure/Repository/IServiceBase.cs index 1541c19..4632e57 100644 --- a/src/Infrastructure/Repository/IServiceBase.cs +++ b/src/Infrastructure/Repository/IServiceBase.cs @@ -7,11 +7,12 @@ namespace Infrastructure.Repository; /// 数据库访问层 /// /// -public interface IServiceBase where T : class, new() +/// +public interface IServiceBase where T : class, new() where TId : IEquatable { - IRepositoryBase DAL { get; } + IRepositoryBase DAL { get; } - Task GetByIdAsync(long id); + Task GetByIdAsync(TId id); Task> GetAllAsync(); diff --git a/src/Infrastructure/Repository/RepositoryBase.cs b/src/Infrastructure/Repository/RepositoryBase.cs index 7d3c96a..86cad03 100644 --- a/src/Infrastructure/Repository/RepositoryBase.cs +++ b/src/Infrastructure/Repository/RepositoryBase.cs @@ -9,11 +9,12 @@ namespace Infrastructure.Repository; /// /// /// -public class RepositoryBase(ISqlSugarClient context) : IRepositoryBase where T : class, new() +/// +public class RepositoryBase(ISqlSugarClient context) : IRepositoryBase where T : class, new() where TId : IEquatable { public ISqlSugarClient DbContext => context; - public async Task GetByIdAsync(long id) + public async Task GetByIdAsync(TId id) { return await context.Queryable().InSingleAsync(id); } diff --git a/src/Infrastructure/Repository/ServiceBase.cs b/src/Infrastructure/Repository/ServiceBase.cs index 3a9f3ef..89df025 100644 --- a/src/Infrastructure/Repository/ServiceBase.cs +++ b/src/Infrastructure/Repository/ServiceBase.cs @@ -4,15 +4,17 @@ using SqlSugar; namespace Infrastructure.Repository; /// -/// +/// /// /// /// -public abstract class ServiceBase(IRepositoryBase dbContext) : IServiceBase where T : class, new() +/// +public abstract class ServiceBase(IRepositoryBase dbContext) + : IServiceBase where T : class, new() where TId : IEquatable { - public IRepositoryBase DAL { get; } = dbContext; + public IRepositoryBase DAL { get; } = dbContext; - public async Task GetByIdAsync(long id) + public async Task GetByIdAsync(TId id) { return await DAL.GetByIdAsync(id); }