我在我的实现中使用存储库模式,并添加了ApiResourceRepository。以下是一个示例:
public class ApiResourceRepository : IApiResourceRepository
{
private readonly IServiceProvider _serviceProvider;
public ApiResourceRepository(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_dbSettings = dbSettings;
}
public async Task<ApiResource> Get(int id)
{
ApiResource result;
using (var serviceScope = _serviceProvider.CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
result = await
context.ApiResources
.Include(x => x.Scopes)
.Include(x => x.UserClaims)
.FirstOrDefaultAsync(x => x.Id == id);
}
return result;
}
}
这是获取/使用ConfigurationDbContext
的建议方法吗?
我也在努力进行集成测试,因为我似乎必须模拟一个可以提供ConfigurationDbContext
实例的IServiceProvider?你如何解决这些问题?