Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SynchronizedLifetimeManager no longer implements IDisposable #296

Open
ENikS opened this issue Dec 12, 2020 · 0 comments
Open

SynchronizedLifetimeManager no longer implements IDisposable #296

ENikS opened this issue Dec 12, 2020 · 0 comments
Assignees
Labels
Breaking change ❕ This change breaks public API Enhancement 🔨 Improvement of existing features
Milestone

Comments

@ENikS
Copy link
Contributor

ENikS commented Dec 12, 2020

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.

@ENikS ENikS added Enhancement 🔨 Improvement of existing features Breaking change ❕ This change breaks public API labels Dec 12, 2020
@ENikS ENikS added this to the 6.0.0 milestone Dec 12, 2020
@ENikS ENikS self-assigned this Dec 12, 2020
@github-actions github-actions bot added the release Release branch label Dec 12, 2020
@ENikS ENikS removed the release Release branch label Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking change ❕ This change breaks public API Enhancement 🔨 Improvement of existing features
Projects
None yet
Development

No branches or pull requests

1 participant