什么是工作<>在Orchard CMS中的课程?

时间:2016-03-30 11:16:52

标签: c# orchardcms

简单明了,Orchard.Environment.Work<>中定义的Orchard\Environment\WorkContextModule.cs类的用例是什么?

可以在几个地方找到,比如

private readonly Work<IContainerService> _containerService;

public Shapes(Work<IContainerService> containerService) {
  _containerService = containerService;
...

是否延迟解决IContainerService

1 个答案:

答案 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,这样就可以处理延迟加载依赖注入。