Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 28, 2024
1 parent f6e3364 commit 87dc39e
Show file tree
Hide file tree
Showing 39 changed files with 294 additions and 278 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ DI.Setup(nameof(Composition))
> [!NOTE]
> In fact, the `Bind().As(Singleton).To<Random>()` binding is unnecessary since _Pure.DI_ supports many .NET BCL types out of the box, including [Random](https://github.com/DevTeam/Pure.DI/blob/27a1ccd604b2fdd55f6bfec01c24c86428ddfdcb/src/Pure.DI.Core/Features/Default.g.cs#L289). It was added just for the example of using the _Singleton_ lifetime.
The Pure.DI source code generator works like this:

```mermaid
flowchart TD
analyzing[Analyzing DI setups] --> types["`Types analysis
constructors/methods/properties/fields`"] --> deps[Creating a dependency graph] --> verification[Dependency graph verification] --> codeGeneration[Code generation]
```

The above code specifies the generation of a partial class named *__Composition__*, this name is defined in the `DI.Setup(nameof(Composition))` call. This class contains a *__Root__* property that returns a graph of objects with an object of type *__Program__* as the root. The type and name of the property is defined by calling `Root<Program>("Root")`. The code of the generated class looks as follows:

```c#
Expand Down
8 changes: 8 additions & 0 deletions readme/ReadmeTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ DI.Setup(nameof(Composition))
> [!NOTE]
> In fact, the `Bind().As(Singleton).To<Random>()` binding is unnecessary since _Pure.DI_ supports many .NET BCL types out of the box, including [Random](https://github.com/DevTeam/Pure.DI/blob/27a1ccd604b2fdd55f6bfec01c24c86428ddfdcb/src/Pure.DI.Core/Features/Default.g.cs#L289). It was added just for the example of using the _Singleton_ lifetime.
The Pure.DI source code generator works like this:

```mermaid
flowchart TD
analyzing[Analyzing DI setups] --> types["`Types analysis
constructors/methods/properties/fields`"] --> deps[Creating a dependency graph] --> verification[Dependency graph verification] --> codeGeneration[Code generation]
```

The above code specifies the generation of a partial class named *__Composition__*, this name is defined in the `DI.Setup(nameof(Composition))` call. This class contains a *__Root__* property that returns a graph of objects with an object of type *__Program__* as the root. The type and name of the property is defined by calling `Root<Program>("Root")`. The code of the generated class looks as follows:

```c#
Expand Down
18 changes: 9 additions & 9 deletions readme/SingletonDetails.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ partial class Singleton
{
private readonly Singleton _root;

private Service1? _scopedService139;
private Service4? _scopedService442;
private Service1? _scopedService141;
private Service4? _scopedService444;

[OrdinalAttribute(20)]
public Singleton()
Expand All @@ -77,22 +77,22 @@ partial class Singleton
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public partial CompositionRoot TestPureDIByCR()
{
if (_scopedService442 is null)
if (_scopedService444 is null)
{
_scopedService442 = new Service4();
_scopedService444 = new Service4();
}

if (_scopedService139 is null)
if (_scopedService141 is null)
{
if (_scopedService442 is null)
if (_scopedService444 is null)
{
_scopedService442 = new Service4();
_scopedService444 = new Service4();
}

_scopedService139 = new Service1(new Service2(new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!)));
_scopedService141 = new Service1(new Service2(new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!)));
}

return new CompositionRoot(_scopedService139!, new Service2(new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!)), new Service2(new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!)), new Service2(new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!), new Service3(_scopedService442!, _scopedService442!)), new Service3(_scopedService442!, _scopedService442!), _scopedService442!, _scopedService442!);
return new CompositionRoot(_scopedService141!, new Service2(new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!)), new Service2(new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!)), new Service2(new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!), new Service3(_scopedService444!, _scopedService444!)), new Service3(_scopedService444!, _scopedService444!), _scopedService444!, _scopedService444!);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
24 changes: 12 additions & 12 deletions readme/accumulators.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ partial class Composition
private readonly Composition _root;
private readonly Lock _lock;

private XyzDependency? _singletonXyzDependency43;
private XyzDependency? _singletonXyzDependency45;

[OrdinalAttribute(20)]
public Composition()
Expand All @@ -69,34 +69,34 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
var accumulator46 = new MyAccumulator();
var accumulator48 = new MyAccumulator();
AbcDependency perBlockAbcDependency4 = new AbcDependency();
if (_root._singletonXyzDependency43 is null)
if (_root._singletonXyzDependency45 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonXyzDependency43 is null)
if (_root._singletonXyzDependency45 is null)
{
XyzDependency _singletonXyzDependency43Temp;
_singletonXyzDependency43Temp = new XyzDependency();
accumulator46.Add(_singletonXyzDependency43Temp);
XyzDependency _singletonXyzDependency45Temp;
_singletonXyzDependency45Temp = new XyzDependency();
accumulator48.Add(_singletonXyzDependency45Temp);
Thread.MemoryBarrier();
_root._singletonXyzDependency43 = _singletonXyzDependency43Temp;
_root._singletonXyzDependency45 = _singletonXyzDependency45Temp;
}
}
}

AbcDependency transientAbcDependency3 = new AbcDependency();
using (_lock.EnterScope())
{
accumulator46.Add(transientAbcDependency3);
accumulator48.Add(transientAbcDependency3);
}
Service transientService1 = new Service(transientAbcDependency3, _root._singletonXyzDependency43!, perBlockAbcDependency4);
Service transientService1 = new Service(transientAbcDependency3, _root._singletonXyzDependency45!, perBlockAbcDependency4);
using (_lock.EnterScope())
{
accumulator46.Add(transientService1);
accumulator48.Add(transientService1);
}
return (transientService1, accumulator46);
return (transientService1, accumulator48);
}
}

Expand Down
16 changes: 8 additions & 8 deletions readme/async-disposable-scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ partial class Composition: IDisposable, IAsyncDisposable
private object[] _disposables;
private int _disposeIndex;

private Dependency? _scopedDependency41;
private Dependency? _scopedDependency43;

[OrdinalAttribute(20)]
public Composition()
Expand All @@ -115,19 +115,19 @@ partial class Composition: IDisposable, IAsyncDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_scopedDependency41 is null)
if (_scopedDependency43 is null)
{
using (_lock.EnterScope())
{
if (_scopedDependency41 is null)
if (_scopedDependency43 is null)
{
_scopedDependency41 = new Dependency();
_disposables[_disposeIndex++] = _scopedDependency41;
_scopedDependency43 = new Dependency();
_disposables[_disposeIndex++] = _scopedDependency43;
}
}
}

return new Service(_scopedDependency41!);
return new Service(_scopedDependency43!);
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ partial class Composition: IDisposable, IAsyncDisposable
_disposeIndex = 0;
disposables = _disposables;
_disposables = new object[1];
_scopedDependency41 = null;
_scopedDependency43 = null;
}

while (disposeIndex-- > 0)
Expand Down Expand Up @@ -194,7 +194,7 @@ partial class Composition: IDisposable, IAsyncDisposable
_disposeIndex = 0;
disposables = _disposables;
_disposables = new object[1];
_scopedDependency41 = null;
_scopedDependency43 = null;
}
finally
{
Expand Down
16 changes: 8 additions & 8 deletions readme/async-disposable-singleton.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ partial class Composition: IDisposable, IAsyncDisposable
private object[] _disposables;
private int _disposeIndex;

private Dependency? _singletonDependency41;
private Dependency? _singletonDependency43;

[OrdinalAttribute(20)]
public Composition()
Expand All @@ -81,19 +81,19 @@ partial class Composition: IDisposable, IAsyncDisposable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_root._singletonDependency41 is null)
if (_root._singletonDependency43 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonDependency41 is null)
if (_root._singletonDependency43 is null)
{
_root._singletonDependency41 = new Dependency();
_root._disposables[_root._disposeIndex++] = _root._singletonDependency41;
_root._singletonDependency43 = new Dependency();
_root._disposables[_root._disposeIndex++] = _root._singletonDependency43;
}
}
}

return new Service(_root._singletonDependency41!);
return new Service(_root._singletonDependency43!);
}
}

Expand All @@ -107,7 +107,7 @@ partial class Composition: IDisposable, IAsyncDisposable
_disposeIndex = 0;
disposables = _disposables;
_disposables = new object[1];
_singletonDependency41 = null;
_singletonDependency43 = null;
}

while (disposeIndex-- > 0)
Expand Down Expand Up @@ -145,7 +145,7 @@ partial class Composition: IDisposable, IAsyncDisposable
_disposeIndex = 0;
disposables = _disposables;
_disposables = new object[1];
_singletonDependency41 = null;
_singletonDependency43 = null;
}
finally
{
Expand Down
10 changes: 5 additions & 5 deletions readme/auto-scoped.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ partial class Composition
private readonly Composition _root;
private readonly Lock _lock;

private Dependency? _scopedDependency41;
private Dependency? _scopedDependency43;

[OrdinalAttribute(20)]
public Composition()
Expand Down Expand Up @@ -116,18 +116,18 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_scopedDependency41 is null)
if (_scopedDependency43 is null)
{
using (_lock.EnterScope())
{
if (_scopedDependency41 is null)
if (_scopedDependency43 is null)
{
_scopedDependency41 = new Dependency();
_scopedDependency43 = new Dependency();
}
}
}

return new Service(_scopedDependency41!);
return new Service(_scopedDependency43!);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions readme/bind-attribute-for-a-generic-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ partial class Composition
private readonly Composition _root;
private readonly Lock _lock;

private Facade? _singletonFacade41;
private Facade? _singletonFacade43;

[OrdinalAttribute(20)]
public Composition()
Expand All @@ -72,19 +72,19 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_root._singletonFacade41 is null)
if (_root._singletonFacade43 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonFacade41 is null)
if (_root._singletonFacade43 is null)
{
_root._singletonFacade41 = new Facade();
_root._singletonFacade43 = new Facade();
}
}
}

IDependency<int> transientIDependency1;
Facade localInstance_1182D12736 = _root._singletonFacade41!;
Facade localInstance_1182D12736 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12736.GetDependency<int>();
return new Service(transientIDependency1);
}
Expand Down
8 changes: 4 additions & 4 deletions readme/bind-attribute-with-lifetime-and-tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ partial class Composition
private readonly Lock _lock;

private IDependency? _singletonIDependency0;
private Facade? _singletonFacade41;
private Facade? _singletonFacade43;

[OrdinalAttribute(20)]
public Composition()
Expand All @@ -79,12 +79,12 @@ partial class Composition
{
if (_root._singletonIDependency0 is null)
{
if (_root._singletonFacade41 is null)
if (_root._singletonFacade43 is null)
{
_root._singletonFacade41 = new Facade();
_root._singletonFacade43 = new Facade();
}

Facade localInstance_1182D12738 = _root._singletonFacade41!;
Facade localInstance_1182D12738 = _root._singletonFacade43!;
_root._singletonIDependency0 = localInstance_1182D12738.Dependency;
}
}
Expand Down
10 changes: 5 additions & 5 deletions readme/bind-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ partial class Composition
private readonly Composition _root;
private readonly Lock _lock;

private Facade? _singletonFacade41;
private Facade? _singletonFacade43;

[OrdinalAttribute(20)]
public Composition()
Expand All @@ -76,19 +76,19 @@ partial class Composition
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_root._singletonFacade41 is null)
if (_root._singletonFacade43 is null)
{
using (_lock.EnterScope())
{
if (_root._singletonFacade41 is null)
if (_root._singletonFacade43 is null)
{
_root._singletonFacade41 = new Facade();
_root._singletonFacade43 = new Facade();
}
}
}

IDependency transientIDependency1;
Facade localInstance_1182D12737 = _root._singletonFacade41!;
Facade localInstance_1182D12737 = _root._singletonFacade43!;
transientIDependency1 = localInstance_1182D12737.Dependency;
return new Service(transientIDependency1);
}
Expand Down
14 changes: 7 additions & 7 deletions readme/complex-generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ partial class Composition
private readonly Composition _root;
private readonly Lock _lock;

private DependencyStruct<int> _singletonDependencyStruct49;
private bool _singletonDependencyStruct49Created;
private DependencyStruct<int> _singletonDependencyStruct51;
private bool _singletonDependencyStruct51Created;

[OrdinalAttribute(10)]
public Composition()
Expand All @@ -94,20 +94,20 @@ partial class Composition
public Program<T1> GetRoot<T1>(T1 depArg)
where T1: notnull
{
if (!_root._singletonDependencyStruct49Created)
if (!_root._singletonDependencyStruct51Created)
{
using (_lock.EnterScope())
{
if (!_root._singletonDependencyStruct49Created)
if (!_root._singletonDependencyStruct51Created)
{
_root._singletonDependencyStruct49 = new DependencyStruct<int>();
_root._singletonDependencyStruct51 = new DependencyStruct<int>();
Thread.MemoryBarrier();
_root._singletonDependencyStruct49Created = true;
_root._singletonDependencyStruct51Created = true;
}
}
}

return new Program<T1>(new Service<T1, int, List<T1>, Dictionary<T1, int>>(new Dependency<T1>(depArg), _root._singletonDependencyStruct49));
return new Program<T1>(new Service<T1, int, List<T1>, Dictionary<T1, int>>(new Dependency<T1>(depArg), _root._singletonDependencyStruct51));
}
}
```
Expand Down
Loading

0 comments on commit 87dc39e

Please sign in to comment.