Understanding Autofac lifetime scopes
autofac, c#
Solution
Passing in a lifetime scope is like passing in the container itself. It resembles the Service locator (anti-) pattern and has exactly the problem you described: Dependecies become non-obvious.
One thing to ask yourself: Are you actually having problems with your memory? If not, I wouldn't bother.
Another pointer: If you have individual services that should be disposed right after usage, use a factory to create them and make your class depend on the factory instead of the service itself.
The usage scenario for lifetime scopes is a little bit different: They are used when you need a local composition root. I never had the need for something like this in a windows application, but in web applications a Session or Request can require a local composition root.
Problem
From the documentation of Autofac, I understand that it keeps a reference to every IDisposable implementor that it creates. Therefore it can lead to OutOfMemoryException. So the suggested way to resolve dependencies is by using a ILifetimeScope. Assume IService implements IDisposable. ``` class MaintenanceTask { private IService service; public MaintenanceTask(ILifetimeScope lifetimeScope) { service = lifetimeScope.Resolve<IService>(); } //...do your work } ``` But the problem with this approach is that it hides dependencies. I have to look at the code to see what that class depends on. Is there any other way to handle this in a more explicit way? More specifically, making dependencies more obvious without having to look at code? Or am I totally mistaken?