Click or drag to resize

OwnedT Class

Represents a dependency that can be released by the dependent component.
Inheritance Hierarchy
SystemObject
  Autofac.UtilDisposable
    Autofac.Features.OwnedInstancesOwnedT

Namespace:  Autofac.Features.OwnedInstances
Assembly:  Autofac (in Autofac.dll) Version: 6.0.0+39696a967e8826f7f1ebc8c1ff4523c9dd75abe0
Syntax
public class Owned<T> : Disposable

Type Parameters

T
The service provided by the dependency.

The OwnedT type exposes the following members.

Constructors
  NameDescription
Public methodOwnedT
Initializes a new instance of the OwnedT class.
Top
Properties
  NameDescription
Protected propertyIsDisposed
Gets a value indicating whether the current instance has been disposed.
(Inherited from Disposable.)
Public propertyValue
Gets or sets the owned value.
Top
Methods
  NameDescription
Public methodDispose
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
(Inherited from Disposable.)
Protected methodDispose(Boolean)
Releases unmanaged and - optionally - managed resources.
(Overrides DisposableDispose(Boolean).)
Public methodDisposeAsync (Inherited from Disposable.)
Protected methodDisposeAsync(Boolean)
Releases unmanaged and - optionally - managed resources asynchronously.
(Overrides DisposableDisposeAsync(Boolean).)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

Autofac automatically provides instances of OwnedT whenever the service T is registered.

It is not necessary for T, or the underlying component, to implement IDisposable. Disposing of the OwnedT object is the correct way to handle cleanup of the dependency, as this will dispose of any other components created indirectly as well.

When OwnedT is resolved, a new ILifetimeScope is created for the underlying T, and tagged with the service matching T, generally a TypedService. This means that shared instances can be tied to this scope by registering them as InstancePerMatchingLifetimeScope(new TypedService(typeof(T))).

Examples
The component D below is disposable and implements IService:
public class D : IService, IDisposable
{
  // ...
}
The dependent component C can dispose of the D instance whenever required by taking a dependency on OwnedT:
public class C
{
  IService _service;

  public C(Owned<IService> service)
  {
    _service = service;
  }

  void DoWork()
  {
    _service.Value.DoSomething();
  }

  void OnFinished()
  {
    _service.Dispose();
  }
}
In general, rather than depending on OwnedT directly, components will depend on System.Func<Owned<T>> in order to create and dispose of other components as required.
See Also