From 7aacfca214fce92c92ca51b6ce73d006c8341f4d Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Thu, 21 Nov 2024 14:25:15 +0300 Subject: [PATCH] Adds the Own/IOwn API class to Pure.DI.Abstractions --- README.md | 1 + readme/accumulators.md | 24 +- readme/async-disposable-scope.md | 20 +- readme/async-disposable-singleton.md | 16 +- readme/async-root.md | 20 +- readme/auto-scoped.md | 20 +- readme/bind-attribute-for-a-generic-type.md | 12 +- .../bind-attribute-with-lifetime-and-tag.md | 10 +- readme/bind-attribute.md | 12 +- readme/build-up-of-an-existing-object.md | 8 +- .../default-lifetime-for-a-type-and-a-tag.md | 18 +- readme/default-lifetime-for-a-type.md | 10 +- readme/default-lifetime.md | 16 +- readme/disposable-singleton.md | 14 +- readme/exposed-generic-roots-with-args.md | 10 +- readme/exposed-generic-roots.md | 10 +- readme/exposed-roots-with-tags.md | 10 +- readme/exposed-roots.md | 10 +- readme/factory.md | 10 +- readme/func-with-arguments.md | 12 +- readme/func-with-tag.md | 4 +- readme/func.md | 4 +- ...sync-composition-roots-with-constraints.md | 44 +-- ...eric-composition-roots-with-constraints.md | 4 +- readme/generic-composition-roots.md | 4 +- readme/keyed-service-provider.md | 18 +- readme/lazy.md | 8 +- readme/manually-started-tasks.md | 10 +- readme/onnewinstance-hint.md | 16 +- readme/perblock.md | 14 +- readme/perresolve.md | 26 +- readme/root-binding.md | 10 +- readme/scope.md | 18 +- readme/service-collection.md | 18 +- readme/service-provider-with-scope.md | 30 +- readme/service-provider.md | 18 +- readme/simplified-factory.md | 8 +- readme/singleton.md | 10 +- readme/tag-type.md | 18 +- readme/tags.md | 18 +- readme/task.md | 20 +- readme/threadsafe-hint.md | 12 +- ...async-disposable-instances-in-delegates.md | 12 +- ...osable-instances-per-a-composition-root.md | 8 +- ...cking-disposable-instances-in-delegates.md | 12 +- ...osable-instances-per-a-composition-root.md | 8 +- ...sable-instances-using-pre-built-classes.md | 371 ++++++++++++++++++ ...able-instances-with-different-lifetimes.md | 46 +-- readme/valuetask.md | 4 +- src/Pure.DI.Abstractions/IOwn.cs | 14 + src/Pure.DI.Abstractions/Own.cs | 122 ++++++ src/Pure.DI.Abstractions/Own`1.cs | 46 +++ .../any/Pure.DI/Abstractions/Composition.g.cs | 21 +- src/Pure.DI.Core/Components/Api.g.cs | 20 +- .../ConsoleAdapter.cs | 2 - ...ckingDisposableWithAbstractionsScenario.cs | 119 ++++++ 56 files changed, 1048 insertions(+), 352 deletions(-) create mode 100644 readme/tracking-disposable-instances-using-pre-built-classes.md create mode 100644 src/Pure.DI.Abstractions/IOwn.cs create mode 100644 src/Pure.DI.Abstractions/Own.cs create mode 100644 src/Pure.DI.Abstractions/Own`1.cs create mode 100644 tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithAbstractionsScenario.cs diff --git a/README.md b/README.md index 18a5d7fb8..5d53c0713 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,7 @@ dotnet run - [A few partial classes](readme/a-few-partial-classes.md) - [Tracking disposable instances per a composition root](readme/tracking-disposable-instances-per-a-composition-root.md) - [Tracking disposable instances in delegates](readme/tracking-disposable-instances-in-delegates.md) +- [Tracking disposable instances using pre-built classes](readme/tracking-disposable-instances-using-pre-built-classes.md) - [Tracking disposable instances with different lifetimes](readme/tracking-disposable-instances-with-different-lifetimes.md) - [Tracking async disposable instances per a composition root](readme/tracking-async-disposable-instances-per-a-composition-root.md) - [Tracking async disposable instances in delegates](readme/tracking-async-disposable-instances-in-delegates.md) diff --git a/readme/accumulators.md b/readme/accumulators.md index 084612d86..483220c19 100644 --- a/readme/accumulators.md +++ b/readme/accumulators.md @@ -49,7 +49,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private XyzDependency? _singletonXyzDependency41; + private XyzDependency? _singletonXyzDependency43; [OrdinalAttribute(20)] public Composition() @@ -69,19 +69,19 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - var accumulator44 = new MyAccumulator(); + var accumulator46 = new MyAccumulator(); AbcDependency perBlockAbcDependency4 = new AbcDependency(); - if (_root._singletonXyzDependency41 is null) + if (_root._singletonXyzDependency43 is null) { using (_lock.EnterScope()) { - if (_root._singletonXyzDependency41 is null) + if (_root._singletonXyzDependency43 is null) { - XyzDependency _singletonXyzDependency41Temp; - _singletonXyzDependency41Temp = new XyzDependency(); - accumulator44.Add(_singletonXyzDependency41Temp); + XyzDependency _singletonXyzDependency43Temp; + _singletonXyzDependency43Temp = new XyzDependency(); + accumulator46.Add(_singletonXyzDependency43Temp); Thread.MemoryBarrier(); - _root._singletonXyzDependency41 = _singletonXyzDependency41Temp; + _root._singletonXyzDependency43 = _singletonXyzDependency43Temp; } } } @@ -89,14 +89,14 @@ partial class Composition AbcDependency transientAbcDependency3 = new AbcDependency(); using (_lock.EnterScope()) { - accumulator44.Add(transientAbcDependency3); + accumulator46.Add(transientAbcDependency3); } - Service transientService1 = new Service(transientAbcDependency3, _root._singletonXyzDependency41!, perBlockAbcDependency4); + Service transientService1 = new Service(transientAbcDependency3, _root._singletonXyzDependency43!, perBlockAbcDependency4); using (_lock.EnterScope()) { - accumulator44.Add(transientService1); + accumulator46.Add(transientService1); } - return (transientService1, accumulator44); + return (transientService1, accumulator46); } } diff --git a/readme/async-disposable-scope.md b/readme/async-disposable-scope.md index 8f45ac109..113c26e50 100644 --- a/readme/async-disposable-scope.md +++ b/readme/async-disposable-scope.md @@ -93,7 +93,7 @@ partial class Composition: IDisposable, IAsyncDisposable private object[] _disposables; private int _disposeIndex; - private Dependency? _scopedDependency39; + private Dependency? _scopedDependency41; [OrdinalAttribute(20)] public Composition() @@ -115,19 +115,19 @@ partial class Composition: IDisposable, IAsyncDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_scopedDependency39 is null) + if (_scopedDependency41 is null) { using (_lock.EnterScope()) { - if (_scopedDependency39 is null) + if (_scopedDependency41 is null) { - _scopedDependency39 = new Dependency(); - _disposables[_disposeIndex++] = _scopedDependency39; + _scopedDependency41 = new Dependency(); + _disposables[_disposeIndex++] = _scopedDependency41; } } } - return new Service(_scopedDependency39!); + return new Service(_scopedDependency41!); } } @@ -139,8 +139,8 @@ partial class Composition: IDisposable, IAsyncDisposable Func perBlockFunc1 = new Func([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { Composition transientComposition3 = this; - Session localValue72 = new Session(transientComposition3); - return localValue72; + Session localValue78 = new Session(transientComposition3); + return localValue78; }); return new Program(perBlockFunc1); } @@ -156,7 +156,7 @@ partial class Composition: IDisposable, IAsyncDisposable _disposeIndex = 0; disposables = _disposables; _disposables = new object[1]; - _scopedDependency39 = null; + _scopedDependency41 = null; } while (disposeIndex-- > 0) @@ -194,7 +194,7 @@ partial class Composition: IDisposable, IAsyncDisposable _disposeIndex = 0; disposables = _disposables; _disposables = new object[1]; - _scopedDependency39 = null; + _scopedDependency41 = null; } finally { diff --git a/readme/async-disposable-singleton.md b/readme/async-disposable-singleton.md index b7c751bcc..9388bcc4e 100644 --- a/readme/async-disposable-singleton.md +++ b/readme/async-disposable-singleton.md @@ -59,7 +59,7 @@ partial class Composition: IDisposable, IAsyncDisposable private object[] _disposables; private int _disposeIndex; - private Dependency? _singletonDependency39; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -81,19 +81,19 @@ partial class Composition: IDisposable, IAsyncDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); - _root._disposables[_root._disposeIndex++] = _root._singletonDependency39; + _root._singletonDependency41 = new Dependency(); + _root._disposables[_root._disposeIndex++] = _root._singletonDependency41; } } } - return new Service(_root._singletonDependency39!); + return new Service(_root._singletonDependency41!); } } @@ -107,7 +107,7 @@ partial class Composition: IDisposable, IAsyncDisposable _disposeIndex = 0; disposables = _disposables; _disposables = new object[1]; - _singletonDependency39 = null; + _singletonDependency41 = null; } while (disposeIndex-- > 0) @@ -145,7 +145,7 @@ partial class Composition: IDisposable, IAsyncDisposable _disposeIndex = 0; disposables = _disposables; _disposables = new object[1]; - _singletonDependency39 = null; + _singletonDependency41 = null; } finally { diff --git a/readme/async-root.md b/readme/async-root.md index 772269250..38edc3c72 100644 --- a/readme/async-root.md +++ b/readme/async-root.md @@ -51,26 +51,26 @@ partial class Composition public Task GetMyServiceAsync(CancellationToken cancellationToken) { TaskFactory perBlockTaskFactory2; - CancellationToken localCancellationToken23 = cancellationToken; + CancellationToken localCancellationToken29 = cancellationToken; TaskCreationOptions transientTaskCreationOptions3 = TaskCreationOptions.None; - TaskCreationOptions localTaskCreationOptions24 = transientTaskCreationOptions3; + TaskCreationOptions localTaskCreationOptions30 = transientTaskCreationOptions3; TaskContinuationOptions transientTaskContinuationOptions4 = TaskContinuationOptions.None; - TaskContinuationOptions localTaskContinuationOptions25 = transientTaskContinuationOptions4; + TaskContinuationOptions localTaskContinuationOptions31 = transientTaskContinuationOptions4; TaskScheduler transientTaskScheduler5 = TaskScheduler.Default; - TaskScheduler localTaskScheduler26 = transientTaskScheduler5; - perBlockTaskFactory2 = new TaskFactory(localCancellationToken23, localTaskCreationOptions24, localTaskContinuationOptions25, localTaskScheduler26); + TaskScheduler localTaskScheduler32 = transientTaskScheduler5; + perBlockTaskFactory2 = new TaskFactory(localCancellationToken29, localTaskCreationOptions30, localTaskContinuationOptions31, localTaskScheduler32); Func perBlockFunc1 = new Func([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - IService localValue27 = new Service(new Dependency()); - return localValue27; + IService localValue33 = new Service(new Dependency()); + return localValue33; }); Task transientTask0; // Injects an instance factory - Func localFactory28 = perBlockFunc1; + Func localFactory34 = perBlockFunc1; // Injects a task factory creating and scheduling task objects - TaskFactory localTaskFactory29 = perBlockTaskFactory2; + TaskFactory localTaskFactory35 = perBlockTaskFactory2; // Creates and starts a task using the instance factory - transientTask0 = localTaskFactory29.StartNew(localFactory28); + transientTask0 = localTaskFactory35.StartNew(localFactory34); return transientTask0; } } diff --git a/readme/auto-scoped.md b/readme/auto-scoped.md index 415bf1b32..e5a3a70ef 100644 --- a/readme/auto-scoped.md +++ b/readme/auto-scoped.md @@ -75,7 +75,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Dependency? _scopedDependency39; + private Dependency? _scopedDependency41; [OrdinalAttribute(20)] public Composition() @@ -100,12 +100,12 @@ partial class Composition Composition transientComposition3 = this; IService transientIService2; // Injects a base composition - Composition localBaseComposition74 = transientComposition3; + Composition localBaseComposition80 = transientComposition3; // Creates a session - var localSession75= new Composition(localBaseComposition74); - transientIService2 = localSession75.SessionRoot; - IService localValue73 = transientIService2; - return localValue73; + var localSession81= new Composition(localBaseComposition80); + transientIService2 = localSession81.SessionRoot; + IService localValue79 = transientIService2; + return localValue79; }); return new Program(perBlockFunc1); } @@ -116,18 +116,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_scopedDependency39 is null) + if (_scopedDependency41 is null) { using (_lock.EnterScope()) { - if (_scopedDependency39 is null) + if (_scopedDependency41 is null) { - _scopedDependency39 = new Dependency(); + _scopedDependency41 = new Dependency(); } } } - return new Service(_scopedDependency39!); + return new Service(_scopedDependency41!); } } } diff --git a/readme/bind-attribute-for-a-generic-type.md b/readme/bind-attribute-for-a-generic-type.md index fbbeefadb..c9d2955c4 100644 --- a/readme/bind-attribute-for-a-generic-type.md +++ b/readme/bind-attribute-for-a-generic-type.md @@ -52,7 +52,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Facade? _singletonFacade39; + private Facade? _singletonFacade41; [OrdinalAttribute(20)] public Composition() @@ -72,20 +72,20 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonFacade39 is null) + if (_root._singletonFacade41 is null) { using (_lock.EnterScope()) { - if (_root._singletonFacade39 is null) + if (_root._singletonFacade41 is null) { - _root._singletonFacade39 = new Facade(); + _root._singletonFacade41 = new Facade(); } } } IDependency transientIDependency1; - Facade localInstance_1182D12730 = _root._singletonFacade39!; - transientIDependency1 = localInstance_1182D12730.GetDependency(); + Facade localInstance_1182D12736 = _root._singletonFacade41!; + transientIDependency1 = localInstance_1182D12736.GetDependency(); return new Service(transientIDependency1); } } diff --git a/readme/bind-attribute-with-lifetime-and-tag.md b/readme/bind-attribute-with-lifetime-and-tag.md index e4dab6b85..0964a7647 100644 --- a/readme/bind-attribute-with-lifetime-and-tag.md +++ b/readme/bind-attribute-with-lifetime-and-tag.md @@ -53,7 +53,7 @@ partial class Composition private readonly Lock _lock; private IDependency? _singletonIDependency0; - private Facade? _singletonFacade39; + private Facade? _singletonFacade41; [OrdinalAttribute(20)] public Composition() @@ -79,13 +79,13 @@ partial class Composition { if (_root._singletonIDependency0 is null) { - if (_root._singletonFacade39 is null) + if (_root._singletonFacade41 is null) { - _root._singletonFacade39 = new Facade(); + _root._singletonFacade41 = new Facade(); } - Facade localInstance_1182D12732 = _root._singletonFacade39!; - _root._singletonIDependency0 = localInstance_1182D12732.Dependency; + Facade localInstance_1182D12738 = _root._singletonFacade41!; + _root._singletonIDependency0 = localInstance_1182D12738.Dependency; } } } diff --git a/readme/bind-attribute.md b/readme/bind-attribute.md index 0ec3333bb..7887cbf12 100644 --- a/readme/bind-attribute.md +++ b/readme/bind-attribute.md @@ -56,7 +56,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Facade? _singletonFacade39; + private Facade? _singletonFacade41; [OrdinalAttribute(20)] public Composition() @@ -76,20 +76,20 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonFacade39 is null) + if (_root._singletonFacade41 is null) { using (_lock.EnterScope()) { - if (_root._singletonFacade39 is null) + if (_root._singletonFacade41 is null) { - _root._singletonFacade39 = new Facade(); + _root._singletonFacade41 = new Facade(); } } } IDependency transientIDependency1; - Facade localInstance_1182D12731 = _root._singletonFacade39!; - transientIDependency1 = localInstance_1182D12731.Dependency; + Facade localInstance_1182D12737 = _root._singletonFacade41!; + transientIDependency1 = localInstance_1182D12737.Dependency; return new Service(transientIDependency1); } } diff --git a/readme/build-up-of-an-existing-object.md b/readme/build-up-of-an-existing-object.md index c33f13c7c..cf0c39a63 100644 --- a/readme/build-up-of-an-existing-object.md +++ b/readme/build-up-of-an-existing-object.md @@ -76,10 +76,10 @@ partial class Composition { Guid transientGuid2 = Guid.NewGuid(); Dependency transientDependency1; - var localDependency33= new Dependency(); - localDependency33.SetId(transientGuid2); - localDependency33.Name = name; - transientDependency1 = localDependency33; + var localDependency39= new Dependency(); + localDependency39.SetId(transientGuid2); + localDependency39.Name = name; + transientDependency1 = localDependency39; return new Service(transientDependency1); } } diff --git a/readme/default-lifetime-for-a-type-and-a-tag.md b/readme/default-lifetime-for-a-type-and-a-tag.md index db3c0f25e..3e50c1589 100644 --- a/readme/default-lifetime-for-a-type-and-a-tag.md +++ b/readme/default-lifetime-for-a-type-and-a-tag.md @@ -53,8 +53,8 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Dependency? _singletonDependency39; - private Dependency? _singletonDependency40; + private Dependency? _singletonDependency41; + private Dependency? _singletonDependency42; [OrdinalAttribute(20)] public Composition() @@ -74,29 +74,29 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency40 is null) + if (_root._singletonDependency42 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency40 is null) + if (_root._singletonDependency42 is null) { - _root._singletonDependency40 = new Dependency(); + _root._singletonDependency42 = new Dependency(); } } } - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return new Service(_root._singletonDependency39!, _root._singletonDependency40!); + return new Service(_root._singletonDependency41!, _root._singletonDependency42!); } } } diff --git a/readme/default-lifetime-for-a-type.md b/readme/default-lifetime-for-a-type.md index 311592ab7..f70d0055f 100644 --- a/readme/default-lifetime-for-a-type.md +++ b/readme/default-lifetime-for-a-type.md @@ -52,7 +52,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Dependency? _singletonDependency39; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -72,18 +72,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return new Service(_root._singletonDependency39!, _root._singletonDependency39!); + return new Service(_root._singletonDependency41!, _root._singletonDependency41!); } } } diff --git a/readme/default-lifetime.md b/readme/default-lifetime.md index 2994eb7f4..73dc33e76 100644 --- a/readme/default-lifetime.md +++ b/readme/default-lifetime.md @@ -54,8 +54,8 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Service? _singletonService40; - private Dependency? _singletonDependency39; + private Service? _singletonService42; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -75,23 +75,23 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonService40 is null) + if (_root._singletonService42 is null) { using (_lock.EnterScope()) { - if (_root._singletonService40 is null) + if (_root._singletonService42 is null) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } - _root._singletonService40 = new Service(_root._singletonDependency39!, _root._singletonDependency39!); + _root._singletonService42 = new Service(_root._singletonDependency41!, _root._singletonDependency41!); } } } - return _root._singletonService40!; + return _root._singletonService42!; } } } diff --git a/readme/disposable-singleton.md b/readme/disposable-singleton.md index 805ceb083..dfa9f5f7e 100644 --- a/readme/disposable-singleton.md +++ b/readme/disposable-singleton.md @@ -57,7 +57,7 @@ partial class Composition: IDisposable private object[] _disposables; private int _disposeIndex; - private Dependency? _singletonDependency39; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -79,19 +79,19 @@ partial class Composition: IDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); - _root._disposables[_root._disposeIndex++] = _root._singletonDependency39; + _root._singletonDependency41 = new Dependency(); + _root._disposables[_root._disposeIndex++] = _root._singletonDependency41; } } } - return new Service(_root._singletonDependency39!); + return new Service(_root._singletonDependency41!); } } @@ -105,7 +105,7 @@ partial class Composition: IDisposable _disposeIndex = 0; disposables = _disposables; _disposables = new object[1]; - _singletonDependency39 = null; + _singletonDependency41 = null; } while (disposeIndex-- > 0) diff --git a/readme/exposed-generic-roots-with-args.md b/readme/exposed-generic-roots-with-args.md index 7161e4237..ca3bba748 100644 --- a/readme/exposed-generic-roots-with-args.md +++ b/readme/exposed-generic-roots-with-args.md @@ -43,7 +43,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Integration.CompositionWithGenericRootsAndArgsInOtherProject? _singletonCompositionWithGenericRootsAndArgsInOtherProject40; + private Integration.CompositionWithGenericRootsAndArgsInOtherProject? _singletonCompositionWithGenericRootsAndArgsInOtherProject42; [OrdinalAttribute(10)] public Composition() @@ -61,20 +61,20 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] public Program GetProgram(int id) { - if (_root._singletonCompositionWithGenericRootsAndArgsInOtherProject40 is null) + if (_root._singletonCompositionWithGenericRootsAndArgsInOtherProject42 is null) { using (_lock.EnterScope()) { - if (_root._singletonCompositionWithGenericRootsAndArgsInOtherProject40 is null) + if (_root._singletonCompositionWithGenericRootsAndArgsInOtherProject42 is null) { - _root._singletonCompositionWithGenericRootsAndArgsInOtherProject40 = new Integration.CompositionWithGenericRootsAndArgsInOtherProject(); + _root._singletonCompositionWithGenericRootsAndArgsInOtherProject42 = new Integration.CompositionWithGenericRootsAndArgsInOtherProject(); } } } Integration.IMyGenericService transientIMyGenericService1; int localId1 = id; - Integration.CompositionWithGenericRootsAndArgsInOtherProject localInstance_1182D1272 = _root._singletonCompositionWithGenericRootsAndArgsInOtherProject40!; + Integration.CompositionWithGenericRootsAndArgsInOtherProject localInstance_1182D1272 = _root._singletonCompositionWithGenericRootsAndArgsInOtherProject42!; transientIMyGenericService1 = localInstance_1182D1272.GetMyService(localId1); return new Program(transientIMyGenericService1); } diff --git a/readme/exposed-generic-roots.md b/readme/exposed-generic-roots.md index 44af691cc..d7f5cb026 100644 --- a/readme/exposed-generic-roots.md +++ b/readme/exposed-generic-roots.md @@ -44,7 +44,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Integration.CompositionWithGenericRootsInOtherProject? _singletonCompositionWithGenericRootsInOtherProject39; + private Integration.CompositionWithGenericRootsInOtherProject? _singletonCompositionWithGenericRootsInOtherProject41; [OrdinalAttribute(20)] public Composition() @@ -64,19 +64,19 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonCompositionWithGenericRootsInOtherProject39 is null) + if (_root._singletonCompositionWithGenericRootsInOtherProject41 is null) { using (_lock.EnterScope()) { - if (_root._singletonCompositionWithGenericRootsInOtherProject39 is null) + if (_root._singletonCompositionWithGenericRootsInOtherProject41 is null) { - _root._singletonCompositionWithGenericRootsInOtherProject39 = new Integration.CompositionWithGenericRootsInOtherProject(); + _root._singletonCompositionWithGenericRootsInOtherProject41 = new Integration.CompositionWithGenericRootsInOtherProject(); } } } Integration.IMyGenericService transientIMyGenericService1; - Integration.CompositionWithGenericRootsInOtherProject localInstance_1182D1270 = _root._singletonCompositionWithGenericRootsInOtherProject39!; + Integration.CompositionWithGenericRootsInOtherProject localInstance_1182D1270 = _root._singletonCompositionWithGenericRootsInOtherProject41!; transientIMyGenericService1 = localInstance_1182D1270.GetMyService(); return new Program(transientIMyGenericService1); } diff --git a/readme/exposed-roots-with-tags.md b/readme/exposed-roots-with-tags.md index f183b1f44..30bea69a3 100644 --- a/readme/exposed-roots-with-tags.md +++ b/readme/exposed-roots-with-tags.md @@ -39,7 +39,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Integration.CompositionWithTagsInOtherProject? _singletonCompositionWithTagsInOtherProject39; + private Integration.CompositionWithTagsInOtherProject? _singletonCompositionWithTagsInOtherProject41; [OrdinalAttribute(20)] public Composition() @@ -59,19 +59,19 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonCompositionWithTagsInOtherProject39 is null) + if (_root._singletonCompositionWithTagsInOtherProject41 is null) { using (_lock.EnterScope()) { - if (_root._singletonCompositionWithTagsInOtherProject39 is null) + if (_root._singletonCompositionWithTagsInOtherProject41 is null) { - _root._singletonCompositionWithTagsInOtherProject39 = new Integration.CompositionWithTagsInOtherProject(); + _root._singletonCompositionWithTagsInOtherProject41 = new Integration.CompositionWithTagsInOtherProject(); } } } Integration.IMyService transientIMyService1; - Integration.CompositionWithTagsInOtherProject localInstance_1182D1276 = _root._singletonCompositionWithTagsInOtherProject39!; + Integration.CompositionWithTagsInOtherProject localInstance_1182D1276 = _root._singletonCompositionWithTagsInOtherProject41!; transientIMyService1 = localInstance_1182D1276.MyService; return new Program(transientIMyService1); } diff --git a/readme/exposed-roots.md b/readme/exposed-roots.md index 4716edbe1..6871591f3 100644 --- a/readme/exposed-roots.md +++ b/readme/exposed-roots.md @@ -42,7 +42,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Integration.CompositionInOtherProject? _singletonCompositionInOtherProject39; + private Integration.CompositionInOtherProject? _singletonCompositionInOtherProject41; [OrdinalAttribute(20)] public Composition() @@ -62,19 +62,19 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonCompositionInOtherProject39 is null) + if (_root._singletonCompositionInOtherProject41 is null) { using (_lock.EnterScope()) { - if (_root._singletonCompositionInOtherProject39 is null) + if (_root._singletonCompositionInOtherProject41 is null) { - _root._singletonCompositionInOtherProject39 = new Integration.CompositionInOtherProject(); + _root._singletonCompositionInOtherProject41 = new Integration.CompositionInOtherProject(); } } } Integration.IMyService transientIMyService1; - Integration.CompositionInOtherProject localInstance_1182D1273 = _root._singletonCompositionInOtherProject39!; + Integration.CompositionInOtherProject localInstance_1182D1273 = _root._singletonCompositionInOtherProject41!; transientIMyService1 = localInstance_1182D1273.MyService; return new Program(transientIMyService1); } diff --git a/readme/factory.md b/readme/factory.md index 4cd239e7b..ffebfceff 100644 --- a/readme/factory.md +++ b/readme/factory.md @@ -110,15 +110,15 @@ partial class Composition // Some custom logic for creating an instance. // For example, here's how you can inject and initialize // an instance of a particular type: - bool localIsFake34 = isFake; - if (localIsFake34) + bool localIsFake40 = isFake; + if (localIsFake40) { {transientIDependency1 = new FakeDependency(); goto transientIDependency1Finish; } } - Dependency localDependency35 = new Dependency(transientDateTimeOffset3); - localDependency35.Initialize(); - transientIDependency1 = localDependency35; + Dependency localDependency41 = new Dependency(transientDateTimeOffset3); + localDependency41.Initialize(); + transientIDependency1 = localDependency41; transientIDependency1Finish:; return new Service(transientIDependency1); } diff --git a/readme/func-with-arguments.md b/readme/func-with-arguments.md index b98ed37b2..2cf8f155a 100644 --- a/readme/func-with-arguments.md +++ b/readme/func-with-arguments.md @@ -90,7 +90,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Clock? _singletonClock39; + private Clock? _singletonClock41; [OrdinalAttribute(20)] public Composition() @@ -119,19 +119,19 @@ partial class Composition // and source code statements "subId" int transientInt324 = subId; int transientInt323 = dependencyId; - if (_root._singletonClock39 is null) + if (_root._singletonClock41 is null) { using (_lock.EnterScope()) { - if (_root._singletonClock39 is null) + if (_root._singletonClock41 is null) { - _root._singletonClock39 = new Clock(); + _root._singletonClock41 = new Clock(); } } } - Dependency localDependency39 = new Dependency(_root._singletonClock39!, transientInt323, transientInt324); - return localDependency39; + Dependency localDependency45 = new Dependency(_root._singletonClock41!, transientInt323, transientInt324); + return localDependency45; }; return new Service(transientFunc1); } diff --git a/readme/func-with-tag.md b/readme/func-with-tag.md index 972a272ce..a7658889e 100644 --- a/readme/func-with-tag.md +++ b/readme/func-with-tag.md @@ -61,8 +61,8 @@ partial class Composition { Func perBlockFunc1 = new Func([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - IDependency localValue40 = new Dependency(); - return localValue40; + IDependency localValue46 = new Dependency(); + return localValue46; }); return new Service(perBlockFunc1); } diff --git a/readme/func.md b/readme/func.md index c7d607eb9..1b1c25694 100644 --- a/readme/func.md +++ b/readme/func.md @@ -64,8 +64,8 @@ partial class Composition { Func perBlockFunc1 = new Func([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - IDependency localValue38 = new Dependency(); - return localValue38; + IDependency localValue44 = new Dependency(); + return localValue44; }); return new Service(perBlockFunc1); } diff --git a/readme/generic-async-composition-roots-with-constraints.md b/readme/generic-async-composition-roots-with-constraints.md index e3344c17f..9749dfc75 100644 --- a/readme/generic-async-composition-roots-with-constraints.md +++ b/readme/generic-async-composition-roots-with-constraints.md @@ -85,29 +85,29 @@ partial class Composition where T: IDisposable { TaskFactory> perBlockTaskFactory2; - CancellationToken localCancellationToken54 = cancellationToken; + CancellationToken localCancellationToken60 = cancellationToken; TaskCreationOptions transientTaskCreationOptions3 = TaskCreationOptions.None; - TaskCreationOptions localTaskCreationOptions55 = transientTaskCreationOptions3; + TaskCreationOptions localTaskCreationOptions61 = transientTaskCreationOptions3; TaskContinuationOptions transientTaskContinuationOptions4 = TaskContinuationOptions.None; - TaskContinuationOptions localTaskContinuationOptions56 = transientTaskContinuationOptions4; + TaskContinuationOptions localTaskContinuationOptions62 = transientTaskContinuationOptions4; TaskScheduler transientTaskScheduler5 = TaskScheduler.Default; - TaskScheduler localTaskScheduler57 = transientTaskScheduler5; - perBlockTaskFactory2 = new TaskFactory>(localCancellationToken54, localTaskCreationOptions55, localTaskContinuationOptions56, localTaskScheduler57); + TaskScheduler localTaskScheduler63 = transientTaskScheduler5; + perBlockTaskFactory2 = new TaskFactory>(localCancellationToken60, localTaskCreationOptions61, localTaskContinuationOptions62, localTaskScheduler63); Func> perBlockFunc1 = new Func>([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { OtherService transientOtherService6; - IDependency localDependency59 = new Dependency(); - transientOtherService6 = new OtherService(localDependency59); - IService localValue58 = transientOtherService6; - return localValue58; + IDependency localDependency65 = new Dependency(); + transientOtherService6 = new OtherService(localDependency65); + IService localValue64 = transientOtherService6; + return localValue64; }); Task> transientTask0; // Injects an instance factory - Func> localFactory60 = perBlockFunc1; + Func> localFactory66 = perBlockFunc1; // Injects a task factory creating and scheduling task objects - TaskFactory> localTaskFactory61 = perBlockTaskFactory2; + TaskFactory> localTaskFactory67 = perBlockTaskFactory2; // Creates and starts a task using the instance factory - transientTask0 = localTaskFactory61.StartNew(localFactory60); + transientTask0 = localTaskFactory67.StartNew(localFactory66); return transientTask0; } @@ -117,26 +117,26 @@ partial class Composition where T1: struct { TaskFactory> perBlockTaskFactory2; - CancellationToken localCancellationToken62 = cancellationToken; + CancellationToken localCancellationToken68 = cancellationToken; TaskCreationOptions transientTaskCreationOptions3 = TaskCreationOptions.None; - TaskCreationOptions localTaskCreationOptions63 = transientTaskCreationOptions3; + TaskCreationOptions localTaskCreationOptions69 = transientTaskCreationOptions3; TaskContinuationOptions transientTaskContinuationOptions4 = TaskContinuationOptions.None; - TaskContinuationOptions localTaskContinuationOptions64 = transientTaskContinuationOptions4; + TaskContinuationOptions localTaskContinuationOptions70 = transientTaskContinuationOptions4; TaskScheduler transientTaskScheduler5 = TaskScheduler.Default; - TaskScheduler localTaskScheduler65 = transientTaskScheduler5; - perBlockTaskFactory2 = new TaskFactory>(localCancellationToken62, localTaskCreationOptions63, localTaskContinuationOptions64, localTaskScheduler65); + TaskScheduler localTaskScheduler71 = transientTaskScheduler5; + perBlockTaskFactory2 = new TaskFactory>(localCancellationToken68, localTaskCreationOptions69, localTaskContinuationOptions70, localTaskScheduler71); Func> perBlockFunc1 = new Func>([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - IService localValue66 = new Service(new Dependency()); - return localValue66; + IService localValue72 = new Service(new Dependency()); + return localValue72; }); Task> transientTask0; // Injects an instance factory - Func> localFactory67 = perBlockFunc1; + Func> localFactory73 = perBlockFunc1; // Injects a task factory creating and scheduling task objects - TaskFactory> localTaskFactory68 = perBlockTaskFactory2; + TaskFactory> localTaskFactory74 = perBlockTaskFactory2; // Creates and starts a task using the instance factory - transientTask0 = localTaskFactory68.StartNew(localFactory67); + transientTask0 = localTaskFactory74.StartNew(localFactory73); return transientTask0; } } diff --git a/readme/generic-composition-roots-with-constraints.md b/readme/generic-composition-roots-with-constraints.md index 94ed3eba9..3fec48a52 100644 --- a/readme/generic-composition-roots-with-constraints.md +++ b/readme/generic-composition-roots-with-constraints.md @@ -83,8 +83,8 @@ partial class Composition where T: IDisposable { OtherService transientOtherService0; - IDependency localDependency69 = new Dependency(); - transientOtherService0 = new OtherService(localDependency69); + IDependency localDependency75 = new Dependency(); + transientOtherService0 = new OtherService(localDependency75); return transientOtherService0; } diff --git a/readme/generic-composition-roots.md b/readme/generic-composition-roots.md index 8defd36a1..e61206224 100644 --- a/readme/generic-composition-roots.md +++ b/readme/generic-composition-roots.md @@ -76,8 +76,8 @@ partial class Composition public IService GetOtherService() { OtherService transientOtherService0; - IDependency localDependency70 = new Dependency(); - transientOtherService0 = new OtherService(localDependency70); + IDependency localDependency76 = new Dependency(); + transientOtherService0 = new OtherService(localDependency76); return transientOtherService0; } diff --git a/readme/keyed-service-provider.md b/readme/keyed-service-provider.md index 09529331a..37c781b88 100644 --- a/readme/keyed-service-provider.md +++ b/readme/keyed-service-provider.md @@ -53,7 +53,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Dependency? _singletonDependency39; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -73,18 +73,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return _root._singletonDependency39!; + return _root._singletonDependency41!; } } @@ -93,18 +93,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return new Service(_root._singletonDependency39!); + return new Service(_root._singletonDependency41!); } } diff --git a/readme/lazy.md b/readme/lazy.md index 8183479c3..1d737b796 100644 --- a/readme/lazy.md +++ b/readme/lazy.md @@ -55,14 +55,14 @@ partial class Composition { Func perBlockFunc2 = new Func([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - IDependency localValue41 = new Dependency(); - return localValue41; + IDependency localValue47 = new Dependency(); + return localValue47; }); Lazy transientLazy1; // Injects an instance factory - Func localFactory42 = perBlockFunc2; + Func localFactory48 = perBlockFunc2; // Creates an instance that supports lazy initialization - transientLazy1 = new Lazy(localFactory42, true); + transientLazy1 = new Lazy(localFactory48, true); return new Service(transientLazy1); } } diff --git a/readme/manually-started-tasks.md b/readme/manually-started-tasks.md index e36f25b47..9f6b790e2 100644 --- a/readme/manually-started-tasks.md +++ b/readme/manually-started-tasks.md @@ -93,13 +93,13 @@ partial class Composition { Func perBlockFunc2 = new Func([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - IDependency localValue43 = new Dependency(); - return localValue43; + IDependency localValue49 = new Dependency(); + return localValue49; }); Task transientTask1; - Func localFactory44 = perBlockFunc2; - CancellationToken localCancellationToken45 = cancellationToken; - transientTask1 = new Task(localFactory44, localCancellationToken45); + Func localFactory50 = perBlockFunc2; + CancellationToken localCancellationToken51 = cancellationToken; + transientTask1 = new Task(localFactory50, localCancellationToken51); return new Service(transientTask1); } } diff --git a/readme/onnewinstance-hint.md b/readme/onnewinstance-hint.md index a21ca6174..2e1a1b3e6 100644 --- a/readme/onnewinstance-hint.md +++ b/readme/onnewinstance-hint.md @@ -67,7 +67,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Dependency? _singletonDependency39; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -87,22 +87,22 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - Dependency _singletonDependency39Temp; - _singletonDependency39Temp = new Dependency(); - OnNewInstance(ref _singletonDependency39Temp, null, Lifetime.Singleton); + Dependency _singletonDependency41Temp; + _singletonDependency41Temp = new Dependency(); + OnNewInstance(ref _singletonDependency41Temp, null, Lifetime.Singleton); Thread.MemoryBarrier(); - _root._singletonDependency39 = _singletonDependency39Temp; + _root._singletonDependency41 = _singletonDependency41Temp; } } } - Service transientService0 = new Service(_root._singletonDependency39!); + Service transientService0 = new Service(_root._singletonDependency41!); OnNewInstance(ref transientService0, null, Lifetime.Transient); return transientService0; } diff --git a/readme/perblock.md b/readme/perblock.md index 0166bddf9..faf8133eb 100644 --- a/readme/perblock.md +++ b/readme/perblock.md @@ -52,8 +52,8 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private (IDependency dep3, IDependency dep4) _singletonValueTuple40; - private bool _singletonValueTuple40Created; + private (IDependency dep3, IDependency dep4) _singletonValueTuple42; + private bool _singletonValueTuple42Created; [OrdinalAttribute(20)] public Composition() @@ -73,22 +73,22 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (!_root._singletonValueTuple40Created) + if (!_root._singletonValueTuple42Created) { using (_lock.EnterScope()) { - if (!_root._singletonValueTuple40Created) + if (!_root._singletonValueTuple42Created) { Dependency perBlockDependency2 = new Dependency(); - _root._singletonValueTuple40 = (perBlockDependency2, perBlockDependency2); + _root._singletonValueTuple42 = (perBlockDependency2, perBlockDependency2); Thread.MemoryBarrier(); - _root._singletonValueTuple40Created = true; + _root._singletonValueTuple42Created = true; } } } Dependency perBlockDependency1 = new Dependency(); - return new Service(perBlockDependency1, perBlockDependency1, _root._singletonValueTuple40); + return new Service(perBlockDependency1, perBlockDependency1, _root._singletonValueTuple42); } } } diff --git a/readme/perresolve.md b/readme/perresolve.md index 2b2695001..a6763efc9 100644 --- a/readme/perresolve.md +++ b/readme/perresolve.md @@ -52,8 +52,8 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private (IDependency dep3, IDependency dep4) _singletonValueTuple40; - private bool _singletonValueTuple40Created; + private (IDependency dep3, IDependency dep4) _singletonValueTuple42; + private bool _singletonValueTuple42Created; [OrdinalAttribute(20)] public Composition() @@ -73,37 +73,37 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - var perResolveDependency39 = default(Dependency); - if (!_root._singletonValueTuple40Created) + var perResolveDependency41 = default(Dependency); + if (!_root._singletonValueTuple42Created) { using (_lock.EnterScope()) { - if (!_root._singletonValueTuple40Created) + if (!_root._singletonValueTuple42Created) { - if (perResolveDependency39 is null) + if (perResolveDependency41 is null) { - perResolveDependency39 = new Dependency(); + perResolveDependency41 = new Dependency(); } - _root._singletonValueTuple40 = (perResolveDependency39!, perResolveDependency39!); + _root._singletonValueTuple42 = (perResolveDependency41!, perResolveDependency41!); Thread.MemoryBarrier(); - _root._singletonValueTuple40Created = true; + _root._singletonValueTuple42Created = true; } } } - if (perResolveDependency39 is null) + if (perResolveDependency41 is null) { using (_lock.EnterScope()) { - if (perResolveDependency39 is null) + if (perResolveDependency41 is null) { - perResolveDependency39 = new Dependency(); + perResolveDependency41 = new Dependency(); } } } - return new Service(perResolveDependency39!, perResolveDependency39!, _root._singletonValueTuple40); + return new Service(perResolveDependency41!, perResolveDependency41!, _root._singletonValueTuple42); } } } diff --git a/readme/root-binding.md b/readme/root-binding.md index c5403c4f6..e9b593417 100644 --- a/readme/root-binding.md +++ b/readme/root-binding.md @@ -33,7 +33,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Dependency? _singletonDependency39; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -53,18 +53,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return new Service(_root._singletonDependency39!); + return new Service(_root._singletonDependency41!); } } } diff --git a/readme/scope.md b/readme/scope.md index 7e8f99bb8..c9cbc39a2 100644 --- a/readme/scope.md +++ b/readme/scope.md @@ -91,7 +91,7 @@ partial class Composition: IDisposable private object[] _disposables; private int _disposeIndex; - private Dependency? _scopedDependency39; + private Dependency? _scopedDependency41; [OrdinalAttribute(20)] public Composition() @@ -113,19 +113,19 @@ partial class Composition: IDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_scopedDependency39 is null) + if (_scopedDependency41 is null) { using (_lock.EnterScope()) { - if (_scopedDependency39 is null) + if (_scopedDependency41 is null) { - _scopedDependency39 = new Dependency(); - _disposables[_disposeIndex++] = _scopedDependency39; + _scopedDependency41 = new Dependency(); + _disposables[_disposeIndex++] = _scopedDependency41; } } } - return new Service(_scopedDependency39!); + return new Service(_scopedDependency41!); } } @@ -137,8 +137,8 @@ partial class Composition: IDisposable Func perBlockFunc1 = new Func([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { Composition transientComposition3 = this; - Session localValue76 = new Session(transientComposition3); - return localValue76; + Session localValue82 = new Session(transientComposition3); + return localValue82; }); return new Program(perBlockFunc1); } @@ -154,7 +154,7 @@ partial class Composition: IDisposable _disposeIndex = 0; disposables = _disposables; _disposables = new object[1]; - _scopedDependency39 = null; + _scopedDependency41 = null; } while (disposeIndex-- > 0) diff --git a/readme/service-collection.md b/readme/service-collection.md index b7adac0a4..4e8935753 100644 --- a/readme/service-collection.md +++ b/readme/service-collection.md @@ -50,7 +50,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Dependency? _singletonDependency39; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -70,18 +70,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return _root._singletonDependency39!; + return _root._singletonDependency41!; } } @@ -90,18 +90,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return new Service(_root._singletonDependency39!); + return new Service(_root._singletonDependency41!); } } diff --git a/readme/service-provider-with-scope.md b/readme/service-provider-with-scope.md index b1cfe1f74..8785281ae 100644 --- a/readme/service-provider-with-scope.md +++ b/readme/service-provider-with-scope.md @@ -81,8 +81,8 @@ partial class Composition: IDisposable private object[] _disposables; private int _disposeIndex; - private Service? _scopedService40; - private Dependency? _singletonDependency39; + private Service? _scopedService42; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -104,18 +104,18 @@ partial class Composition: IDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return _root._singletonDependency39!; + return _root._singletonDependency41!; } } @@ -124,24 +124,24 @@ partial class Composition: IDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_scopedService40 is null) + if (_scopedService42 is null) { using (_lock.EnterScope()) { - if (_scopedService40 is null) + if (_scopedService42 is null) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } - _scopedService40 = new Service(_root._singletonDependency39!); - _disposables[_disposeIndex++] = _scopedService40; + _scopedService42 = new Service(_root._singletonDependency41!); + _disposables[_disposeIndex++] = _scopedService42; } } } - return _scopedService40!; + return _scopedService42!; } } @@ -215,8 +215,8 @@ partial class Composition: IDisposable _disposeIndex = 0; disposables = _disposables; _disposables = new object[1]; - _scopedService40 = null; - _singletonDependency39 = null; + _scopedService42 = null; + _singletonDependency41 = null; } while (disposeIndex-- > 0) diff --git a/readme/service-provider.md b/readme/service-provider.md index a0cd14fd9..617a5d985 100644 --- a/readme/service-provider.md +++ b/readme/service-provider.md @@ -48,7 +48,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Dependency? _singletonDependency39; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -68,18 +68,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return _root._singletonDependency39!; + return _root._singletonDependency41!; } } @@ -88,18 +88,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return new Service(_root._singletonDependency39!); + return new Service(_root._singletonDependency41!); } } diff --git a/readme/simplified-factory.md b/readme/simplified-factory.md index 524c4a77a..60eea06d9 100644 --- a/readme/simplified-factory.md +++ b/readme/simplified-factory.md @@ -82,11 +82,11 @@ partial class Composition get { Dependency transientDependency1; - Dependency localDependency36 = new Dependency(); + Dependency localDependency42 = new Dependency(); DateTimeOffset transientDateTimeOffset3 = DateTimeOffset.Now; - DateTimeOffset localTime37 = transientDateTimeOffset3; - localDependency36.Initialize(localTime37); - transientDependency1 = localDependency36; + DateTimeOffset localTime43 = transientDateTimeOffset3; + localDependency42.Initialize(localTime43); + transientDependency1 = localDependency42; return new Service(transientDependency1); } } diff --git a/readme/singleton.md b/readme/singleton.md index 5d5124479..68724ae30 100644 --- a/readme/singleton.md +++ b/readme/singleton.md @@ -63,7 +63,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private Dependency? _singletonDependency39; + private Dependency? _singletonDependency41; [OrdinalAttribute(20)] public Composition() @@ -83,18 +83,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency39 is null) + if (_root._singletonDependency41 is null) { - _root._singletonDependency39 = new Dependency(); + _root._singletonDependency41 = new Dependency(); } } } - return new Service(_root._singletonDependency39!, _root._singletonDependency39!); + return new Service(_root._singletonDependency41!, _root._singletonDependency41!); } } } diff --git a/readme/tag-type.md b/readme/tag-type.md index bfaaffe0a..12cb2d33a 100644 --- a/readme/tag-type.md +++ b/readme/tag-type.md @@ -63,7 +63,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private XyzDependency? _singletonXyzDependency40; + private XyzDependency? _singletonXyzDependency42; [OrdinalAttribute(20)] public Composition() @@ -83,18 +83,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonXyzDependency40 is null) + if (_root._singletonXyzDependency42 is null) { using (_lock.EnterScope()) { - if (_root._singletonXyzDependency40 is null) + if (_root._singletonXyzDependency42 is null) { - _root._singletonXyzDependency40 = new XyzDependency(); + _root._singletonXyzDependency42 = new XyzDependency(); } } } - return _root._singletonXyzDependency40!; + return _root._singletonXyzDependency42!; } } @@ -103,18 +103,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonXyzDependency40 is null) + if (_root._singletonXyzDependency42 is null) { using (_lock.EnterScope()) { - if (_root._singletonXyzDependency40 is null) + if (_root._singletonXyzDependency42 is null) { - _root._singletonXyzDependency40 = new XyzDependency(); + _root._singletonXyzDependency42 = new XyzDependency(); } } } - return new Service(new AbcDependency(), _root._singletonXyzDependency40!, new AbcDependency()); + return new Service(new AbcDependency(), _root._singletonXyzDependency42!, new AbcDependency()); } } diff --git a/readme/tags.md b/readme/tags.md index f7b5bb2ac..50ee22312 100644 --- a/readme/tags.md +++ b/readme/tags.md @@ -67,7 +67,7 @@ partial class Composition private readonly Composition _root; private readonly Lock _lock; - private XyzDependency? _singletonXyzDependency40; + private XyzDependency? _singletonXyzDependency42; [OrdinalAttribute(20)] public Composition() @@ -87,18 +87,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonXyzDependency40 is null) + if (_root._singletonXyzDependency42 is null) { using (_lock.EnterScope()) { - if (_root._singletonXyzDependency40 is null) + if (_root._singletonXyzDependency42 is null) { - _root._singletonXyzDependency40 = new XyzDependency(); + _root._singletonXyzDependency42 = new XyzDependency(); } } } - return _root._singletonXyzDependency40!; + return _root._singletonXyzDependency42!; } } @@ -107,18 +107,18 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonXyzDependency40 is null) + if (_root._singletonXyzDependency42 is null) { using (_lock.EnterScope()) { - if (_root._singletonXyzDependency40 is null) + if (_root._singletonXyzDependency42 is null) { - _root._singletonXyzDependency40 = new XyzDependency(); + _root._singletonXyzDependency42 = new XyzDependency(); } } } - return new Service(new AbcDependency(), _root._singletonXyzDependency40!, new AbcDependency()); + return new Service(new AbcDependency(), _root._singletonXyzDependency42!, new AbcDependency()); } } } diff --git a/readme/task.md b/readme/task.md index 3098dbe83..450c7382d 100644 --- a/readme/task.md +++ b/readme/task.md @@ -80,26 +80,26 @@ partial class Composition public IService GetRoot(CancellationToken cancellationToken) { TaskFactory perBlockTaskFactory3; - CancellationToken localCancellationToken46 = cancellationToken; + CancellationToken localCancellationToken52 = cancellationToken; TaskCreationOptions transientTaskCreationOptions4 = TaskCreationOptions.None; - TaskCreationOptions localTaskCreationOptions47 = transientTaskCreationOptions4; + TaskCreationOptions localTaskCreationOptions53 = transientTaskCreationOptions4; TaskContinuationOptions transientTaskContinuationOptions5 = TaskContinuationOptions.None; - TaskContinuationOptions localTaskContinuationOptions48 = transientTaskContinuationOptions5; + TaskContinuationOptions localTaskContinuationOptions54 = transientTaskContinuationOptions5; TaskScheduler transientTaskScheduler6 = TaskScheduler.Current; - TaskScheduler localTaskScheduler49 = transientTaskScheduler6; - perBlockTaskFactory3 = new TaskFactory(localCancellationToken46, localTaskCreationOptions47, localTaskContinuationOptions48, localTaskScheduler49); + TaskScheduler localTaskScheduler55 = transientTaskScheduler6; + perBlockTaskFactory3 = new TaskFactory(localCancellationToken52, localTaskCreationOptions53, localTaskContinuationOptions54, localTaskScheduler55); Func perBlockFunc2 = new Func([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - IDependency localValue50 = new Dependency(); - return localValue50; + IDependency localValue56 = new Dependency(); + return localValue56; }); Task transientTask1; // Injects an instance factory - Func localFactory51 = perBlockFunc2; + Func localFactory57 = perBlockFunc2; // Injects a task factory creating and scheduling task objects - TaskFactory localTaskFactory52 = perBlockTaskFactory3; + TaskFactory localTaskFactory58 = perBlockTaskFactory3; // Creates and starts a task using the instance factory - transientTask1 = localTaskFactory52.StartNew(localFactory51); + transientTask1 = localTaskFactory58.StartNew(localFactory57); return new Service(transientTask1); } } diff --git a/readme/threadsafe-hint.md b/readme/threadsafe-hint.md index 1f5d3f42a..f2b5e8ca8 100644 --- a/readme/threadsafe-hint.md +++ b/readme/threadsafe-hint.md @@ -36,7 +36,7 @@ partial class Composition { private readonly Composition _root; - private Service? _singletonService40; + private Service? _singletonService42; [OrdinalAttribute(20)] public Composition() @@ -54,17 +54,17 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - if (_root._singletonService40 is null) + if (_root._singletonService42 is null) { Func perBlockFunc0 = new Func([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - IDependency localValue71 = new Dependency(); - return localValue71; + IDependency localValue77 = new Dependency(); + return localValue77; }); - _root._singletonService40 = new Service(perBlockFunc0); + _root._singletonService42 = new Service(perBlockFunc0); } - return _root._singletonService40!; + return _root._singletonService42!; } } } diff --git a/readme/tracking-async-disposable-instances-in-delegates.md b/readme/tracking-async-disposable-instances-in-delegates.md index 15238a547..9d4b303d4 100644 --- a/readme/tracking-async-disposable-instances-in-delegates.md +++ b/readme/tracking-async-disposable-instances-in-delegates.md @@ -96,23 +96,23 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - var accumulator43 = new Owned(); + var accumulator45 = new Owned(); Func> perBlockFunc1 = new Func>([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - var accumulator43 = new Owned(); + var accumulator45 = new Owned(); Dependency transientDependency4 = new Dependency(); using (_lock.EnterScope()) { - accumulator43.Add(transientDependency4); + accumulator45.Add(transientDependency4); } Owned perBlockOwned2; // Creates the owner of an instance - Owned localOwned8 = accumulator43; + Owned localOwned8 = accumulator45; IDependency localValue9 = transientDependency4; perBlockOwned2 = new Owned(localValue9, localOwned8); using (_lock.EnterScope()) { - accumulator43.Add(perBlockOwned2); + accumulator45.Add(perBlockOwned2); } Owned localValue7 = perBlockOwned2; return localValue7; @@ -120,7 +120,7 @@ partial class Composition Service transientService0 = new Service(perBlockFunc1); using (_lock.EnterScope()) { - accumulator43.Add(transientService0); + accumulator45.Add(transientService0); } return transientService0; } diff --git a/readme/tracking-async-disposable-instances-per-a-composition-root.md b/readme/tracking-async-disposable-instances-per-a-composition-root.md index 76b95de46..815a692a3 100644 --- a/readme/tracking-async-disposable-instances-per-a-composition-root.md +++ b/readme/tracking-async-disposable-instances-per-a-composition-root.md @@ -89,20 +89,20 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - var accumulator42 = new Owned(); + var accumulator44 = new Owned(); Dependency transientDependency3 = new Dependency(); using (_lock.EnterScope()) { - accumulator42.Add(transientDependency3); + accumulator44.Add(transientDependency3); } Owned perBlockOwned0; // Creates the owner of an instance - Owned localOwned10 = accumulator42; + Owned localOwned10 = accumulator44; IService localValue11 = new Service(transientDependency3); perBlockOwned0 = new Owned(localValue11, localOwned10); using (_lock.EnterScope()) { - accumulator42.Add(perBlockOwned0); + accumulator44.Add(perBlockOwned0); } return perBlockOwned0; } diff --git a/readme/tracking-disposable-instances-in-delegates.md b/readme/tracking-disposable-instances-in-delegates.md index 1847c0ab1..b9974e28f 100644 --- a/readme/tracking-disposable-instances-in-delegates.md +++ b/readme/tracking-disposable-instances-in-delegates.md @@ -89,23 +89,23 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - var accumulator43 = new Owned(); + var accumulator45 = new Owned(); Func> perBlockFunc1 = new Func>([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - var accumulator43 = new Owned(); + var accumulator45 = new Owned(); Dependency transientDependency4 = new Dependency(); using (_lock.EnterScope()) { - accumulator43.Add(transientDependency4); + accumulator45.Add(transientDependency4); } Owned perBlockOwned2; // Creates the owner of an instance - Owned localOwned13 = accumulator43; + Owned localOwned13 = accumulator45; IDependency localValue14 = transientDependency4; perBlockOwned2 = new Owned(localValue14, localOwned13); using (_lock.EnterScope()) { - accumulator43.Add(perBlockOwned2); + accumulator45.Add(perBlockOwned2); } Owned localValue12 = perBlockOwned2; return localValue12; @@ -113,7 +113,7 @@ partial class Composition Service transientService0 = new Service(perBlockFunc1); using (_lock.EnterScope()) { - accumulator43.Add(transientService0); + accumulator45.Add(transientService0); } return transientService0; } diff --git a/readme/tracking-disposable-instances-per-a-composition-root.md b/readme/tracking-disposable-instances-per-a-composition-root.md index 781f59799..e3c0e2a3c 100644 --- a/readme/tracking-disposable-instances-per-a-composition-root.md +++ b/readme/tracking-disposable-instances-per-a-composition-root.md @@ -85,20 +85,20 @@ partial class Composition [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - var accumulator42 = new Owned(); + var accumulator44 = new Owned(); Dependency transientDependency3 = new Dependency(); using (_lock.EnterScope()) { - accumulator42.Add(transientDependency3); + accumulator44.Add(transientDependency3); } Owned perBlockOwned0; // Creates the owner of an instance - Owned localOwned15 = accumulator42; + Owned localOwned15 = accumulator44; IService localValue16 = new Service(transientDependency3); perBlockOwned0 = new Owned(localValue16, localOwned15); using (_lock.EnterScope()) { - accumulator42.Add(perBlockOwned0); + accumulator44.Add(perBlockOwned0); } return perBlockOwned0; } diff --git a/readme/tracking-disposable-instances-using-pre-built-classes.md b/readme/tracking-disposable-instances-using-pre-built-classes.md new file mode 100644 index 000000000..b5f194be1 --- /dev/null +++ b/readme/tracking-disposable-instances-using-pre-built-classes.md @@ -0,0 +1,371 @@ +#### Tracking disposable instances using pre-built classes + +[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](../tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithAbstractionsScenario.cs) + +If you want ready-made classes for tracking disposable objects in your libraries but don't want to create your own, you can add this package to your projects: + +[![NuGet](https://img.shields.io/nuget/v/Pure.DI.Abstractions)](https://www.nuget.org/packages/Pure.DI.Abstractions) + +It contains attributes like `Inject` and `Inject` that work for constructors and their arguments, methods and their arguments, properties and fields. They allow you to setup all injection parameters. + + +```c# +interface IDependency +{ + bool IsDisposed { get; } +} + +class Dependency : IDependency, IDisposable +{ + public bool IsDisposed { get; private set; } + + public void Dispose() => IsDisposed = true; +} + +interface IService +{ + public IDependency Dependency { get; } + + public IDependency SingleDependency { get; } +} + +class Service( + Func> dependencyFactory, + [Tag("single")] Func> singleDependencyFactory) + : IService, IDisposable +{ + private readonly Own _dependency = dependencyFactory(); + private readonly Own _singleDependency = singleDependencyFactory(); + + public IDependency Dependency => _dependency.Value; + + public IDependency SingleDependency => _singleDependency.Value; + + public void Dispose() + { + _dependency.Dispose(); + _singleDependency.Dispose(); + } +} + +partial class Composition +{ + static void Setup() => + DI.Setup() + .Bind().To() + .Bind("single").As(Lifetime.Singleton).To() + .Bind().To() + + // Composition root + .Root("Root"); +} + +var composition = new Composition(); +var root1 = composition.Root; +var root2 = composition.Root; +root1.Dependency.ShouldNotBe(root2.Dependency); +root1.SingleDependency.ShouldBe(root2.SingleDependency); + +root2.Dispose(); + +// Checks that the disposable instances +// associated with root1 have been disposed of +root2.Dependency.IsDisposed.ShouldBeTrue(); + +// But the singleton is still not disposed of +root2.SingleDependency.IsDisposed.ShouldBeFalse(); + +// Checks that the disposable instances +// associated with root2 have not been disposed of +root1.Dependency.IsDisposed.ShouldBeFalse(); +root1.SingleDependency.IsDisposed.ShouldBeFalse(); + +root1.Dispose(); + +// Checks that the disposable instances +// associated with root2 have been disposed of +root1.Dependency.IsDisposed.ShouldBeTrue(); + +// But the singleton is still not disposed of +root1.SingleDependency.IsDisposed.ShouldBeFalse(); + +composition.Dispose(); +root1.SingleDependency.IsDisposed.ShouldBeTrue(); +``` + +This package should also be included in a project: + +[![NuGet](https://img.shields.io/nuget/v/Pure.DI)](https://www.nuget.org/packages/Pure.DI) + +The following partial class will be generated: + +```c# +partial class Composition: IDisposable +{ + private readonly Composition _root; + private readonly Lock _lock; + private object[] _disposables; + private int _disposeIndex; + + private Dependency? _singletonDependency42; + + [OrdinalAttribute(20)] + public Composition() + { + _root = this; + _lock = new Lock(); + _disposables = new object[1]; + } + + internal Composition(Composition parentScope) + { + _root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root; + _lock = _root._lock; + _disposables = parentScope._disposables; + } + + public Service Root + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + var accumulator48 = new Abstractions.Own(); + Func> perBlockFunc2 = new Func>([MethodImpl(MethodImplOptions.AggressiveInlining)] () => + { + var accumulator48 = new Abstractions.Own(); + if (_root._singletonDependency42 is null) + { + using (_lock.EnterScope()) + { + if (_root._singletonDependency42 is null) + { + _root._singletonDependency42 = new Dependency(); + _root._disposables[_root._disposeIndex++] = _root._singletonDependency42; + } + } + } + + Abstractions.Own perBlockOwn3; + // Creates the owner of an instance + Abstractions.Own localOwn18 = accumulator48; + IDependency localValue19 = _root._singletonDependency42!; + perBlockOwn3 = new Abstractions.Own(localValue19, localOwn18); + using (_lock.EnterScope()) + { + accumulator48.Add(perBlockOwn3); + } + Abstractions.Own localValue17 = perBlockOwn3; + return localValue17; + }); + Func> perBlockFunc1 = new Func>([MethodImpl(MethodImplOptions.AggressiveInlining)] () => + { + var accumulator48 = new Abstractions.Own(); + Dependency transientDependency7 = new Dependency(); + using (_lock.EnterScope()) + { + accumulator48.Add(transientDependency7); + } + Abstractions.Own perBlockOwn5; + // Creates the owner of an instance + Abstractions.Own localOwn21 = accumulator48; + IDependency localValue22 = transientDependency7; + perBlockOwn5 = new Abstractions.Own(localValue22, localOwn21); + using (_lock.EnterScope()) + { + accumulator48.Add(perBlockOwn5); + } + Abstractions.Own localValue20 = perBlockOwn5; + return localValue20; + }); + Service transientService0 = new Service(perBlockFunc1, perBlockFunc2); + using (_lock.EnterScope()) + { + accumulator48.Add(transientService0); + } + return transientService0; + } + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T Resolve() + { + return Resolver.Value.Resolve(this); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T Resolve(object? tag) + { + return Resolver.Value.ResolveByTag(this, tag); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public object Resolve(Type type) + { + var index = (int)(_bucketSize * ((uint)RuntimeHelpers.GetHashCode(type) % 1)); + ref var pair = ref _buckets[index]; + return pair.Key == type ? pair.Value.Resolve(this) : Resolve(type, index); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private object Resolve(Type type, int index) + { + var finish = index + _bucketSize; + while (++index < finish) + { + ref var pair = ref _buckets[index]; + if (pair.Key == type) + { + return pair.Value.Resolve(this); + } + } + + throw new InvalidOperationException($"{CannotResolveMessage} {OfTypeMessage} {type}."); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public object Resolve(Type type, object? tag) + { + var index = (int)(_bucketSize * ((uint)RuntimeHelpers.GetHashCode(type) % 1)); + ref var pair = ref _buckets[index]; + return pair.Key == type ? pair.Value.ResolveByTag(this, tag) : Resolve(type, tag, index); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private object Resolve(Type type, object? tag, int index) + { + var finish = index + _bucketSize; + while (++index < finish) + { + ref var pair = ref _buckets[index]; + if (pair.Key == type) + { + return pair.Value.ResolveByTag(this, tag); + } + } + + throw new InvalidOperationException($"{CannotResolveMessage} \"{tag}\" {OfTypeMessage} {type}."); + } + + public void Dispose() + { + int disposeIndex; + object[] disposables; + using (_lock.EnterScope()) + { + disposeIndex = _disposeIndex; + _disposeIndex = 0; + disposables = _disposables; + _disposables = new object[1]; + _singletonDependency42 = null; + } + + while (disposeIndex-- > 0) + { + switch (disposables[disposeIndex]) + { + case IDisposable disposableInstance: + try + { + disposableInstance.Dispose(); + } + catch (Exception exception) + { + OnDisposeException(disposableInstance, exception); + } + break; + } + } + } + + partial void OnDisposeException(T disposableInstance, Exception exception) where T : IDisposable; + + private readonly static int _bucketSize; + private readonly static Pair>[] _buckets; + + static Composition() + { + var valResolver_0000 = new Resolver_0000(); + Resolver.Value = valResolver_0000; + _buckets = Buckets>.Create( + 1, + out _bucketSize, + new Pair>[1] + { + new Pair>(typeof(Service), valResolver_0000) + }); + } + + private const string CannotResolveMessage = "Cannot resolve composition root "; + private const string OfTypeMessage = "of type "; + + private class Resolver: IResolver + { + public static IResolver Value = new Resolver(); + + public virtual T Resolve(Composition composite) + { + throw new InvalidOperationException($"{CannotResolveMessage}{OfTypeMessage}{typeof(T)}."); + } + + public virtual T ResolveByTag(Composition composite, object tag) + { + throw new InvalidOperationException($"{CannotResolveMessage}\"{tag}\" {OfTypeMessage}{typeof(T)}."); + } + } + + private sealed class Resolver_0000: Resolver + { + public override Service Resolve(Composition composition) + { + return composition.Root; + } + + public override Service ResolveByTag(Composition composition, object tag) + { + switch (tag) + { + case null: + return composition.Root; + + default: + return base.ResolveByTag(composition, tag); + } + } + } +} +``` + +Class diagram: + +```mermaid +classDiagram + class Composition { + <> + +Service Root + + T ResolveᐸTᐳ() + + T ResolveᐸTᐳ(object? tag) + + object Resolve(Type type) + + object Resolve(Type type, object? tag) + } + Composition --|> IDisposable + class FuncᐸOwnᐸIDependencyᐳᐳ + class OwnᐸIDependencyᐳ + class Own + Dependency --|> IDependency + class Dependency { + +Dependency() + } + class IDependency { + <> + } + Composition ..> Service : Service Root + Service o-- "PerBlock" FuncᐸOwnᐸIDependencyᐳᐳ : FuncᐸOwnᐸIDependencyᐳᐳ + Service o-- "PerBlock" FuncᐸOwnᐸIDependencyᐳᐳ : "single" FuncᐸOwnᐸIDependencyᐳᐳ + FuncᐸOwnᐸIDependencyᐳᐳ o-- "PerBlock" OwnᐸIDependencyᐳ : OwnᐸIDependencyᐳ + FuncᐸOwnᐸIDependencyᐳᐳ o-- "PerBlock" OwnᐸIDependencyᐳ : "single" OwnᐸIDependencyᐳ + OwnᐸIDependencyᐳ *-- Own : Own + OwnᐸIDependencyᐳ *-- Dependency : IDependency + OwnᐸIDependencyᐳ *-- Own : Own + OwnᐸIDependencyᐳ o-- "Singleton" Dependency : "single" IDependency +``` + diff --git a/readme/tracking-disposable-instances-with-different-lifetimes.md b/readme/tracking-disposable-instances-with-different-lifetimes.md index 579c64014..66ccce52f 100644 --- a/readme/tracking-disposable-instances-with-different-lifetimes.md +++ b/readme/tracking-disposable-instances-with-different-lifetimes.md @@ -97,7 +97,7 @@ partial class Composition: IDisposable private object[] _disposables; private int _disposeIndex; - private Dependency? _singletonDependency40; + private Dependency? _singletonDependency42; [OrdinalAttribute(20)] public Composition() @@ -119,58 +119,58 @@ partial class Composition: IDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - var accumulator46 = new Owned(); + var accumulator48 = new Owned(); Func> perBlockFunc2 = new Func>([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - var accumulator46 = new Owned(); - if (_root._singletonDependency40 is null) + var accumulator48 = new Owned(); + if (_root._singletonDependency42 is null) { using (_lock.EnterScope()) { - if (_root._singletonDependency40 is null) + if (_root._singletonDependency42 is null) { - _root._singletonDependency40 = new Dependency(); - _root._disposables[_root._disposeIndex++] = _root._singletonDependency40; + _root._singletonDependency42 = new Dependency(); + _root._disposables[_root._disposeIndex++] = _root._singletonDependency42; } } } Owned perBlockOwned3; // Creates the owner of an instance - Owned localOwned18 = accumulator46; - IDependency localValue19 = _root._singletonDependency40!; - perBlockOwned3 = new Owned(localValue19, localOwned18); + Owned localOwned24 = accumulator48; + IDependency localValue25 = _root._singletonDependency42!; + perBlockOwned3 = new Owned(localValue25, localOwned24); using (_lock.EnterScope()) { - accumulator46.Add(perBlockOwned3); + accumulator48.Add(perBlockOwned3); } - Owned localValue17 = perBlockOwned3; - return localValue17; + Owned localValue23 = perBlockOwned3; + return localValue23; }); Func> perBlockFunc1 = new Func>([MethodImpl(MethodImplOptions.AggressiveInlining)] () => { - var accumulator46 = new Owned(); + var accumulator48 = new Owned(); Dependency transientDependency7 = new Dependency(); using (_lock.EnterScope()) { - accumulator46.Add(transientDependency7); + accumulator48.Add(transientDependency7); } Owned perBlockOwned5; // Creates the owner of an instance - Owned localOwned21 = accumulator46; - IDependency localValue22 = transientDependency7; - perBlockOwned5 = new Owned(localValue22, localOwned21); + Owned localOwned27 = accumulator48; + IDependency localValue28 = transientDependency7; + perBlockOwned5 = new Owned(localValue28, localOwned27); using (_lock.EnterScope()) { - accumulator46.Add(perBlockOwned5); + accumulator48.Add(perBlockOwned5); } - Owned localValue20 = perBlockOwned5; - return localValue20; + Owned localValue26 = perBlockOwned5; + return localValue26; }); Service transientService0 = new Service(perBlockFunc1, perBlockFunc2); using (_lock.EnterScope()) { - accumulator46.Add(transientService0); + accumulator48.Add(transientService0); } return transientService0; } @@ -246,7 +246,7 @@ partial class Composition: IDisposable _disposeIndex = 0; disposables = _disposables; _disposables = new object[1]; - _singletonDependency40 = null; + _singletonDependency42 = null; } while (disposeIndex-- > 0) diff --git a/readme/valuetask.md b/readme/valuetask.md index 2489b555b..b91ff8ca5 100644 --- a/readme/valuetask.md +++ b/readme/valuetask.md @@ -64,9 +64,9 @@ partial class Composition get { ValueTask transientValueTask1; - IDependency localValue53 = new Dependency(); + IDependency localValue59 = new Dependency(); // Initializes a new instance of the ValueTask class using the supplied instance - transientValueTask1 = new ValueTask(localValue53); + transientValueTask1 = new ValueTask(localValue59); return new Service(transientValueTask1); } } diff --git a/src/Pure.DI.Abstractions/IOwn.cs b/src/Pure.DI.Abstractions/IOwn.cs new file mode 100644 index 000000000..d1549301e --- /dev/null +++ b/src/Pure.DI.Abstractions/IOwn.cs @@ -0,0 +1,14 @@ +namespace Pure.DI.Abstractions; + +using System; + +/// +/// This abstraction allows a disposable object to be disposed of. +/// +public interface IOwn: + IDisposable +#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER + , IAsyncDisposable +#endif +{ +} \ No newline at end of file diff --git a/src/Pure.DI.Abstractions/Own.cs b/src/Pure.DI.Abstractions/Own.cs new file mode 100644 index 000000000..738412398 --- /dev/null +++ b/src/Pure.DI.Abstractions/Own.cs @@ -0,0 +1,122 @@ +// ReSharper disable RedundantUsingDirective +namespace Pure.DI.Abstractions; + +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +/// +/// Performs accumulation and disposal of disposable objects. +/// +#if !NET20 && !NET35 && !NETSTANDARD1_0 && !NETSTANDARD1_1 && !NETSTANDARD1_2 && !NETSTANDARD1_3 && !NETSTANDARD1_4 && !NETSTANDARD1_5 && !NETSTANDARD1_6 && !NETCOREAPP1_0 && !NETCOREAPP1_1 +[ExcludeFromCodeCoverage] +#endif +public sealed class Own : List, IOwn +{ + private volatile bool _isDisposed; + + /// + public void Dispose() + { + if (_isDisposed) + { + return; + } + + _isDisposed = true; + try + { + for (var i = Count - 1; i >= 0; i--) + { + switch (this[i]) + { + case IOwn: + break; + + case IDisposable disposableInstance: + try + { + disposableInstance.Dispose(); + } + catch (Exception) + { + // ignored + } + + break; + +#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER + case IAsyncDisposable asyncDisposableInstance: + try + { + var valueTask = asyncDisposableInstance.DisposeAsync(); + if (!valueTask.IsCompleted) + { + valueTask.AsTask().Wait(); + } + } + catch (Exception) + { + // ignored + } + break; +#endif + } + } + } + finally + { + Clear(); + } + } + +#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + public async System.Threading.Tasks.ValueTask DisposeAsync() + { + if (_isDisposed) + { + return; + } + + _isDisposed = true; + try + { + for (var i = Count - 1; i >= 0; i--) + { + switch (this[i]) + { + case IOwn: + break; + + case IAsyncDisposable asyncDisposableInstance: + try + { + await asyncDisposableInstance.DisposeAsync(); + } + catch (Exception) + { + // ignored + } + break; + + case IDisposable disposableInstance: + try + { + disposableInstance.Dispose(); + } + catch (Exception) + { + // ignored + } + break; + } + } + } + finally + { + Clear(); + } + } +#endif +} \ No newline at end of file diff --git a/src/Pure.DI.Abstractions/Own`1.cs b/src/Pure.DI.Abstractions/Own`1.cs new file mode 100644 index 000000000..54e0e93aa --- /dev/null +++ b/src/Pure.DI.Abstractions/Own`1.cs @@ -0,0 +1,46 @@ +// ReSharper disable UnusedMember.Local +// ReSharper disable RedundantUsingDirective +namespace Pure.DI.Abstractions; + +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; + +/// +/// Contains a value and gives the ability to dispose of that value. +/// +/// Own value. +/// The abstraction allows a disposable object to be disposed of. +/// Type of value owned. +[DebuggerDisplay("{Value}")] +[DebuggerTypeProxy(typeof(Own<>.DebugView))] +public readonly struct Own(T value, IOwn own) : IOwn +{ + /// + /// Own value. + /// + public readonly T Value = value; + + private readonly IOwn _own = own; + + /// + public void Dispose() => _own.Dispose(); + +#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER + /// + public System.Threading.Tasks.ValueTask DisposeAsync() + { + return _own.DisposeAsync(); + } +#endif + +#if !NET20 && !NET35 && !NETSTANDARD1_0 && !NETSTANDARD1_1 && !NETSTANDARD1_2 && !NETSTANDARD1_3 && !NETSTANDARD1_4 && !NETSTANDARD1_5 && !NETSTANDARD1_6 && !NETCOREAPP1_0 && !NETCOREAPP1_1 + [ExcludeFromCodeCoverage] +#endif + private class DebugView(Own own) + { + public T Value => own.Value; + + [DebuggerBrowsable(DebuggerBrowsableState.Collapsed)] + public IOwn Own => own._own; + } +} \ No newline at end of file diff --git a/src/Pure.DI.Abstractions/any/Pure.DI/Abstractions/Composition.g.cs b/src/Pure.DI.Abstractions/any/Pure.DI/Abstractions/Composition.g.cs index ba093abe6..7a3587055 100644 --- a/src/Pure.DI.Abstractions/any/Pure.DI/Abstractions/Composition.g.cs +++ b/src/Pure.DI.Abstractions/any/Pure.DI/Abstractions/Composition.g.cs @@ -18,7 +18,26 @@ private static void Setup() .OrdinalAttribute>(1) .TypeAttribute>() .TagAttribute() - .OrdinalAttribute(1); + .OrdinalAttribute(1) + .Accumulate( + Lifetime.Transient, + Lifetime.PerResolve, + Lifetime.PerBlock) +#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER + .Accumulate( + Lifetime.Transient, + Lifetime.PerResolve, + Lifetime.PerBlock) +#endif + .Bind().To((global::Pure.DI.Abstractions.Own own) => own) + .Bind>() + .As(Lifetime.PerBlock) + .To(ctx => { + // Creates the owner of an instance + ctx.Inject(out var own); + ctx.Inject(ctx.Tag, out var value); + return new global::Pure.DI.Abstractions.Own(value, own); + }); } } } diff --git a/src/Pure.DI.Core/Components/Api.g.cs b/src/Pure.DI.Core/Components/Api.g.cs index 14b207a9b..9498444b0 100644 --- a/src/Pure.DI.Core/Components/Api.g.cs +++ b/src/Pure.DI.Core/Components/Api.g.cs @@ -1225,7 +1225,7 @@ internal class Tag } /// - /// Gives the ability to manage disposable objects. + /// This abstraction allows a disposable object to be disposed of. /// internal interface IOwned : global::System.IDisposable @@ -1236,12 +1236,12 @@ internal interface IOwned } /// - /// Gives the ability to manage disposable objects. + /// Performs accumulation and disposal of disposable objects. /// #if !NET20 && !NET35 && !NETSTANDARD1_0 && !NETSTANDARD1_1 && !NETSTANDARD1_2 && !NETSTANDARD1_3 && !NETSTANDARD1_4 && !NETSTANDARD1_5 && !NETSTANDARD1_6 && !NETCOREAPP1_0 && !NETCOREAPP1_1 [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] #endif - internal sealed partial class Owned : global::System.Collections.Generic.List, global::Pure.DI.IOwned + internal sealed partial class Owned: global::System.Collections.Generic.List, global::Pure.DI.IOwned { private volatile bool _isDisposed; @@ -1373,18 +1373,24 @@ partial void OnDisposeAsyncException(T asyncDisposableInstance, global::Syste } /// - /// Contains a value and gives the ability to manage disposable objects. + /// Contains a value and gives the ability to dispose of that value. /// + /// Type of value owned. [global::System.Diagnostics.DebuggerDisplay("{Value}")] [global::System.Diagnostics.DebuggerTypeProxy(typeof(global::Pure.DI.Owned<>.DebugView))] - internal readonly struct Owned : global::Pure.DI.IOwned + internal readonly struct Owned: global::Pure.DI.IOwned { /// - /// The value. + /// Own value. /// public readonly T Value; private readonly global::Pure.DI.IOwned _owned; - + + /// + /// Creates a new instance. + /// + /// Own value. + /// The abstraction allows a disposable object to be disposed of. public Owned(T value, global::Pure.DI.IOwned owned) { Value = value; diff --git a/src/Pure.DI.Templates/Templates/Pure.DI.Template.ClassLibrary/ConsoleAdapter.cs b/src/Pure.DI.Templates/Templates/Pure.DI.Template.ClassLibrary/ConsoleAdapter.cs index c1c7a1e16..29aba9b9c 100644 --- a/src/Pure.DI.Templates/Templates/Pure.DI.Template.ClassLibrary/ConsoleAdapter.cs +++ b/src/Pure.DI.Templates/Templates/Pure.DI.Template.ClassLibrary/ConsoleAdapter.cs @@ -1,5 +1,3 @@ -using System; - namespace _PureDIProjectName_; public class ConsoleAdapter: IInput, IOutput diff --git a/tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithAbstractionsScenario.cs b/tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithAbstractionsScenario.cs new file mode 100644 index 000000000..dcf043301 --- /dev/null +++ b/tests/Pure.DI.UsageTests/Advanced/TrackingDisposableWithAbstractionsScenario.cs @@ -0,0 +1,119 @@ +/* +$v=true +$p=101 +$d=Tracking disposable instances using pre-built classes +$h=If you want ready-made classes for tracking disposable objects in your libraries but don't want to create your own, you can add this package to your projects: +$h= +$h=[![NuGet](https://img.shields.io/nuget/v/Pure.DI.Abstractions)](https://www.nuget.org/packages/Pure.DI.Abstractions) +$h= +$h=It contains attributes like `Inject` and `Inject` that work for constructors and their arguments, methods and their arguments, properties and fields. They allow you to setup all injection parameters. +$f=This package should also be included in a project: +$f= +$f=[![NuGet](https://img.shields.io/nuget/v/Pure.DI)](https://www.nuget.org/packages/Pure.DI) +*/ + +// ReSharper disable CheckNamespace +// ReSharper disable UnusedMember.Local +// ReSharper disable UnusedParameterInPartialMethod +// ReSharper disable ArrangeTypeModifiers +// ReSharper disable ArrangeTypeMemberModifiers +// ReSharper disable UnusedMemberInSuper.Global + +namespace Pure.DI.UsageTests.Advanced.TrackingDisposableWithAbstractionsScenario; + +using Abstractions; +using Xunit; + +// { +interface IDependency +{ + bool IsDisposed { get; } +} + +class Dependency : IDependency, IDisposable +{ + public bool IsDisposed { get; private set; } + + public void Dispose() => IsDisposed = true; +} + +interface IService +{ + public IDependency Dependency { get; } + + public IDependency SingleDependency { get; } +} + +class Service( + Func> dependencyFactory, + [Tag("single")] Func> singleDependencyFactory) + : IService, IDisposable +{ + private readonly Own _dependency = dependencyFactory(); + private readonly Own _singleDependency = singleDependencyFactory(); + + public IDependency Dependency => _dependency.Value; + + public IDependency SingleDependency => _singleDependency.Value; + + public void Dispose() + { + _dependency.Dispose(); + _singleDependency.Dispose(); + } +} + +partial class Composition +{ + static void Setup() => + DI.Setup() + .Bind().To() + .Bind("single").As(Lifetime.Singleton).To() + .Bind().To() + + // Composition root + .Root("Root"); +} +// } + +public class Scenario +{ + [Fact] + public void Run() + { +// { + var composition = new Composition(); + var root1 = composition.Root; + var root2 = composition.Root; + root1.Dependency.ShouldNotBe(root2.Dependency); + root1.SingleDependency.ShouldBe(root2.SingleDependency); + + root2.Dispose(); + + // Checks that the disposable instances + // associated with root1 have been disposed of + root2.Dependency.IsDisposed.ShouldBeTrue(); + + // But the singleton is still not disposed of + root2.SingleDependency.IsDisposed.ShouldBeFalse(); + + // Checks that the disposable instances + // associated with root2 have not been disposed of + root1.Dependency.IsDisposed.ShouldBeFalse(); + root1.SingleDependency.IsDisposed.ShouldBeFalse(); + + root1.Dispose(); + + // Checks that the disposable instances + // associated with root2 have been disposed of + root1.Dependency.IsDisposed.ShouldBeTrue(); + + // But the singleton is still not disposed of + root1.SingleDependency.IsDisposed.ShouldBeFalse(); + + composition.Dispose(); + root1.SingleDependency.IsDisposed.ShouldBeTrue(); +// } + new Composition().SaveClassDiagram(); + } +} \ No newline at end of file