OwnedT Class |
Namespace: Autofac.Features.OwnedInstances
The OwnedT type exposes the following members.
Name | Description | |
---|---|---|
IsDisposed |
Gets a value indicating whether the current instance has been disposed.
(Inherited from Disposable.) | |
Value |
Gets or sets the owned value.
|
Name | Description | |
---|---|---|
Dispose |
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
(Inherited from Disposable.) | |
Dispose(Boolean) |
Releases unmanaged and - optionally - managed resources.
(Overrides DisposableDispose(Boolean).) | |
DisposeAsync | (Inherited from Disposable.) | |
DisposeAsync(Boolean) |
Releases unmanaged and - optionally - managed resources asynchronously.
(Overrides DisposableDisposeAsync(Boolean).) | |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
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))).
public class D : IService, IDisposable { // ... }
public class C { IService _service; public C(Owned<IService> service) { _service = service; } void DoWork() { _service.Value.DoSomething(); } void OnFinished() { _service.Dispose(); } }