I would like to reuse the same base repository in ASP.NET Web API and also in a worker project. Of course in a worker we cannot use scoped services so need to make sure it is thread safe.
Is the example shown below thread safe to use when registering the context as a singleton?
Base repository class as shown (not whole class shown)
public class RepositoryBase<TContext> : IRepositoryBase<TContext> where TContext : DbContext, new()
{
public Func<TContext> GetContext;
public RepositoryBase()
{
GetContext = GetCurrentContext;
}
protected TContext GetCurrentContext()
{
return new TContext();
}
public IQueryable<TEntity> QueryAll<TEntity>() where TEntity : class
{
return GetDbSet<TEntity>().AsQueryable();
}
public void AddRange<TEntity>(IEnumerable<TEntity> entities) where TEntity : class
{
GetDbSet<TEntity>().AddRange(entities);
}
public async Task SaveChangesAsync()
{
await Context.SaveChangesAsync();
}
}
Here is my worker repository base class
public class WorkerRepositoryBase<TContext> : RepositoryBase<TContext>, IWorkerRepositoryBase<TContext> where TContext : DbContext, new()
{
private readonly IServiceProvider _serviceProvider;
public WorkerRepositoryBase(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
protected override TContext GetCurrentContext()
{
using var scope = _serviceProvider.CreateScope();
return scope.ServiceProvider.GetRequiredService<TContext>();
}
}
Sorry for the silly question, I somehow missed that I CAN use scoped services in a worker.... https://learn.microsoft.com/en-us/dotnet/core/extensions/scoped-service?pivots=dotnet-7-0.