我在重构某些代码时遇到了这个问题,这在很大程度上取决于Disposable模式。
我确实有一个只读字段DbContext,正在构造函数中对其进行初始化。现在,我想知道是否可以将字段传递到using块中,以使其以正确的方式处理。还是我必须摆脱字段和初始化才能在每个using块中对其进行初始化
protected readonly DbContext _context;
public Repository(DbContext context)
{
_context = context;
}
public Task<T> GetAsync(Guid id)
{
using (_context)
{
return _context.Set<T>().FindAsync(new CancellationToken(), id);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望以正确的方式应用Dispose模式