为 CRUD 操作创建基础服务

Creating a basic service for CRUD operations

我写了下面的代码来总结我的 DomainServices 中的所有 CRUD 操作,但它在第一次使用时抛出错误。

// generic class

public class GenericManager<T>  : DomainService where T : class, IEntity<int>
{
    public readonly IRepository<T, int> _genericRepository;

    public async Task<T> GetAsync(int id) => await AsyncExecuter.FirstOrDefaultAsync(_genericRepository.Where(m => m.Id == id));
    public async Task AddAsync(T entity) => await _genericRepository.InsertAsync(entity);
    public async Task AddManyAsync(IEnumerable<T> entities) => await _genericRepository.InsertManyAsync(entities);
    public async Task UpdateAsync(T entity) => await _genericRepository.UpdateAsync(entity);
    public async Task UpdateManyAsync(IEnumerable<T> entities) => await _genericRepository.UpdateManyAsync(entities);
    public async Task DeleteAsync(T entity) => await _genericRepository.DeleteAsync(entity);
}

// my domain services
public class ClientManager : GenericManager<Client>
{
    public async Task<Client> GetClientByNameAsync(string userName) => await AsyncExecuter.FirstOrDefaultAsync(_genericRepository.Where(m => m.UserName == userName));
    public async Task<Client> FindClientWithVKID(string vkid) => await AsyncExecuter.FirstOrDefaultAsync(_genericRepository.Where(m => m.VkId == vkid));
}

// usage my domain service other code
var user = await _clientManager.GetAsync(userId);

但是由于程序的工作,出现了这样的错误

System.ArgumentNullException: Value cannot be null. (Parameter 'source')
at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
at RenewalTML.Data.ClientManager.GetClientByNameAsync(String userName) in 
C:\Users\Fearp\source\repos\RenewalTML\RenewalTML\Data\User\ClientManager.cs:line 15
at RenewalTML.Data.ClientAuthServices.LoginIsReady(UserLoginModel model) in 
C:\Users\Fearp\source\repos\RenewalTML\RenewalTML\Data\User\ClientAuthServices.cs:line 236
at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation 
invocation, IInvocationProceedInfo proceedInfo)
at 
Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
at Volo.Abp.GlobalFeatures.GlobalFeatureInterceptor.InterceptAsync(IAbpMethodInvocation 
invocation)
at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult] 
(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation 
invocation, IInvocationProceedInfo proceedInfo)
at 
Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
at Volo.Abp.Validation.ValidationInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult] 
(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation 
invocation, IInvocationProceedInfo proceedInfo)
at 
Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
at Volo.Abp.Auditing.AuditingInterceptor.ProceedByLoggingAsync(IAbpMethodInvocation 
invocation, IAuditingHelper auditingHelper, IAuditLogScope auditLogScope)
at Volo.Abp.Auditing.AuditingInterceptor.ProcessWithNewAuditingScopeAsync(IAbpMethodInvocation 
invocation, AbpAuditingOptions options, ICurrentUser currentUser, IAuditingManager 
auditingManager, IAuditingHelper auditingHelper)
at Volo.Abp.Auditing.AuditingInterceptor.ProcessWithNewAuditingScopeAsync(IAbpMethodInvocation 
invocation, AbpAuditingOptions options, ICurrentUser currentUser, IAuditingManager 
auditingManager, IAuditingHelper auditingHelper)
at Volo.Abp.Auditing.AuditingInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult] 
(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
at Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation 
invocation, IInvocationProceedInfo proceedInfo)
at 
Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue`1.ProceedAsync()
at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter`1.InterceptAsync[TResult] 
(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func`3 proceed)
at RenewalTML.Pages.Client.Authorization.LoginExecuted(EditContext editContext) in 
C:\Users\Fearp\source\repos\RenewalTML\RenewalTML\Pages\Client\Authorization.razor:line 227
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task 
task)
at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task 
task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle) 

我原本以为会出错。我怎样才能创建这样一个可行的代码,这样我就不会在我的应用程序中为 CRUD 操作编写相同的代码?

我用户:.NET 5.0 Blazor Web 服务器,EFCore + mysql Volo.Abp版本:4.3.3

似乎 public readonly IRepository<T, int> _genericRepository;null 因为你从来没有初始化它。因此,当在 ClientManager 中调用 _genericRepository.Where(m => m.UserName == userName) 时,您会得到一个 LINQ 异常,表明 source (_genericRepository) 是 null.

您可能需要在 ClientManager.

的构造函数中使用 DI 容器注入的实例初始化 genericRepository

更新

参考你正在使用的框架的相同文档,这基本上是缺失的部分。

可能您需要这样的东西:

public class ClientManager : GenericManager<Client>
{

    public ClientManager(IRepository<Client, Guid> clientRepository)
    {
        _genericRepository = clientRepository;
    }

    
...