Skip to content

Func<> and custom delegate injection #17

@dmytroett

Description

@dmytroett

Summary

Implement factory and delegate injection mechanisms in AttributedDI to allow resolving factories/delegates for services (e.g. Func<T> and custom delegate types).

Motivation

  • Improve flexibility when constructing services that require runtime parameters.
  • Avoid boilerplate factory types in user code while keeping DI-friendly patterns.
  • Provide a clear, documented approach that works well with the library's AOP-style registration.

Proposed APIs / Examples

  • Direct Func resolution

    • Allow consumers to request Func<IMyService> which returns a new IMyService instance when invoked.
  • Delegate generation

    • Support generating delegates that map to service constructors, e.g.:
      public delegate IMyService Create(string param, int val);
      // Container resolves Create -> delegate that constructs/resolves IMyService using provided args

Considerations / Constraints

  • Func approach may require generating resolver code per service or inspecting constructors to decide generation; avoid forcing attributes on services if possible.
  • Delegate generation must be compatible with common mocking/proxy frameworks (Moq, Castle) to avoid false anti-patterns; determine what limitations exist with dynamic proxying of delegate-returning registrations.
  • Decide lifecycle semantics (transient vs scoped/singleton) for factories and produced instances and document them.

Acceptance Criteria

  • Support resolving Func<T> from the container.
  • Support resolving custom delegates that accept parameters and return service instances.
  • Document limitations and compatibility notes with Moq / Castle Proxy and typical testing frameworks.
  • Add clear usage examples in README or docs.
  • Add tests that verify delegate resolution works and respects service lifetimes.

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