Skip to content

Factory async + pooling: generator DI/Autofac integration #190

Description

@Skymly

Problem

Phase 2 (PR #189) added async/pooled registry generation but without DI/Autofac integration. The generated {Contract}AsyncRegistry and {Contract}PooledRegistry classes only emit Create() (DirectNew). They need Create(IServiceProvider), Create(ILifetimeScope), RegisterDi, and RegisterAutofac methods when DI/Autofac integration is enabled.

Changes

Refactor

  • Extract CreateAsyncFactoryRegistryBuilderExpression and CreatePooledFactoryRegistryBuilderExpression from FactorySyntaxFactory into shared helpers that accept RegistrationResolveTarget (DirectNew/ServiceProvider/ComponentContext)

DiIntegrationSyntaxHelper (new methods)

  • CreateAsyncFactoryCreateFromServiceProviderMethod — generates Create(IServiceProvider) for async registry
  • CreatePooledFactoryCreateFromServiceProviderMethod — generates Create(IServiceProvider) for pooled registry
  • CreateAsyncFactoryRegistryBuilderExpression(contract, entries, resolveTarget) — builder chain with ServiceProvider resolution
  • CreatePooledFactoryRegistryBuilderExpression(contract, entries, poolSize, resolveTarget) — builder chain with WithPooling + ServiceProvider resolution
  • CreateAsyncFactoryRegistryInterfaceType / CreatePooledFactoryRegistryInterfaceType — type syntax helpers

AutofacIntegrationSyntaxHelper (new methods)

  • CreateAsyncFactoryCreateFromComponentContextMethod — generates Create(ILifetimeScope) for async registry
  • CreatePooledFactoryCreateFromComponentContextMethod — generates Create(ILifetimeScope) for pooled registry

FactorySyntaxFactory

  • CreateAsyncRegistryCompilationUnit: add DI members when EnableDi, Autofac members when EnableAutofac
  • CreatePooledRegistryCompilationUnit: same

Generated code structure

  • Create(IServiceProvider): uses serviceProvider.GetRequiredService() with async lambda ct => ...CreateAsync(ct) or sync () => ...
  • Create(ILifetimeScope): uses lifetimeScope.Resolve() with same lambda logic
  • RegisterDi: registers impl types as Transient, registry as Singleton via TryAdd
  • RegisterAutofac: registers impl types with InstanceSharing, registry as SingleInstance lambda

Tests (4 Verify snapshots)

  • EmitsRegisterDiWhenDiIntegrationEnabledForAsyncFactory
  • EmitsRegisterDiWhenDiIntegrationEnabledForPooledFactory
  • EmitsRegisterAutofacWhenAutofacIntegrationEnabledForAsyncFactory
  • EmitsRegisterAutofacWhenAutofacIntegrationEnabledForPooledFactory

Module

SourceGenerators

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions