Skip to content

Commit

Permalink
Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed May 17, 2024
1 parent c20c395 commit f819c15
Show file tree
Hide file tree
Showing 18 changed files with 548 additions and 43 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ dotnet run
- [Default values](readme/default-values.md)
- [Required properties or fields](readme/required-properties-or-fields.md)
- [Root binding](readme/root-binding.md)
- [Async Root](readme/async-root.md)
### Lifetimes
- [Transient](readme/transient.md)
- [Singleton](readme/singleton.md)
Expand Down Expand Up @@ -244,6 +245,7 @@ dotnet run
- [Generic composition roots](readme/generic-composition-roots.md)
- [Complex generics](readme/complex-generics.md)
- [Generic composition roots with constraints](readme/generic-composition-roots-with-constraints.md)
- [Generic async composition roots with constraints](readme/generic-async-composition-roots-with-constraints.md)
### Attributes
- [Constructor ordinal attribute](readme/constructor-ordinal-attribute.md)
- [Member ordinal attribute](readme/member-ordinal-attribute.md)
Expand Down
4 changes: 2 additions & 2 deletions readme/async-disposable-scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ partial class Composition: IDisposable, IAsyncDisposable
() =>
{
Composition transientComposition2 = this;
var localValue29 = new Session(transientComposition2);
return localValue29;
var localValue51 = new Session(transientComposition2);
return localValue51;
});
}
}
Expand Down
138 changes: 138 additions & 0 deletions readme/async-root.md
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
```

10 changes: 5 additions & 5 deletions readme/auto-scoped.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ partial class Composition
Composition transientComposition2 = this;
IService transientIService1;
{
var localBaseComposition31 = transientComposition2;
var localBaseComposition53 = transientComposition2;
// Creates a session
var localSession32 = new Composition(localBaseComposition31);
transientIService1 = localSession32.SessionRoot;
var localSession54 = new Composition(localBaseComposition53);
transientIService1 = localSession54.SessionRoot;
}

var localValue30 = transientIService1;
return localValue30;
var localValue52 = transientIService1;
return localValue52;
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions readme/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ partial class Composition
DateTimeOffset transientDateTimeOffset3 = DateTimeOffset.Now;
Dependency transientDependency1;
{
var localDependency10 = new Dependency(transientDateTimeOffset3);
localDependency10.Initialize();
transientDependency1 = localDependency10;
var localDependency17 = new Dependency(transientDateTimeOffset3);
localDependency17.Initialize();
transientDependency1 = localDependency17;
}

return new Service(transientDependency1);
Expand Down
4 changes: 2 additions & 2 deletions readme/func-with-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ partial class Composition
}
}

var localDependency12 = new Dependency(_root._singletonClock36!, transientInt323);
return localDependency12;
var localDependency19 = new Dependency(_root._singletonClock36!, transientInt323);
return localDependency19;
};
return new Service(transientFunc1);
}
Expand Down
4 changes: 2 additions & 2 deletions readme/func-with-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
() =>
{
var localValue13 = new Dependency();
return localValue13;
var localValue20 = new Dependency();
return localValue20;
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions readme/func.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
() =>
{
var localValue11 = new Dependency();
return localValue11;
var localValue18 = new Dependency();
return localValue18;
});
}
}
Expand Down
Loading

0 comments on commit f819c15

Please sign in to comment.