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

前端 未结 1 1448
予麋鹿
予麋鹿 2021-01-14 23:35

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

相关标签:
1条回答
  • 2021-01-15 00:15

    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.

    0 讨论(0)
提交回复
热议问题