如何从 DryIoc 中解析的实例获取隐式创建的范围?
How to get implicitly created scope from resolved instance in DryIoc?
我们将工作单元作为 ViewModel 的外部依赖项。 ViewModel 和 UnitOfWork 都实现了用于清理的 IDisposable 接口。 ViewModel 使用 UnitOfWork,但不释放它:
public class UnitOfWork: IDisposable
{
public void Dispose()
{
// some clean-up code here.
}
}
public class ViewModel: IDisposable
{
private readonly UnitOfWork _unitOfWork;
public ViewModel(UnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public void Dispose()
{
// some clean-up code here, but NO _unitOfWork.Dispose() because ViewModel does not create UnitOfWork.
}
}
ViewModel 是暂时的:每次调用 DI 容器的 Resolve() 方法时都应创建新实例。 ViewModel 由无法访问 DI 容器的外部代码处理。目前,UnitOfWork 是有范围的:只应为一个 ViewModel 创建一个实例,并且应在 ViewModel 的 Dispose() 上调用 Dispose()。
我们使用 DryIoc 4.0.7。 documentation says 当前解析范围可以作为 IDisposable 对象注入到构造函数中。所以我们改变我们的 ViewModel:
public class ViewModel: IDisposable
{
private readonly UnitOfWork _unitOfWork;
private readonly IDisposable _scope;
public ViewModel(UnitOfWork unitOfWork, IDisposable scope)
{
_unitOfWork = unitOfWork;
_scope = scope;
}
public void Dispose()
{
// some clean-up code here.
_scope.Dispose(); // we also dispose current scope.
}
}
我们的组合根现在看起来像这样:
var container = new Container();
container.Register<UnitOfWork>(reuse: Reuse.Scoped);
container.Register<ViewModel>(
setup: Setup.With(openResolutionScope: true, allowDisposableTransient: true));
我看不出这段代码与文档中提供的代码有何不同,但它在 Resolve() 方法上抛出异常:
DryIoc.ContainerException: 'Unable to resolve IDisposable as parameter "scope"
in ViewModel FactoryId=53 IsResolutionCall
from Container with Scope {Name={ServiceType=ViewModel}}
with Rules with {AutoConcreteTypeResolution}
with Made={FactoryMethod=ConstructorWithResolvableArguments}
Where no service registrations found
and no dynamic registrations found in 0 of Rules.DynamicServiceProviders
and nothing found in 1 of Rules.UnknownServiceResolvers'
我们缺少什么?文档是否过时?有没有另一种方法来获得隐式创建的范围来处置它们?
此文档已过时 - 我已打开 respective issue。
当前范围可以注入为IResolverContext
:
public ViewModel(UnitOfWork unitOfWork, IResolverContext scopedContext) {...}
我们将工作单元作为 ViewModel 的外部依赖项。 ViewModel 和 UnitOfWork 都实现了用于清理的 IDisposable 接口。 ViewModel 使用 UnitOfWork,但不释放它:
public class UnitOfWork: IDisposable
{
public void Dispose()
{
// some clean-up code here.
}
}
public class ViewModel: IDisposable
{
private readonly UnitOfWork _unitOfWork;
public ViewModel(UnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public void Dispose()
{
// some clean-up code here, but NO _unitOfWork.Dispose() because ViewModel does not create UnitOfWork.
}
}
ViewModel 是暂时的:每次调用 DI 容器的 Resolve() 方法时都应创建新实例。 ViewModel 由无法访问 DI 容器的外部代码处理。目前,UnitOfWork 是有范围的:只应为一个 ViewModel 创建一个实例,并且应在 ViewModel 的 Dispose() 上调用 Dispose()。 我们使用 DryIoc 4.0.7。 documentation says 当前解析范围可以作为 IDisposable 对象注入到构造函数中。所以我们改变我们的 ViewModel:
public class ViewModel: IDisposable
{
private readonly UnitOfWork _unitOfWork;
private readonly IDisposable _scope;
public ViewModel(UnitOfWork unitOfWork, IDisposable scope)
{
_unitOfWork = unitOfWork;
_scope = scope;
}
public void Dispose()
{
// some clean-up code here.
_scope.Dispose(); // we also dispose current scope.
}
}
我们的组合根现在看起来像这样:
var container = new Container();
container.Register<UnitOfWork>(reuse: Reuse.Scoped);
container.Register<ViewModel>(
setup: Setup.With(openResolutionScope: true, allowDisposableTransient: true));
我看不出这段代码与文档中提供的代码有何不同,但它在 Resolve() 方法上抛出异常:
DryIoc.ContainerException: 'Unable to resolve IDisposable as parameter "scope"
in ViewModel FactoryId=53 IsResolutionCall
from Container with Scope {Name={ServiceType=ViewModel}}
with Rules with {AutoConcreteTypeResolution}
with Made={FactoryMethod=ConstructorWithResolvableArguments}
Where no service registrations found
and no dynamic registrations found in 0 of Rules.DynamicServiceProviders
and nothing found in 1 of Rules.UnknownServiceResolvers'
我们缺少什么?文档是否过时?有没有另一种方法来获得隐式创建的范围来处置它们?
此文档已过时 - 我已打开 respective issue。
当前范围可以注入为IResolverContext
:
public ViewModel(UnitOfWork unitOfWork, IResolverContext scopedContext) {...}