Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow decorators to take relationship types #1000

Closed
YairHalberstadt opened this issue Jul 9, 2019 · 2 comments
Closed

Allow decorators to take relationship types #1000

YairHalberstadt opened this issue Jul 9, 2019 · 2 comments

Comments

@YairHalberstadt
Copy link

YairHalberstadt commented Jul 9, 2019

I have an implementation of an interface which cannot be resolved too early in the application lifetime, and so must be resolved lazily.

Because this is used by hundreds of components, rather than use Lazy, I've created a lazy wrapper over the interface, and would like to register it.

public class LazyMyInterface : IMyInterface
{
     private readonly Lazy<IMyInterface> _myInterface;

     public LazyInterface(Lazy<IMyInterface> myInterface) => _myInterface = myInterface;

     private IMyInterface MyInterface => _myInterface.Value;

     public void MyMethod => MyInterface.MyMethod();
}

However I can't see any way to register it. Registering it directly will lead to an infinite loop, as it will replace the implementation of IMyInterface.

Registering it as a decorator causes the eager evaluation of MyInterface.

I think this could be resolved by allowing decorators to take relationship types of the interface.

@nblumhardt
Copy link
Member

Hi! This is some tricky territory - I suspect you may be able to do this by implementing and plugging in your own IRegistrationSource, but you'll need to check out https://github.com/autofac/Autofac/blob/develop/src/Autofac/Features/LazyDependencies/LazyRegistrationSource.cs and tinker with it to catch any gotchas. Good luck!

@alexmg
Copy link
Member

alexmg commented Sep 2, 2019

Closing as a duplicate of #967 which has a lot more discussion. It certainly is tricky territory.

@alexmg alexmg closed this as completed Sep 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants