Skip to content

SynchronizedLifetimeManager no longer implements IDisposable #296

Open

Description

@ENikS

IDisposable implementation

Unity v4 and v5 declared SynchronizedLifetimeManager as follows:

SynchronizedLifetimeManager : LifetimeManager, IDisposable
{
...
}

Problem

Every lifetime manager implementing IDisposable must be added to the container's scope to be disposed when that scope goes away or being disposed manually. In cases when object held by the manager is not IDisposable the manager would be held by the lifetime container for no good reason.

This implementation was mandated by the original API where LefetimeManager.SetValue(object value) had only one parameter for the value itself.

Solution

Unity v5 introduced new API: LefetimeManager.SetValue(object value, ILifetimeContainer scope).
This new API allowed to add objects to the scope on demand and in case of ContainerControlledTransientManager multiple objects when resolved.

With addition of this new API the SynchronizedLifetimeManager was still derived from IDisposable and was held in scope collection. This added unnecessary iterations during disposal as well as disposed some objects more than once.

Unity v6 removes IDisposable from SynchronizedLifetimeManager and deprecated ILifetimeContainer interface so these issues are no longer contribute to overhead.

SetValue API

Starting with Unity v6, the new API is as follows:

object? TryGetValue(ICollection<IDisposable> scope)

object? GetValue(ICollection<IDisposable> scope)

void SetValue(object? newValue, ICollection<IDisposable> scope)

Impact

Custom managers derived from SynchronizedLifetimeManager must no longer rely on framework to be added to scope collection. If custom manager must implement IDisposable and be disposed during scope destruction, the manager must implement IDisposable and add itself to the scope during SetValue(...) call.

Activity

  1. added this to the 6.0.0 milestone on Dec 12, 2020
  2. self-assigned this
    on Dec 12, 2020
  3. added a commit that references this issue on Dec 12, 2020
    6c486d6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions