2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2017-07-12 20:40:09 +02:00
|
|
|
|
|
2021-02-15 11:41:12 +01:00
|
|
|
|
namespace Umbraco.Cms.Core.Scoping
|
2017-07-12 20:40:09 +02:00
|
|
|
|
{
|
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>
|
2017-07-12 20:40:09 +02:00
|
|
|
|
public abstract class ScopeContextualBase : IDisposable
|
|
|
|
|
|
{
|
2019-03-14 19:48:44 +01:00
|
|
|
|
private bool _scoped;
|
2017-07-12 20:40:09 +02:00
|
|
|
|
|
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>
|
2017-07-14 12:51:57 +02:00
|
|
|
|
public static T Get<T>(IScopeProvider scopeProvider, string key, Func<bool, T> ctor)
|
2017-07-12 20:40:09 +02:00
|
|
|
|
where T : ScopeContextualBase
|
|
|
|
|
|
{
|
2019-02-22 15:27:22 +01:00
|
|
|
|
// no scope context = create a non-scoped object
|
2017-07-12 20:40:09 +02:00
|
|
|
|
var scopeContext = scopeProvider.Context;
|
|
|
|
|
|
if (scopeContext == null)
|
2017-07-14 12:51:57 +02:00
|
|
|
|
return ctor(false);
|
2017-07-12 20:40:09 +02:00
|
|
|
|
|
2019-02-22 15:27:22 +01:00
|
|
|
|
// create & enlist the scoped object
|
2017-07-12 20:40:09 +02:00
|
|
|
|
var w = scopeContext.Enlist("ScopeContextualBase_" + key,
|
2017-07-14 12:51:57 +02:00
|
|
|
|
() => ctor(true),
|
2017-07-12 20:40:09 +02:00
|
|
|
|
(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>
|
2017-07-12 20:40:09 +02:00
|
|
|
|
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>
|
2017-07-12 20:40:09 +02:00
|
|
|
|
public abstract void Release(bool completed);
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|