Skip to content

Commit 51dc5da

Browse files
Update README
1 parent 5cd1221 commit 51dc5da

71 files changed

Lines changed: 740 additions & 409 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,7 @@ DI.Setup("Composition")
11651165
| [DisableAutoBindingImplementationTypeNameWildcard](#disableautobindingimplementationtypenamewildcard-hint) | Wildcard | | * |
11661166
| [DisableAutoBindingLifetimeRegularExpression](#disableautobindinglifetimeregularexpression-hint) | Regular expression | | .+ |
11671167
| [DisableAutoBindingLifetimeWildcard](#disableautobindinglifetimewildcard-hint) | Wildcard | | * |
1168+
| [LightweightAnonymousRoot](#lightweightanonymousroot-hint) | _On_ or _Off_ | | _On_ |
11681169

11691170
The list of hints will be gradually expanded to meet the needs and desires for fine-tuning code generation. Please feel free to add your ideas.
11701171

@@ -1822,6 +1823,16 @@ DI.Setup(nameof(Composition))
18221823
.Root<Service>("MyService");
18231824
```
18241825

1826+
### LightweightAnonymousRoot Hint
1827+
1828+
Controls whether anonymous composition roots are generated in a lightweight manner. When _On_ (default), anonymous roots are optimized for minimal overhead. Set to _Off_ if you need full debugging capabilities for anonymous roots.
1829+
1830+
```c#
1831+
// LightweightAnonymousRoot = Off
1832+
DI.Setup("Composition")
1833+
.Bind<IService>().To<Service>();
1834+
```
1835+
18251836
</details>
18261837

18271838
<details>

readme/accumulators.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ partial class Composition
105105
get
106106
{
107107
var perBlockTelemetryRegistry1 = new TelemetryRegistry();
108+
var perBlockSqlDataSource5 = new SqlDataSource();
108109
if (_singletonNetworkDataSource53 is null)
109110
lock (_lock)
110111
if (_singletonNetworkDataSource53 is null)
@@ -122,7 +123,7 @@ partial class Composition
122123
perBlockTelemetryRegistry1.Add(transientSqlDataSource3);
123124
}
124125

125-
var transientDashboard2 = new Dashboard(transientSqlDataSource3, _singletonNetworkDataSource53, new SqlDataSource());
126+
var transientDashboard2 = new Dashboard(transientSqlDataSource3, _singletonNetworkDataSource53, perBlockSqlDataSource5);
126127
lock (_lock)
127128
{
128129
perBlockTelemetryRegistry1.Add(transientDashboard2);

readme/async-disposable-scope.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ partial class Composition: IDisposable, IAsyncDisposable
173173
[MethodImpl(MethodImplOptions.AggressiveInlining)]
174174
get
175175
{
176-
Func<Session> transientFunc524 = new Func<Session>(
176+
Func<Session> perBlockFunc542 = new Func<Session>(
177177
[MethodImpl(MethodImplOptions.AggressiveInlining)]
178178
() =>
179179
{
180180
return new Session(this);
181181
});
182-
return new Program(transientFunc524);
182+
return new Program(perBlockFunc542);
183183
}
184184
}
185185

@@ -274,8 +274,8 @@ classDiagram
274274
Composition ..> Service : IService SessionRoot
275275
Service o-- "Scoped" Dependency : IDependency
276276
Program o-- "PerBlock" FuncᐸSessionᐳ : FuncᐸSessionᐳ
277-
Session *-- Composition : Composition
278277
FuncᐸSessionᐳ *-- Session : Session
278+
Session *-- Composition : Composition
279279
namespace Pure.DI.UsageTests.Lifetimes.AsyncDisposableScopeScenario {
280280
class Composition {
281281
<<partial>>

readme/async-enumerable.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ partial class Composition
8989
get
9090
{
9191
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92-
async IAsyncEnumerable<IHealthCheck> EnumerationOf_transientIAsyncEnumerable345()
92+
async IAsyncEnumerable<IHealthCheck> EnumerationOf_transientIAsyncEnumerable349()
9393
{
9494
yield return new MemoryCheck();
9595
yield return new ExternalServiceCheck();
9696
await Task.CompletedTask;
9797
}
9898

99-
return new HealthService(EnumerationOf_transientIAsyncEnumerable345());
99+
return new HealthService(EnumerationOf_transientIAsyncEnumerable349());
100100
}
101101
}
102102
}

readme/async-root.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,24 @@ partial class Composition
7878
{
7979
Task<IBackupService> transientTask194;
8080
// Injects an instance factory
81-
Func<IBackupService> transientFunc195 = new Func<IBackupService>(
81+
Func<IBackupService> perBlockFunc195 = new Func<IBackupService>(
8282
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8383
() =>
8484
{
8585
return new BackupService(new FileStore());
8686
});
87-
Func<IBackupService> localFactory = transientFunc195;
87+
Func<IBackupService> localFactory = perBlockFunc195;
8888
// Injects a task factory creating and scheduling task objects
89-
TaskFactory<IBackupService> transientTaskFactory196;
89+
TaskFactory<IBackupService> perBlockTaskFactory196;
9090
CancellationToken localCancellationToken = cancellationToken;
9191
TaskCreationOptions transientTaskCreationOptions200 = TaskCreationOptions.None;
9292
TaskCreationOptions localTaskCreationOptions = transientTaskCreationOptions200;
9393
TaskContinuationOptions transientTaskContinuationOptions201 = TaskContinuationOptions.None;
9494
TaskContinuationOptions localTaskContinuationOptions = transientTaskContinuationOptions201;
9595
TaskScheduler transientTaskScheduler202 = TaskScheduler.Default;
9696
TaskScheduler localTaskScheduler = transientTaskScheduler202;
97-
transientTaskFactory196 = new TaskFactory<IBackupService>(localCancellationToken, localTaskCreationOptions, localTaskContinuationOptions, localTaskScheduler);
98-
TaskFactory<IBackupService> localTaskFactory = transientTaskFactory196;
97+
perBlockTaskFactory196 = new TaskFactory<IBackupService>(localCancellationToken, localTaskCreationOptions, localTaskContinuationOptions, localTaskScheduler);
98+
TaskFactory<IBackupService> localTaskFactory = perBlockTaskFactory196;
9999
// Creates and starts a task using the instance factory
100100
transientTask194 = localTaskFactory.StartNew(localFactory);
101101
return transientTask194;

readme/auto-scoped.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,18 @@ partial class Composition
154154
[MethodImpl(MethodImplOptions.AggressiveInlining)]
155155
get
156156
{
157-
Func<IListeningSession> transientFunc532 = new Func<IListeningSession>(
157+
Func<IListeningSession> perBlockFunc550 = new Func<IListeningSession>(
158158
[MethodImpl(MethodImplOptions.AggressiveInlining)]
159159
() =>
160160
{
161-
IListeningSession transientIListeningSession533;
161+
IListeningSession transientIListeningSession551;
162162
Composition localParentScope = this;
163163
// Create a child scope so scoped services (PlaybackQueue) are unique per session.
164164
var localScope = new Composition(localParentScope);
165-
transientIListeningSession533 = localScope.Session;
166-
return transientIListeningSession533;
165+
transientIListeningSession551 = localScope.Session;
166+
return transientIListeningSession551;
167167
});
168-
return new MusicApp(transientFunc532);
168+
return new MusicApp(perBlockFunc550);
169169
}
170170
}
171171

readme/automapper.md

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ partial class Composition: IDisposable
188188
private object[] _disposables;
189189
private int _disposeIndex;
190190

191+
private Program? _singletonProgram58;
191192
private Microsoft.Extensions.Logging.ILogger? _singletonILogger55;
192193
private Func<Student, Person>? _singletonFunc59;
193194
private AutoMapper.Mapper? _singletonMapper56;
@@ -214,50 +215,55 @@ partial class Composition: IDisposable
214215
[MethodImpl(MethodImplOptions.AggressiveInlining)]
215216
get
216217
{
217-
if (_root._singletonFunc59 is null)
218+
if (_root._singletonProgram58 is null)
218219
lock (_lock)
219-
if (_root._singletonFunc59 is null)
220+
if (_root._singletonProgram58 is null)
220221
{
221-
_root._singletonFunc59 = source =>
222-
{
223-
if (_root._singletonMapper56 is null)
224-
{
225-
EnsureLoggerFactoryExists();
226-
LoggerFactory localLoggerFactory = _root._singletonLoggerFactory54;
227-
// Create the mapping configuration
228-
var localConfiguration = new MapperConfiguration(cfg =>
222+
if (_root._singletonFunc59 is null)
223+
lock (_lock)
224+
if (_root._singletonFunc59 is null)
229225
{
230-
cfg.CreateMap<Student, Person>();
231-
}, localLoggerFactory);
232-
localConfiguration.CompileMappings();
233-
// Create the mapper
234-
_root._singletonMapper56 = new Mapper(localConfiguration);
235-
}
236-
237-
AutoMapper.IMapper localMapper = _root._singletonMapper56;
238-
// source -> target
239-
Person localTarget = localMapper.Map<Student, Person>(source);
240-
// Building-up a mapped value with dependencies
241-
if (_root._singletonPersonFormatter53 is null)
242-
{
243-
_root._singletonPersonFormatter53 = new PersonFormatter();
244-
}
245-
246-
localTarget.Formatter = _root._singletonPersonFormatter53;
247-
return localTarget;
248-
};
249-
}
226+
_root._singletonFunc59 = source =>
227+
{
228+
if (_root._singletonMapper56 is null)
229+
{
230+
EnsureLoggerFactoryExists();
231+
LoggerFactory localLoggerFactory = _root._singletonLoggerFactory54;
232+
// Create the mapping configuration
233+
var localConfiguration = new MapperConfiguration(cfg =>
234+
{
235+
cfg.CreateMap<Student, Person>();
236+
}, localLoggerFactory);
237+
localConfiguration.CompileMappings();
238+
// Create the mapper
239+
_root._singletonMapper56 = new Mapper(localConfiguration);
240+
}
241+
242+
AutoMapper.IMapper localMapper = _root._singletonMapper56;
243+
// source -> target
244+
Person localTarget = localMapper.Map<Student, Person>(source);
245+
// Building-up a mapped value with dependencies
246+
if (_root._singletonPersonFormatter53 is null)
247+
{
248+
_root._singletonPersonFormatter53 = new PersonFormatter();
249+
}
250250

251-
if (_root._singletonILogger55 is null)
252-
lock (_lock)
253-
if (_root._singletonILogger55 is null)
254-
{
255-
EnsureLoggerFactoryExists();
256-
LoggerFactory localLoggerFactory1 = _root._singletonLoggerFactory54;
257-
_root._singletonILogger55 = localLoggerFactory1.CreateLogger("info");
251+
localTarget.Formatter = _root._singletonPersonFormatter53;
252+
return localTarget;
253+
};
254+
}
255+
256+
if (_root._singletonILogger55 is null)
257+
{
258+
EnsureLoggerFactoryExists();
259+
LoggerFactory localLoggerFactory1 = _root._singletonLoggerFactory54;
260+
_root._singletonILogger55 = localLoggerFactory1.CreateLogger("info");
261+
}
262+
263+
_root._singletonProgram58 = new Program(_root._singletonILogger55, new StudentService(_root._singletonFunc59));
258264
}
259265

260-
return new Program(_root._singletonILogger55, new StudentService(_root._singletonFunc59));
266+
return _root._singletonProgram58;
261267
[MethodImpl(MethodImplOptions.AggressiveInlining)]
262268
void EnsureLoggerFactoryExists()
263269
{
@@ -282,6 +288,7 @@ partial class Composition: IDisposable
282288
_disposeIndex = 0;
283289
disposables = _disposables;
284290
_disposables = new object[1];
291+
_singletonProgram58 = null;
285292
_singletonILogger55 = null;
286293
_singletonFunc59 = null;
287294
_singletonMapper56 = null;

readme/bind-attribute.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ partial class Composition
109109
EnsureDeviceFeatureProviderExists();
110110
DeviceFeatureProvider localInstance_1182D1277 = _singletonDeviceFeatureProvider51;
111111
transientIGps209 = localInstance_1182D1277.Gps;
112-
Func<ICamera> transientFunc210 = new Func<ICamera>(
112+
Func<ICamera> perBlockFunc210 = new Func<ICamera>(
113113
[MethodImpl(MethodImplOptions.AggressiveInlining)]
114114
() =>
115115
{
@@ -119,7 +119,7 @@ partial class Composition
119119
transientICamera212 = localInstance_1182D1278.Camera;
120120
return transientICamera212;
121121
});
122-
return new PhotoService(transientIGps209, transientFunc210);
122+
return new PhotoService(transientIGps209, perBlockFunc210);
123123
[MethodImpl(MethodImplOptions.AggressiveInlining)]
124124
void EnsureDeviceFeatureProviderExists()
125125
{

readme/build-up-of-an-existing-generic-object.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,16 @@ partial class Composition
106106
public IFacade<Guid> GetFacade(string userName)
107107
{
108108
if (userName is null) throw new ArgumentNullException(nameof(userName));
109-
UserContext<Guid> transientUserContext458;
109+
UserContext<Guid> transientUserContext474;
110110
// The "BuildUp" method injects dependencies into an existing object.
111111
// This is useful when the object is created externally (e.g., by a UI framework
112112
// or an ORM) or requires specific initialization before injection.
113113
UserContext<Guid> localContext = new UserContext<Guid>();
114-
Guid transientGuid460 = Guid.NewGuid();
114+
Guid transientGuid476 = Guid.NewGuid();
115115
localContext.UserName = userName;
116-
localContext.SetId(transientGuid460);
117-
transientUserContext458 = localContext;
118-
return new Facade<Guid>(transientUserContext458);
116+
localContext.SetId(transientGuid476);
117+
transientUserContext474 = localContext;
118+
return new Facade<Guid>(transientUserContext474);
119119
}
120120
}
121121
```

readme/check-for-a-root.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,30 @@ partial class Composition
116116
}
117117
}
118118

119-
private IUserRepository Root2
119+
private LightweightRoot LightRoot
120120
{
121121
[MethodImpl(MethodImplOptions.AggressiveInlining)]
122122
get
123123
{
124-
return new SqlUserRepository();
124+
Func<IUserRepository> perBlockFunc502 = new Func<IUserRepository>(
125+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
126+
() =>
127+
{
128+
return new SqlUserRepository();
129+
});
130+
return new LightweightRoot()
131+
{
132+
IUserRepository = perBlockFunc502
133+
};
134+
}
135+
}
136+
137+
private IUserRepository Root1
138+
{
139+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
140+
get
141+
{
142+
return LightRoot.IUserRepository();
125143
}
126144
}
127145

@@ -204,7 +222,7 @@ partial class Composition
204222
OnNewRoot<IUserService, UserService>(valResolver_0000, "Root", null, Lifetime.Transient);
205223
Resolver<IUserService>.Value = valResolver_0000;
206224
var valResolver_0001 = new Resolver_0001();
207-
OnNewRoot<IUserRepository, SqlUserRepository>(valResolver_0001, "Root2", "Primary", Lifetime.Transient);
225+
OnNewRoot<IUserRepository, SqlUserRepository>(valResolver_0001, "Root1", "Primary", Lifetime.Transient);
208226
Resolver<IUserRepository>.Value = valResolver_0001;
209227
_buckets = Buckets<IResolver<Composition, object>>.Create(
210228
4,
@@ -266,13 +284,19 @@ partial class Composition
266284
switch (tag)
267285
{
268286
case "Primary":
269-
return composition.Root2;
287+
return composition.Root1;
270288

271289
default:
272290
return base.ResolveByTag(composition, tag);
273291
}
274292
}
275293
}
294+
295+
#pragma warning disable CS0649
296+
private sealed class LightweightRoot: LightweightRoot
297+
{
298+
[OrdinalAttribute()] public Func<IUserRepository> IUserRepository;
299+
}
276300
}
277301
```
278302

@@ -289,12 +313,23 @@ Class diagram:
289313
classDiagram
290314
SqlUserRepository --|> IUserRepository : "Primary"
291315
UserService --|> IUserService
316+
Composition ..> LightweightRoot : LightweightRoot LightRoot69d
292317
Composition ..> UserService : IUserService Root
293318
Composition ..> SqlUserRepository : IUserRepository _
294319
UserService *-- SqlUserRepository : "Primary" IUserRepository
320+
LightweightRoot o-- "PerBlock" FuncᐸIUserRepositoryᐳ : "Primary" FuncᐸIUserRepositoryᐳ
321+
FuncᐸIUserRepositoryᐳ *-- SqlUserRepository : "Primary" IUserRepository
322+
namespace Pure.DI {
323+
class LightweightRoot {
324+
<<class>>
325+
+LightweightRoot()
326+
+FuncᐸIUserRepositoryᐳ IUserRepository
327+
}
328+
}
295329
namespace Pure.DI.UsageTests.Hints.CheckForRootScenario {
296330
class Composition {
297331
<<partial>>
332+
-LightweightRoot LightRoot69d
298333
+IUserService Root
299334
-IUserRepository _
300335
+ T ResolveᐸTᐳ()
@@ -318,5 +353,10 @@ classDiagram
318353
+IUserRepository Repository
319354
}
320355
}
356+
namespace System {
357+
class FuncᐸIUserRepositoryᐳ {
358+
<<delegate>>
359+
}
360+
}
321361
```
322362

0 commit comments

Comments
 (0)