|
5 | 5 | ## Table Of Contents |
6 | 6 |
|
7 | 7 | * Introduction |
8 | | - * <a href="#async-and-di">Async in DI</a> |
| 8 | + * <a href="#async-and-di">`async` in DI</a> |
9 | 9 | * <a href="#example">Example</a> |
10 | 10 | * Advanced |
11 | 11 | * <a href="#static-memory-pool">Static Memory Pools</a> |
|
16 | 16 | ## Introduction |
17 | 17 | ### <a id="async-and-di"></a>Async in DI |
18 | 18 |
|
19 | | -In dependency injection, the injector resolves dependencies of the target class only once, often after class is first created. In other words, injection is a one time process that does not track the injected dependencies to update them later on. If a dependency is not ready at the moment of injection; either binding would not resolve in case of optional bindings or fail completely throwing an error. |
| 19 | +In dependency injection, the injector resolves dependencies of the target class only once, often after class is first created. In other words, injection is a one time process that does not track the injected dependencies to update them later on. If a dependency is not ready at the moment of injection, either the binding wouldn't resolve in case of optional bindings or would fail completely throwing an error. |
20 | 20 |
|
21 | | -This is creates a dilemma when implementing dependencies that are resolved asyncroniously. You can design around the DI limitations by carefully architecting your code so that the injection happens after async process is completed. This requires careful planning, increased complexity in setup. It is also prone to errors. |
| 21 | +This creates a dilemma while implementing dependencies that are resolved asynchronous. You can design around the DI limitations by carefully designing your code so that the injection happens after the `async` process is completed. This requires careful planning, which leads to an increased complexity in the setup, and is also prone to errors. |
22 | 22 |
|
23 | | -Alternatively you can inject a intermediary object that tracks the result of the async operation. When you need to access the dependency, you can use this intermediary object to check if async task is completed and get the resulting object. With the experimental async support, we would like to provide ways to tackle this problem in Extenject. You can find Async extensions in **Plugins/Zenject/OptionalExtras/Async** folder. |
| 23 | +Alternatively you can inject an intermediary object that tracks the result of the `async` operation. When you need to access the dependency, you can use this intermediary object to check if the `async` task is completed and get the resulting object. With the experimental `async` support, we would like to provide ways to tackle this problem in Extenject. You can find `async` extensions in the folder **Plugins/Zenject/OptionalExtras/Async**. |
24 | 24 |
|
25 | 25 | ### <a id="example"></a>Example |
26 | 26 |
|
@@ -69,13 +69,13 @@ public class Bar : IInitializable, IDisposable |
69 | 69 | } |
70 | 70 | ``` |
71 | 71 |
|
72 | | -Here we use `BindAsync<IFoo>().FromMethod()` to pass an async method delegate that waits for 100 ms and then returns a newly created `Foo` object. This method can be any other method with `async Task<T> Method()` signature. `BindAsync<T>` extension provides a separate binder for async operations. This binder is currently limited to a few `FromX()` providers. Features like Pooling and Factories are not supported at the moment. |
| 72 | +Here we use `BindAsync<IFoo>().FromMethod()` to pass an `async` lambda that waits for 100 ms and then returns a newly created `Foo` instance. This method can be any other method with the signature `Task<T> Method()`. *Note: the `async` keyword is an implementation detail and thus not part of the signature. The `BindAsync<T>` extension method provides a separate binder for `async` operations. This binder is currently limited to a few `FromX()` providers. Features like Pooling and Factories are not supported at the moment. |
73 | 73 |
|
74 | | -With above binding `AsyncInject<IFoo>` object is added to the container. Since scope is set to `AsCached()` the operation will run only once and `AsyncInject<IFoo>` will keep the result. It is important to note that async operation will not start before this binding is resolved. If you want async operation to start immediately after installing use `NonLazy()` option. |
| 74 | +With the above `AsyncInject<IFoo>` binding, the instance is added to the container. Since the scope is set to `AsCached()` the operation will run only once and `AsyncInject<IFoo>` will keep the result. It is important to note that `async` operations won't start before this binding is getting resolved. If you want `async` operation to start immediately after installing, use `NonLazy()` option. |
75 | 75 |
|
76 | | -Once injected to `Bar`, we can check whether the return value of the async operation is already available by `TryGetResult`. method. This method will return false if there is no result to return. If result is not ready yet, we can listen to `Completed` event to get the return value when async operation completes. |
| 76 | +Once injected to `Bar`, we can check whether the return value of the `async` operation is already available by calling the `TryGetResult`. method. This method will return `false` if there is no result to return. If result is not ready yet, we can listen to the `Completed` event to get the return value when the `async` operation completes. |
77 | 77 |
|
78 | | -Alternatively we can use following methods to check result. |
| 78 | +Alternatively we can use the following methods to check the results availability. |
79 | 79 | ```csharp |
80 | 80 | // Use HasResult to check if result exists |
81 | 81 | if (_asyncFoo.HasResult) |
|
0 commit comments