Files
Umbraco-CMS/src/Umbraco.Infrastructure/Scoping/ScopeContextualBase.cs

63 lines
2.1 KiB
C#
Raw Normal View History

2017-07-20 11:21:28 +02:00
using System;
namespace Umbraco.Cms.Core.Scoping
{
2019-02-22 15:27:22 +01:00
/// <summary>
/// Provides a base class for scope contextual objects.
/// </summary>
/// <remarks>
/// <para>A scope contextual object is enlisted in the current scope context,
/// if any, and released when the context exists. It must be used in a 'using'
/// block, and will be released when disposed, if not part of a scope.</para>
/// </remarks>
public abstract class ScopeContextualBase : IDisposable
{
2019-03-14 19:48:44 +01:00
private bool _scoped;
2019-02-22 15:27:22 +01:00
/// <summary>
/// Gets a contextual object.
/// </summary>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="scopeProvider">A scope provider.</param>
/// <param name="key">A context key for the object.</param>
/// <param name="ctor">A function producing the contextual object.</param>
/// <returns>The contextual object.</returns>
/// <remarks>
/// <para></para>
/// </remarks>
public static T Get<T>(IScopeProvider scopeProvider, string key, Func<bool, T> ctor)
where T : ScopeContextualBase
{
2019-02-22 15:27:22 +01:00
// no scope context = create a non-scoped object
var scopeContext = scopeProvider.Context;
if (scopeContext == null)
return ctor(false);
2019-02-22 15:27:22 +01:00
// create & enlist the scoped object
var w = scopeContext.Enlist("ScopeContextualBase_" + key,
() => ctor(true),
(completed, item) => { item.Release(completed); });
w._scoped = true;
return w;
}
2019-02-22 15:27:22 +01:00
/// <inheritdoc />
/// <remarks>
/// <para>If not scoped, then this releases the contextual object.</para>
/// </remarks>
public void Dispose()
{
if (_scoped == false)
Release(true);
}
2019-02-22 15:27:22 +01:00
/// <summary>
/// Releases the contextual object.
/// </summary>
/// <param name="completed">A value indicating whether the scoped operation completed.</param>
public abstract void Release(bool completed);
}
2017-07-20 11:21:28 +02:00
}