Factories should inherit creation context

Issue #256 wontfix
Honza Rameš created an issue

Factories created using IFactory<...> and registered using RegisterFactory should inherit creation context from the time the factory was created and use it when the factory is asked for the service it creates. This should allow proper functionality of services with PerResolve lifetime.

Factories created using TFunc<...> work like that already.

See attached test case. If ChildFactory = TFunc<IChild>; is uncommented and RegisterFactory commented, the test runs OK, otherwise two separate instances of TParent are created.

Comments (2)

  1. Stefan Glienke repo owner

    Explicitly registered factories don't capture the creation context. Doing so can cause potential problems with unintented extension of lifetime of instances temporarily captured by the context.

    If the implicitly created factories (TFunc<T>) will not capture the context anymore in the future to make this behavior consistent is yet to be decided for 1.3.

    To solve this particular problem the ChildFactory should be IFactory<IParent,IChild> and Self should be passed when calling. This also makes the code more pure DI compatible because the factory does not need to know the parent it got injected to and could even be a singleton because it does not have any contextual state.

  2. Log in to comment