using Umbraco.Cms.Core.Web; namespace Umbraco.Cms.Core; /// /// Represents a reference to an instance. /// /// /// /// A reference points to an and it may own it (when it /// is a root reference) or just reference it. A reference must be disposed after it has /// been used. Disposing does nothing if the reference is not a root reference. Otherwise, /// it disposes the and clears the /// . /// /// public class UmbracoContextReference : IDisposable { private readonly IUmbracoContextAccessor _umbracoContextAccessor; private bool _disposedValue; /// /// Initializes a new instance of the class. /// public UmbracoContextReference(IUmbracoContext umbracoContext, bool isRoot, IUmbracoContextAccessor umbracoContextAccessor) { IsRoot = isRoot; UmbracoContext = umbracoContext; _umbracoContextAccessor = umbracoContextAccessor; } /// /// Gets the . /// public IUmbracoContext UmbracoContext { get; } /// /// Gets a value indicating whether the reference is a root reference. /// public bool IsRoot { get; } public void Dispose() => Dispose(true); protected virtual void Dispose(bool disposing) { if (!_disposedValue) { if (disposing) { if (IsRoot) { UmbracoContext.Dispose(); _umbracoContextAccessor.Clear(); } } _disposedValue = true; } } }