简单明了,Orchard.Environment.Work<>
中定义的Orchard\Environment\WorkContextModule.cs
类的用例是什么?
可以在几个地方找到,比如
private readonly Work<IContainerService> _containerService;
public Shapes(Work<IContainerService> containerService) {
_containerService = containerService;
...
是否延迟解决IContainerService
?
答案 0 :(得分:6)
Work
类用于延迟加载依赖注入。实例化类时,依赖性未得到解决,但仅在调用Value
属性时才解决:
private readonly IMyService _myService;
private readonly IMyOtherService _myOtherService;
public MyClass(Work<IMyService> myService, IMyOtherService myOtherService) {
// Just assign the Work class to the backing property
// The dependency won't be resolved until '_myService.Value' is called
_myService = myService;
// The IMyOtherService is resolved and assigned to the _myOtherService property
_myOtherService = myOtherService;
}
现在只有在调用_myService.Value
时,依赖性解析器才能解析IMyService,这样就可以处理延迟加载依赖注入。