You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Ioc/12_Container.md
+184-2Lines changed: 184 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -128,7 +128,7 @@ Configure generation with `IocContainerAttribute` properties:
128
128
> The container generated by `SourceGen.Ioc` does not resolve `IServiceCollection` by itself. If you want to integrate with `IServiceCollection`, you must ensure that the generated container should be constructed with an external `IServiceProvider`.
129
129
130
130
> [!TIP]
131
-
> For most .NET applications, the simplest way to integrate the generated container is to use it with `MS.E.DI`. Since the container implements `IServiceProviderFactory<IServiceCollection>`, you can call `new AppContainer(null).CreateServiceProvider(services)` to build a container that uses `MS.E.DI` as the fallback `IServiceProvider`.
131
+
> For most .NET applications, the simplest way to integrate the generated container is to use it with `MS.E.DI`. Since the container implements `IServiceProviderFactory<IServiceCollection>`, you can call `new AppContainer().CreateServiceProvider(services)` to build a container that uses `MS.E.DI` as the fallback `IServiceProvider`.
132
132
133
133
## Filtering by Tags
134
134
@@ -496,14 +496,196 @@ This ensures that cached `ObjectFactory` instances and property activator delega
496
496
> The hot reload handler is only generated when at least one of `IComponentActivator` or `IComponentPropertyActivator` is implemented.
497
497
> `ClearCache` only clears the caches that actually exist based on which interfaces are implemented.
498
498
499
+
## Partial Accessors (Fast-Path Service Resolution)
500
+
501
+
Declare **partial methods** or **partial properties** in the `[IocContainer]` class to get strongly-typed, fast-path accessors for registered services. The generator implements them automatically — no dictionary lookup, no boxing.
502
+
503
+
### Supported Patterns
504
+
505
+
|Pattern|Declaration|Generated Implementation|
506
+
|:---|:---|:---|
507
+
|Partial method|`public partial IMyService GetMyService();`|Calls the internal resolver directly|
When the return type is nullable, the accessor falls back to the container's `GetService` and returns `null` if the service is not registered — it never throws.
1. The return type is matched against registered service types (fully qualified name).
636
+
2. If `[IocInject]` is present, its `Key` is used for keyed service resolution.
637
+
3. If a matching internal resolver exists, it is called directly (fast-path).
638
+
4. If no internal resolver exists but `IntegrateServiceProvider = true`, fallback to `GetService` / `GetRequiredService`.
639
+
5. If no resolver exists and `IntegrateServiceProvider = false`:
640
+
-**Nullable** return type → returns `default`
641
+
-**Non-nullable** return type → `throw new InvalidOperationException(…)` (and reports `SGIOC021`)
642
+
643
+
### Naming Conflict Handling
644
+
645
+
If a user-declared partial method name collides with an internally generated resolver name (e.g., both named `GetMyService`), the generator automatically renames the internal resolver with a `_Resolve` suffix:
646
+
647
+
```csharp
648
+
// User declares:
649
+
publicpartialIMyServiceGetMyService();
650
+
651
+
// Generator renames internal resolver to avoid conflict:
0 commit comments