What is the Work<> class for in Orchard CMS?

情到浓时终转凉″ 提交于 2019-12-04 01:55:04

问题


Plain and simple, what is the use case of the Orchard.Environment.Work<> class defined in Orchard\Environment\WorkContextModule.cs?

It can be found in several places like

private readonly Work<IContainerService> _containerService;

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

Is it for delayed resolution of IContainerService?


回答1:


The Work class is for lazy loading dependency injection. The dependency is not resolved when instantiating the class, but only when calling the Value property:

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;
}

Now only when _myService.Value is called, the IMyService gets resolved by the Dependency resolver, which gives you the working of a lazy loading dependency injection.



来源:https://stackoverflow.com/questions/36307200/what-is-the-work-class-for-in-orchard-cms

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!