Cleanup - Attempt
This commit is contained in:
@@ -7,160 +7,110 @@ namespace Umbraco.Core
|
||||
/// </summary>
|
||||
public static class Attempt
|
||||
{
|
||||
// note:
|
||||
// cannot rely on overloads only to differenciate between with/without status
|
||||
// in some cases it will always be ambiguous, so be explicit w/ 'WithStatus' methods
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful attempt with a result.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the attempted operation result.</typeparam>
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The successful attempt.</returns>
|
||||
public static Attempt<T> Succeed<T>(T result)
|
||||
public static Attempt<TResult> Succeed<TResult>(TResult result)
|
||||
{
|
||||
return Attempt<T>.Succeed(result);
|
||||
return Attempt<TResult>.Succeed(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful attempt with a result and a status.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
/// <typeparam name="TStatus">The type of the attempted operation status.</typeparam>
|
||||
/// <param name="status">The status of the attempt.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The successful attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> SucceedWithStatus<TResult, TStatus>(TStatus status, TResult result)
|
||||
{
|
||||
return Attempt<TResult, TStatus>.Succeed(status, result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed attempt with a result.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the attempted operation result.</typeparam>
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<T> Fail<T>(T result)
|
||||
public static Attempt<TResult> Fail<TResult>(TResult result)
|
||||
{
|
||||
return Attempt<T>.Fail(result);
|
||||
return Attempt<TResult>.Fail(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed attempt with a result and a status.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
/// <typeparam name="TStatus">The type of the attempted operation status.</typeparam>
|
||||
/// <param name="status">The status of the attempt.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> FailWithStatus<TResult, TStatus>(TStatus status, TResult result)
|
||||
{
|
||||
return Attempt<TResult, TStatus>.Fail(status, result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed attempt with a result and an exception.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the attempted operation result.</typeparam>
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <param name="exception">The exception causing the failure of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<T> Fail<T>(T result, Exception exception)
|
||||
public static Attempt<TResult> Fail<TResult>(TResult result, Exception exception)
|
||||
{
|
||||
return Attempt<T>.Fail(result, exception);
|
||||
return Attempt<TResult>.Fail(result, exception);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed attempt with a result, an exception and a status.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
/// <typeparam name="TStatus">The type of the attempted operation status.</typeparam>
|
||||
/// <param name="status">The status of the attempt.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <param name="exception">The exception causing the failure of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> FailWithStatus<TResult, TStatus>(TStatus status, TResult result, Exception exception)
|
||||
{
|
||||
return Attempt<TResult, TStatus>.Fail(status, result, exception);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful or a failed attempt, with a result.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the attempted operation result.</typeparam>
|
||||
/// <param name="success">A value indicating whether the attempt is successful.</param>
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
/// <param name="condition">A value indicating whether the attempt is successful.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The attempt.</returns>
|
||||
public static Attempt<T> If<T>(bool success, T result)
|
||||
public static Attempt<TResult> If<TResult>(bool condition, TResult result)
|
||||
{
|
||||
return Attempt<T>.SucceedIf(success, result);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Executes an attempt function, with callbacks.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the attempted operation result.</typeparam>
|
||||
/// <param name="attempt">The attempt returned by the attempt function.</param>
|
||||
/// <param name="onSuccess">An action to execute in case the attempt succeeds.</param>
|
||||
/// <param name="onFail">An action to execute in case the attempt fails.</param>
|
||||
/// <returns>The outcome of the attempt.</returns>
|
||||
/// <remarks>Runs <paramref name="onSuccess"/> or <paramref name="onFail"/> depending on the
|
||||
/// whether the attempt function reports a success or a failure.</remarks>
|
||||
public static Outcome Try<T>(Attempt<T> attempt, Action<T> onSuccess, Action<Exception> onFail = null)
|
||||
{
|
||||
if (attempt.Success)
|
||||
{
|
||||
onSuccess(attempt.Result);
|
||||
return Outcome.Success;
|
||||
}
|
||||
|
||||
if (onFail != null)
|
||||
{
|
||||
onFail(attempt.Exception);
|
||||
}
|
||||
|
||||
return Outcome.Failure;
|
||||
return Attempt<TResult>.If(condition, result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents the outcome of an attempt.
|
||||
/// Creates a successful or a failed attempt, with a result.
|
||||
/// </summary>
|
||||
/// <remarks>Can be a success or a failure, and allows for attempts chaining.</remarks>
|
||||
public struct Outcome
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
/// <typeparam name="TStatus">The type of the attempted operation status.</typeparam>
|
||||
/// <param name="condition">A value indicating whether the attempt is successful.</param>
|
||||
/// <param name="succStatus">The status of the successful attempt.</param>
|
||||
/// <param name="failStatus">The status of the failed attempt.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> IfWithStatus<TResult, TStatus>(bool condition, TStatus succStatus, TStatus failStatus, TResult result)
|
||||
{
|
||||
private readonly bool _success;
|
||||
|
||||
/// <summary>
|
||||
/// Gets an outcome representing a success.
|
||||
/// </summary>
|
||||
public static readonly Outcome Success = new Outcome(true);
|
||||
|
||||
/// <summary>
|
||||
/// Gets an outcome representing a failure.
|
||||
/// </summary>
|
||||
public static readonly Outcome Failure = new Outcome(false);
|
||||
|
||||
private Outcome(bool success)
|
||||
{
|
||||
_success = success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes another attempt function, if the previous one failed, with callbacks.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the attempted operation result.</typeparam>
|
||||
/// <param name="nextFunction">The attempt function to execute, returning an attempt.</param>
|
||||
/// <param name="onSuccess">An action to execute in case the attempt succeeds.</param>
|
||||
/// <param name="onFail">An action to execute in case the attempt fails.</param>
|
||||
/// <returns>If it executes, returns the outcome of the attempt, else returns a success outcome.</returns>
|
||||
/// <remarks>
|
||||
/// <para>Executes only if the previous attempt failed, else does not execute and return a success outcome.</para>
|
||||
/// <para>If it executes, then runs <paramref name="onSuccess"/> or <paramref name="onFail"/> depending on the
|
||||
/// whether the attempt function reports a success or a failure.</para>
|
||||
/// </remarks>
|
||||
public Outcome OnFailure<T>(Func<Attempt<T>> nextFunction, Action<T> onSuccess, Action<Exception> onFail = null)
|
||||
{
|
||||
return _success
|
||||
? Success
|
||||
: ExecuteNextFunction(nextFunction, onSuccess, onFail);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes another attempt function, if the previous one succeeded, with callbacks.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the attempted operation result.</typeparam>
|
||||
/// <param name="nextFunction">The attempt function to execute, returning an attempt.</param>
|
||||
/// <param name="onSuccess">An action to execute in case the attempt succeeds.</param>
|
||||
/// <param name="onFail">An action to execute in case the attempt fails.</param>
|
||||
/// <returns>If it executes, returns the outcome of the attempt, else returns a failed outcome.</returns>
|
||||
/// <remarks>
|
||||
/// <para>Executes only if the previous attempt succeeded, else does not execute and return a success outcome.</para>
|
||||
/// <para>If it executes, then runs <paramref name="onSuccess"/> or <paramref name="onFail"/> depending on the
|
||||
/// whether the attempt function reports a success or a failure.</para>
|
||||
/// </remarks>
|
||||
public Outcome OnSuccess<T>(Func<Attempt<T>> nextFunction, Action<T> onSuccess, Action<Exception> onFail = null)
|
||||
{
|
||||
return _success
|
||||
? ExecuteNextFunction(nextFunction, onSuccess, onFail)
|
||||
: Failure;
|
||||
}
|
||||
|
||||
private static Outcome ExecuteNextFunction<T>(Func<Attempt<T>> nextFunction, Action<T> onSuccess, Action<Exception> onFail = null)
|
||||
{
|
||||
var attempt = nextFunction();
|
||||
|
||||
if (attempt.Success)
|
||||
{
|
||||
onSuccess(attempt.Result);
|
||||
return Success;
|
||||
}
|
||||
|
||||
if (onFail != null)
|
||||
{
|
||||
onFail(attempt.Exception);
|
||||
}
|
||||
|
||||
return Failure;
|
||||
}
|
||||
return Attempt<TResult, TStatus>.If(condition, succStatus, failStatus, result);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,12 @@ namespace Umbraco.Core
|
||||
/// <summary>
|
||||
/// Represents the result of an operation attempt.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the attempted operation result.</typeparam>
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
[Serializable]
|
||||
public struct Attempt<T>
|
||||
public struct Attempt<TResult>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this <see cref="Attempt{T}"/> was successful.
|
||||
/// Gets a value indicating whether this <see cref="Attempt{TResult}"/> was successful.
|
||||
/// </summary>
|
||||
public bool Success { get; }
|
||||
|
||||
@@ -22,13 +22,13 @@ namespace Umbraco.Core
|
||||
/// <summary>
|
||||
/// Gets the attempt result.
|
||||
/// </summary>
|
||||
public T Result { get; }
|
||||
public TResult Result { get; }
|
||||
|
||||
// optimize, use a singleton failed attempt
|
||||
private static readonly Attempt<T> Failed = new Attempt<T>(false, default(T), null);
|
||||
private static readonly Attempt<TResult> Failed = new Attempt<TResult>(false, default(TResult), null);
|
||||
|
||||
// private - use Succeed() or Fail() methods to create attempts
|
||||
private Attempt(bool success, T result, Exception exception)
|
||||
private Attempt(bool success, TResult result, Exception exception)
|
||||
{
|
||||
Success = success;
|
||||
Result = result;
|
||||
@@ -39,9 +39,9 @@ namespace Umbraco.Core
|
||||
/// Creates a successful attempt.
|
||||
/// </summary>
|
||||
/// <returns>The successful attempt.</returns>
|
||||
public static Attempt<T> Succeed()
|
||||
public static Attempt<TResult> Succeed()
|
||||
{
|
||||
return new Attempt<T>(true, default(T), null);
|
||||
return new Attempt<TResult>(true, default(TResult), null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -49,16 +49,16 @@ namespace Umbraco.Core
|
||||
/// </summary>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The successful attempt.</returns>
|
||||
public static Attempt<T> Succeed(T result)
|
||||
public static Attempt<TResult> Succeed(TResult result)
|
||||
{
|
||||
return new Attempt<T>(true, result, null);
|
||||
return new Attempt<TResult>(true, result, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed attempt.
|
||||
/// </summary>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<T> Fail()
|
||||
public static Attempt<TResult> Fail()
|
||||
{
|
||||
return Failed;
|
||||
}
|
||||
@@ -68,9 +68,9 @@ namespace Umbraco.Core
|
||||
/// </summary>
|
||||
/// <param name="exception">The exception causing the failure of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<T> Fail(Exception exception)
|
||||
public static Attempt<TResult> Fail(Exception exception)
|
||||
{
|
||||
return new Attempt<T>(false, default(T), exception);
|
||||
return new Attempt<TResult>(false, default(TResult), exception);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -78,9 +78,9 @@ namespace Umbraco.Core
|
||||
/// </summary>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<T> Fail(T result)
|
||||
public static Attempt<TResult> Fail(TResult result)
|
||||
{
|
||||
return new Attempt<T>(false, result, null);
|
||||
return new Attempt<TResult>(false, result, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -89,9 +89,9 @@ namespace Umbraco.Core
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <param name="exception">The exception causing the failure of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<T> Fail(T result, Exception exception)
|
||||
public static Attempt<TResult> Fail(TResult result, Exception exception)
|
||||
{
|
||||
return new Attempt<T>(false, result, exception);
|
||||
return new Attempt<TResult>(false, result, exception);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -99,9 +99,9 @@ namespace Umbraco.Core
|
||||
/// </summary>
|
||||
/// <param name="condition">A value indicating whether the attempt is successful.</param>
|
||||
/// <returns>The attempt.</returns>
|
||||
public static Attempt<T> SucceedIf(bool condition)
|
||||
public static Attempt<TResult> If(bool condition)
|
||||
{
|
||||
return condition ? new Attempt<T>(true, default(T), null) : Failed;
|
||||
return condition ? new Attempt<TResult>(true, default(TResult), null) : Failed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -110,9 +110,9 @@ namespace Umbraco.Core
|
||||
/// <param name="condition">A value indicating whether the attempt is successful.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The attempt.</returns>
|
||||
public static Attempt<T> SucceedIf(bool condition, T result)
|
||||
public static Attempt<TResult> If(bool condition, TResult result)
|
||||
{
|
||||
return new Attempt<T>(condition, result, null);
|
||||
return new Attempt<TResult>(condition, result, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -120,7 +120,7 @@ namespace Umbraco.Core
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <returns></returns>
|
||||
public static implicit operator bool(Attempt<T> a)
|
||||
public static implicit operator bool(Attempt<TResult> a)
|
||||
{
|
||||
return a.Success;
|
||||
}
|
||||
142
src/Umbraco.Core/AttemptOfTResultTStatus.cs
Normal file
142
src/Umbraco.Core/AttemptOfTResultTStatus.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
|
||||
namespace Umbraco.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the result of an operation attempt.
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult">The type of the attempted operation result.</typeparam>
|
||||
/// <typeparam name="TStatus">The type of the attempted operation status.</typeparam>
|
||||
[Serializable]
|
||||
public struct Attempt<TResult, TStatus>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this <see cref="Attempt{TResult,TStatus}"/> was successful.
|
||||
/// </summary>
|
||||
public bool Success { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the exception associated with an unsuccessful attempt.
|
||||
/// </summary>
|
||||
public Exception Exception { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the attempt result.
|
||||
/// </summary>
|
||||
public TResult Result { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the attempt status.
|
||||
/// </summary>
|
||||
public TStatus Status { get; }
|
||||
|
||||
// private - use Succeed() or Fail() methods to create attempts
|
||||
private Attempt(bool success, TResult result, TStatus status, Exception exception)
|
||||
{
|
||||
Success = success;
|
||||
Result = result;
|
||||
Status = status;
|
||||
Exception = exception;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful attempt.
|
||||
/// </summary>
|
||||
/// <param name="status">The status of the attempt.</param>
|
||||
/// <returns>The successful attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> Succeed(TStatus status)
|
||||
{
|
||||
return new Attempt<TResult, TStatus>(true, default(TResult), status, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful attempt with a result.
|
||||
/// </summary>
|
||||
/// <param name="status">The status of the attempt.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The successful attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> Succeed(TStatus status, TResult result)
|
||||
{
|
||||
return new Attempt<TResult, TStatus>(true, result, status, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed attempt.
|
||||
/// </summary>
|
||||
/// <param name="status">The status of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> Fail(TStatus status)
|
||||
{
|
||||
return new Attempt<TResult, TStatus>(false, default(TResult), status, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed attempt with an exception.
|
||||
/// </summary>
|
||||
/// <param name="status">The status of the attempt.</param>
|
||||
/// <param name="exception">The exception causing the failure of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> Fail(TStatus status, Exception exception)
|
||||
{
|
||||
return new Attempt<TResult, TStatus>(false, default(TResult), status, exception);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed attempt with a result.
|
||||
/// </summary>
|
||||
/// <param name="status">The status of the attempt.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> Fail(TStatus status, TResult result)
|
||||
{
|
||||
return new Attempt<TResult, TStatus>(false, result, status, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a failed attempt with a result and an exception.
|
||||
/// </summary>
|
||||
/// <param name="status">The status of the attempt.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <param name="exception">The exception causing the failure of the attempt.</param>
|
||||
/// <returns>The failed attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> Fail(TStatus status, TResult result, Exception exception)
|
||||
{
|
||||
return new Attempt<TResult, TStatus>(false, result, status, exception);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful or a failed attempt.
|
||||
/// </summary>
|
||||
/// <param name="condition">A value indicating whether the attempt is successful.</param>
|
||||
/// <param name="succStatus">The status of the successful attempt.</param>
|
||||
/// <param name="failStatus">The status of the failed attempt.</param>
|
||||
/// <returns>The attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> If(bool condition, TStatus succStatus, TStatus failStatus)
|
||||
{
|
||||
return new Attempt<TResult, TStatus>(condition, default(TResult), condition ? succStatus : failStatus, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a successful or a failed attempt, with a result.
|
||||
/// </summary>
|
||||
/// <param name="condition">A value indicating whether the attempt is successful.</param>
|
||||
/// <param name="succStatus">The status of the successful attempt.</param>
|
||||
/// <param name="failStatus">The status of the failed attempt.</param>
|
||||
/// <param name="result">The result of the attempt.</param>
|
||||
/// <returns>The attempt.</returns>
|
||||
public static Attempt<TResult, TStatus> If(bool condition, TStatus succStatus, TStatus failStatus, TResult result)
|
||||
{
|
||||
return new Attempt<TResult, TStatus>(condition, result, condition ? succStatus : failStatus, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implicity operator to check if the attempt was successful without having to access the 'success' property
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <returns></returns>
|
||||
public static implicit operator bool(Attempt<TResult, TStatus> a)
|
||||
{
|
||||
return a.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,8 @@
|
||||
<Compile Include="ApplicationEventHandler.cs" />
|
||||
<Compile Include="AssemblyExtensions.cs" />
|
||||
<Compile Include="AsyncLock.cs" />
|
||||
<Compile Include="AttemptOfT.cs" />
|
||||
<Compile Include="AttemptOfTResult.cs" />
|
||||
<Compile Include="AttemptOfTResultTStatus.cs" />
|
||||
<Compile Include="Cache\CacheHelper.cs" />
|
||||
<Compile Include="Cache\CacheKeys.cs" />
|
||||
<Compile Include="Cache\CacheProviderExtensions.cs" />
|
||||
|
||||
@@ -7,36 +7,6 @@ namespace Umbraco.Tests
|
||||
[TestFixture]
|
||||
public class AttemptTests
|
||||
{
|
||||
[Test]
|
||||
public void Chained_Attempts()
|
||||
{
|
||||
Attempt.Try(Attempt.Succeed("success!"),
|
||||
s => Assert.AreEqual("success!", s),
|
||||
exception => Assert.Fail("Should have been successful."))
|
||||
|
||||
// previous one was a success so that one SHOULD NOT run
|
||||
// and report success
|
||||
.OnFailure(() => Attempt.Succeed(123),
|
||||
i => Assert.Fail("The previous attempt was successful!"),
|
||||
exception => Assert.Fail("The previous attempt was successful!"))
|
||||
|
||||
// previous one did not run, last run was a success so that one SHOULD run
|
||||
// and report failure
|
||||
.OnSuccess(() => Attempt<double>.Fail(new Exception("Failed!")),
|
||||
d => Assert.Fail("Should have failed."),
|
||||
exception => Assert.AreEqual("Failed!", exception.Message))
|
||||
|
||||
// previous one did run and was a failure so that one SHOULD NOT run
|
||||
.OnSuccess(() => Attempt.Succeed(987),
|
||||
i => Assert.Fail("The previous attempt failed!"),
|
||||
exception => Assert.Fail("The previous attempt failed!"))
|
||||
|
||||
// previous one did not run, last run was a failure so that one SHOULD run
|
||||
// then why does it run?
|
||||
.OnFailure(() => Attempt.Succeed("finished"),
|
||||
i => Assert.AreEqual("finished", i),
|
||||
exception => Assert.Fail("Should have been successful."));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AttemptIf()
|
||||
|
||||
@@ -198,7 +198,7 @@ namespace Umbraco.Web.Models
|
||||
}
|
||||
|
||||
var value = PublishedContent.GetPropertyValue(name, recurse);
|
||||
return Attempt<object>.SucceedIf(value != null, value);
|
||||
return Attempt<object>.If(value != null, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -177,38 +177,54 @@ namespace Umbraco.Web.Models.Trees
|
||||
|
||||
internal void ConvertLegacyMenuItem(IUmbracoEntity item, string nodeType, string currentSection)
|
||||
{
|
||||
//First try to get a URL/title from the legacy action,
|
||||
// if that doesn't work, try to get the legacy confirm view
|
||||
|
||||
//in some edge cases, item can be null so we'll just convert those to "-1" and "" for id and name since these edge cases don't need that.
|
||||
Attempt
|
||||
.Try(LegacyTreeDataConverter.GetUrlAndTitleFromLegacyAction(Action,
|
||||
item == null ? "-1" : item.Id.ToInvariantString(),
|
||||
nodeType,
|
||||
item == null ? "" : item.Name, currentSection),
|
||||
action => LaunchDialogUrl(action.Url, action.DialogTitle))
|
||||
.OnFailure(() => LegacyTreeDataConverter.GetLegacyConfirmView(Action, currentSection),
|
||||
view => LaunchDialogView(
|
||||
view,
|
||||
ApplicationContext.Current.Services.TextService.Localize("defaultdialogs/confirmdelete") + " '" + (item == null ? "" : item.Name) + "' ?"));
|
||||
// try to get a URL/title from the legacy action,
|
||||
// in some edge cases, item can be null so we'll just convert those to "-1" and "" for id and name since these edge cases don't need that.
|
||||
var attempt = LegacyTreeDataConverter.GetUrlAndTitleFromLegacyAction(Action,
|
||||
item == null ? "-1" : item.Id.ToInvariantString(),
|
||||
nodeType,
|
||||
item == null ? "" : item.Name, currentSection);
|
||||
if (attempt)
|
||||
{
|
||||
var action = attempt.Result;
|
||||
LaunchDialogUrl(action.Url, action.DialogTitle);
|
||||
}
|
||||
else
|
||||
{
|
||||
// if that doesn't work, try to get the legacy confirm view
|
||||
var attempt2 = LegacyTreeDataConverter.GetLegacyConfirmView(Action, currentSection);
|
||||
if (attempt2)
|
||||
{
|
||||
var view = attempt2.Result;
|
||||
var textService = ApplicationContext.Current.Services.TextService;
|
||||
LaunchDialogView(view, textService.Localize("defaultdialogs/confirmdelete") + " '" + (item == null ? "" : item.Name) + "' ?");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void ConvertLegacyFileSystemMenuItem(string path, string nodeType, string currentSection)
|
||||
{
|
||||
//First try to get a URL/title from the legacy action,
|
||||
// if that doesn't work, try to get the legacy confirm view
|
||||
|
||||
//in some edge cases, item can be null so we'll just convert those to "-1" and "" for id and name since these edge cases don't need that.
|
||||
Attempt
|
||||
.Try(LegacyTreeDataConverter.GetUrlAndTitleFromLegacyAction(Action,
|
||||
path,
|
||||
nodeType,
|
||||
path, currentSection),
|
||||
action => LaunchDialogUrl(action.Url, action.DialogTitle))
|
||||
.OnFailure(() => LegacyTreeDataConverter.GetLegacyConfirmView(Action, currentSection),
|
||||
view => LaunchDialogView(
|
||||
view,
|
||||
ApplicationContext.Current.Services.TextService.Localize("defaultdialogs/confirmdelete") + " '" + path + "' ?"));
|
||||
// try to get a URL/title from the legacy action,
|
||||
// in some edge cases, item can be null so we'll just convert those to "-1" and "" for id and name since these edge cases don't need that.
|
||||
var attempt = LegacyTreeDataConverter.GetUrlAndTitleFromLegacyAction(Action,
|
||||
path,
|
||||
nodeType,
|
||||
path, currentSection);
|
||||
if (attempt)
|
||||
{
|
||||
var action = attempt.Result;
|
||||
LaunchDialogUrl(action.Url, action.DialogTitle);
|
||||
}
|
||||
else
|
||||
{
|
||||
// if that doesn't work, try to get the legacy confirm view
|
||||
var attempt2 = LegacyTreeDataConverter.GetLegacyConfirmView(Action, currentSection);
|
||||
if (attempt2)
|
||||
{
|
||||
var view = attempt2.Result;
|
||||
var textService = ApplicationContext.Current.Services.TextService;
|
||||
LaunchDialogView(view, textService.Localize("defaultdialogs/confirmdelete") + " '" + path + "' ?");
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -127,20 +127,30 @@ namespace Umbraco.Web.Trees
|
||||
|
||||
var currentAction = t;
|
||||
|
||||
//First try to get a URL/title from the legacy action,
|
||||
// if that doesn't work, try to get the legacy confirm view
|
||||
// if that doesn't work and there's no jsAction in there already then add the legacy js method call
|
||||
Attempt
|
||||
.Try(GetUrlAndTitleFromLegacyAction(currentAction, xmlTreeNode.NodeID, xmlTreeNode.NodeType, xmlTreeNode.Text, currentSection),
|
||||
action => menuItem.LaunchDialogUrl(action.Url, action.DialogTitle))
|
||||
.OnFailure(() => GetLegacyConfirmView(currentAction, currentSection),
|
||||
view => menuItem.LaunchDialogView(
|
||||
view,
|
||||
ApplicationContext.Current.Services.TextService.Localize("defaultdialogs/confirmdelete") + " '" + xmlTreeNode.Text + "' ?"))
|
||||
.OnFailure(() => menuItem.AdditionalData.ContainsKey(MenuItem.JsActionKey)
|
||||
? Attempt.Fail(false)
|
||||
: Attempt.Succeed(true),
|
||||
b => menuItem.ExecuteLegacyJs(menuItem.Action.JsFunctionName));
|
||||
// try to get a URL/title from the legacy action
|
||||
var attempt = GetUrlAndTitleFromLegacyAction(currentAction, xmlTreeNode.NodeID, xmlTreeNode.NodeType, xmlTreeNode.Text, currentSection);
|
||||
if (attempt)
|
||||
{
|
||||
var action = attempt.Result;
|
||||
menuItem.LaunchDialogUrl(action.Url, action.DialogTitle);
|
||||
}
|
||||
else
|
||||
{
|
||||
// if that doesn't work, try to get the legacy confirm view
|
||||
var attempt2 = GetLegacyConfirmView(currentAction, currentSection);
|
||||
if (attempt2)
|
||||
{
|
||||
var view = attempt2.Result;
|
||||
var textService = ApplicationContext.Current.Services.TextService;
|
||||
menuItem.LaunchDialogView(view, textService.Localize("defaultdialogs/confirmdelete") + " '" + xmlTreeNode.Text + "' ?");
|
||||
}
|
||||
else
|
||||
{
|
||||
// if that doesn't work and there's no jsAction in there already then add the legacy js method call
|
||||
if (menuItem.AdditionalData.ContainsKey(MenuItem.JsActionKey) == false)
|
||||
menuItem.ExecuteLegacyJs(menuItem.Action.JsFunctionName);
|
||||
}
|
||||
}
|
||||
|
||||
numAdded++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user