Owned<T> - Controlled Lifetime In Dependency Injection With Autofac

Introduction 

In general, Autofac controls the lifetime of components. It automatically disposes of all the disposable components at the end of lifetime scope. However, that may mean a component is held on to for too long. Generally speaking, an owned dependency corresponds to some unit of work performed by the dependent component.

Releasing an Owned<T> 
 
We use an owned dependency when we want to control the disposal of components on our own. The owner of an owned dependency controls the lifetime of that component. Therefore, the owner can release the component when it is no longer required. Consequently, the unused component is not held for too long, waiting for the scope to end.
 
When we dispose of a component, only that component is disposed of and not its dependencies. However, when we release an owned component, all its dependencies along with the component are disposed of. It is equally important to note, that shared dependencies (e.g., singletons) of the component will not be disposed of.
  1. public class Consumer  
  2. {  
  3.     Owned<IService> _service;  
  4.   
  5.     public Consumer(Owned<IService> service)   
  6.     { _service = service; }  
  7.   
  8.     public void ConsumeService()  
  9.     {  
  10.         // _service is used for some task  
  11.         _service.Value.DoSomething();  
  12.   
  13.         // _service is no longer needed here, so it is released  
  14.         _service.Dispose();  
  15.     }  
  16. }  
In the code above, Autofac internally creates a lifetime scope in which IService is resolved, and when we call Dispose() on it, the lifetime scope is disposed. This is to say that, disposing of IService will also dispose of its dependencies unless those dependencies are shared (e.g., singletons).

The Catch
 
In the above example, if we register our service as InstancePerLifetimeScope() and resolve it as Owned<IService> then we may not get the same instance as being used elsewhere in the same lifetime scope. Please refer to the below code (from Autofac documentation): 
  1. var builder = new ContainerBuilder();  
  2. builder.RegisterType<Consumer>().InstancePerLifetimeScope();  
  3. builder.RegisterType<IService>().InstancePerLifetimeScope();  
  4. var container = builder.Build();  
  5.   
  6. using(var scope = container.BeginLifetimeScope())  
  7. {  
  8.   //Here we resolve a IService that is InstancePerLifetimeScope();  
  9.   var serviceOne = scope.Resolve<IService>();  
  10.   serviceOne.DoSomething();  
  11.   
  12.   //This will be the same as serviceOne from above.  
  13.   var serviceTwo = scope.Resolve<IService>();  
  14.   serviceTwo.DoSomething();  
  15.   
  16.   //The IService used in Consumer will NOT be the same as the others.  
  17.   var consumer = scope.Resolve<IService>();  
  18.   consumer.ConsumeService();  
  19. }  
This is obvious because we don’t want one component to dispose of the IService out from under everything else. However, if we want to control the disposal of a component all the time, it is recommended to register that component as ExternallyOwned(), in which case, the container will never call Dispose() on it.

Related Articles 


Similar Articles