-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c20c395
commit f819c15
Showing
18 changed files
with
548 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#### Async Root | ||
|
||
[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Basics/AsyncRootScenario.cs) | ||
|
||
|
||
```c# | ||
interface IDependency; | ||
|
||
class Dependency : IDependency; | ||
|
||
interface IService; | ||
|
||
class Service(IDependency dependency) : IService; | ||
|
||
DI.Setup(nameof(Composition)) | ||
.Bind<IDependency>().To<Dependency>() | ||
.Bind<IService>().To<Service>() | ||
|
||
// Composition root | ||
.Root<Task<IService>>("GetMyServiceAsync", kind: RootKinds.Method); | ||
|
||
var composition = new Composition(); | ||
using var cancellationTokenSource = new CancellationTokenSource(); | ||
|
||
// Creates the composition root asynchronously | ||
var service = await composition.GetMyServiceAsync(); | ||
``` | ||
|
||
The following partial class will be generated: | ||
|
||
```c# | ||
partial class Composition | ||
{ | ||
private readonly Composition _root; | ||
private readonly object _lock; | ||
|
||
public Composition() | ||
{ | ||
_root = this; | ||
_lock = new object(); | ||
} | ||
|
||
internal Composition(Composition parentScope) | ||
{ | ||
_root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root; | ||
_lock = _root._lock; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public Task<IService> GetMyServiceAsync() | ||
{ | ||
var perResolveFunc40 = default(Func<IService>); | ||
TaskScheduler transientTaskScheduler5 = TaskScheduler.Default; | ||
TaskContinuationOptions transientTaskContinuationOptions4 = TaskContinuationOptions.None; | ||
TaskCreationOptions transientTaskCreationOptions3 = TaskCreationOptions.None; | ||
CancellationToken transientCancellationToken2 = CancellationToken.None; | ||
TaskFactory<IService> perBlockTaskFactory1; | ||
{ | ||
var localCancellationToken10 = transientCancellationToken2; | ||
var localTaskCreationOptions11 = transientTaskCreationOptions3; | ||
var localTaskContinuationOptions12 = transientTaskContinuationOptions4; | ||
var localTaskScheduler13 = transientTaskScheduler5; | ||
perBlockTaskFactory1 = new TaskFactory<IService>(localCancellationToken10, localTaskCreationOptions11, localTaskContinuationOptions12, localTaskScheduler13); | ||
} | ||
|
||
if (perResolveFunc40 == null) | ||
{ | ||
lock (_lock) | ||
{ | ||
if (perResolveFunc40 == null) | ||
{ | ||
perResolveFunc40 = new Func<IService>( | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
() => | ||
{ | ||
var localValue14 = new Service(new Dependency()); | ||
return localValue14; | ||
}); | ||
} | ||
} | ||
} | ||
|
||
Task<IService> transientTask0; | ||
{ | ||
var localFactory15 = perResolveFunc40!; | ||
var localTaskFactory16 = perBlockTaskFactory1; | ||
transientTask0 = localTaskFactory16.StartNew(localFactory15); | ||
} | ||
|
||
return transientTask0; | ||
} | ||
} | ||
``` | ||
|
||
Class diagram: | ||
|
||
```mermaid | ||
classDiagram | ||
class Composition { | ||
<<partial>> | ||
+TaskᐸIServiceᐳ GetMyServiceAsync() | ||
} | ||
class CancellationToken | ||
class TaskScheduler | ||
class TaskCreationOptions | ||
class TaskContinuationOptions | ||
class TaskFactory | ||
Dependency --|> IDependency | ||
class Dependency { | ||
+Dependency() | ||
} | ||
Service --|> IService | ||
class Service { | ||
+Service(IDependency dependency) | ||
} | ||
class FuncᐸIServiceᐳ | ||
class TaskFactoryᐸIServiceᐳ | ||
class IDependency { | ||
<<interface>> | ||
} | ||
class IService { | ||
<<interface>> | ||
} | ||
TaskFactory *-- CancellationToken : CancellationToken | ||
TaskFactory *-- TaskCreationOptions : TaskCreationOptions | ||
TaskFactory *-- TaskContinuationOptions : TaskContinuationOptions | ||
TaskFactory *-- TaskScheduler : TaskScheduler | ||
Service *-- Dependency : IDependency | ||
Composition ..> TaskᐸIServiceᐳ : TaskᐸIServiceᐳ GetMyServiceAsync() | ||
TaskᐸIServiceᐳ o-- "PerResolve" FuncᐸIServiceᐳ : FuncᐸIServiceᐳ | ||
TaskᐸIServiceᐳ o-- "PerBlock" TaskFactoryᐸIServiceᐳ : TaskFactoryᐸIServiceᐳ | ||
FuncᐸIServiceᐳ *-- Service : IService | ||
TaskFactoryᐸIServiceᐳ *-- CancellationToken : CancellationToken | ||
TaskFactoryᐸIServiceᐳ *-- TaskCreationOptions : TaskCreationOptions | ||
TaskFactoryᐸIServiceᐳ *-- TaskContinuationOptions : TaskContinuationOptions | ||
TaskFactoryᐸIServiceᐳ *-- TaskScheduler : TaskScheduler | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.