diff --git a/src/Umbraco.Core/ApplicationContext.cs b/src/Umbraco.Core/ApplicationContext.cs
index b90f3f3e94..45901847eb 100644
--- a/src/Umbraco.Core/ApplicationContext.cs
+++ b/src/Umbraco.Core/ApplicationContext.cs
@@ -100,7 +100,7 @@ namespace Umbraco.Core
//
public bool IsConfigured
{
- // fixme - let's do this for the time being
+ // fixme - we should not do this - ok for now
get
{
return Configured;
diff --git a/src/Umbraco.Core/Attempt.cs b/src/Umbraco.Core/Attempt.cs
index 6054056645..ca2bbe35fe 100644
--- a/src/Umbraco.Core/Attempt.cs
+++ b/src/Umbraco.Core/Attempt.cs
@@ -2,135 +2,165 @@ using System;
namespace Umbraco.Core
{
- public struct AttemptOutcome
+ ///
+ /// Provides ways to create attempts.
+ ///
+ public static class Attempt
{
- private readonly bool _success;
-
- public AttemptOutcome(bool success)
+ ///
+ /// Creates a successful attempt with a result.
+ ///
+ /// The type of the attempted operation result.
+ /// The result of the attempt.
+ /// The successful attempt.
+ public static Attempt Succeed(T result)
{
- _success = success;
+ return Attempt.Succeed(result);
}
- public AttemptOutcome IfFailed(Func> nextAttempt, Action onSuccess, Action onFail = null)
- {
- if (_success == false)
- {
- return ExecuteNextAttempt(nextAttempt, onSuccess, onFail);
- }
-
- //return a successful outcome since the last one was successful, this allows the next AttemptOutcome chained to
- // continue properly.
- return new AttemptOutcome(true);
- }
-
- public AttemptOutcome IfSuccessful(Func> nextAttempt, Action onSuccess, Action onFail = null)
- {
- if (_success)
- {
- return ExecuteNextAttempt(nextAttempt, onSuccess, onFail);
- }
- //return a failed outcome since the last one was not successful, this allows the next AttemptOutcome chained to
- // continue properly.
- return new AttemptOutcome(false);
- }
-
- private AttemptOutcome ExecuteNextAttempt(Func> nextAttempt, Action onSuccess, Action onFail = null)
- {
- var attempt = nextAttempt();
- if (attempt.Success)
- {
- onSuccess(attempt.Result);
- return new AttemptOutcome(true);
- }
-
- if (onFail != null)
- {
- onFail(attempt.Error);
- }
- return new AttemptOutcome(false);
- }
- }
-
- ///
- /// Represents the result of an operation attempt
- ///
- ///
- ///
- [Serializable]
- public struct Attempt
- {
- private readonly bool _success;
- private readonly T _result;
- private readonly Exception _error;
-
- ///
- /// Gets a value indicating whether this represents a successful operation.
- ///
- ///
- public bool Success
- {
- get { return _success; }
- }
-
- ///
- /// Gets the error associated with an unsuccessful attempt.
- ///
- /// The error.
- public Exception Error { get { return _error; } }
-
- ///
- /// Gets the parse result.
- ///
- ///
- public T Result
- {
- get { return _result; }
- }
-
///
- /// Perform the attempt with callbacks
+ /// Creates a failed attempt with a result.
///
- ///
- ///
- ///
- public static AttemptOutcome Try(Attempt attempt, Action onSuccess, Action onFail = null)
+ /// The type of the attempted operation result.
+ /// The result of the attempt.
+ /// The failed attempt.
+ public static Attempt Fail(T result)
+ {
+ return Attempt.Fail(result);
+ }
+
+ ///
+ /// Creates a failed attempt with a result and an exception.
+ ///
+ /// The type of the attempted operation result.
+ /// The result of the attempt.
+ /// The exception causing the failure of the attempt.
+ /// The failed attempt.
+ public static Attempt Fail(T result, Exception exception)
+ {
+ return Attempt.Fail(result, exception);
+ }
+
+ ///
+ /// Creates a successful or a failed attempt, with a result.
+ ///
+ /// The type of the attempted operation result.
+ /// A value indicating whether the attempt is successful.
+ /// The result of the attempt.
+ /// The attempt.
+ public static Attempt If(bool success, T result)
+ {
+ return Attempt.SucceedIf(success, result);
+ }
+
+
+ ///
+ /// Executes an attempt function, with callbacks.
+ ///
+ /// The type of the attempted operation result.
+ /// The attempt returned by the attempt function.
+ /// An action to execute in case the attempt succeeds.
+ /// An action to execute in case the attempt fails.
+ /// The outcome of the attempt.
+ /// Runs or depending on the
+ /// whether the attempt function reports a success or a failure.
+ public static Outcome Try(Attempt attempt, Action onSuccess, Action onFail = null)
{
if (attempt.Success)
{
onSuccess(attempt.Result);
- return new AttemptOutcome(true);
+ return Outcome.Success;
}
if (onFail != null)
{
- onFail(attempt.Error);
+ onFail(attempt.Exception);
}
- return new AttemptOutcome(false);
+
+ return Outcome.Failure;
}
- ///
- /// Represents an unsuccessful parse operation
- ///
- public static readonly Attempt False = new Attempt(false, default(T));
+ ///
+ /// Represents the outcome of an attempt.
+ ///
+ /// Can be a success or a failure, and allows for attempts chaining.
+ public struct Outcome
+ {
+ private readonly bool _success;
- ///
- /// Initializes a new instance of the struct.
- ///
- /// If set to true this tuple represents a successful parse result.
- /// The parse result.
- ///
- public Attempt(bool success, T result)
- {
- _success = success;
- _result = result;
- _error = null;
- }
+ ///
+ /// Gets an outcome representing a success.
+ ///
+ public static readonly Outcome Success = new Outcome(true);
- public Attempt(Exception error)
- {
- _success = false;
- _result = default(T);
- _error = error;
- }
- }
+ ///
+ /// Gets an outcome representing a failure.
+ ///
+ public static readonly Outcome Failure = new Outcome(false);
+
+ private Outcome(bool success)
+ {
+ _success = success;
+ }
+
+ ///
+ /// Executes another attempt function, if the previous one failed, with callbacks.
+ ///
+ /// The type of the attempted operation result.
+ /// The attempt function to execute, returning an attempt.
+ /// An action to execute in case the attempt succeeds.
+ /// An action to execute in case the attempt fails.
+ /// If it executes, returns the outcome of the attempt, else returns a success outcome.
+ ///
+ /// Executes only if the previous attempt failed, else does not execute and return a success outcome.
+ /// If it executes, then runs or depending on the
+ /// whether the attempt function reports a success or a failure.
+ ///
+ public Outcome OnFailure(Func> nextFunction, Action onSuccess, Action onFail = null)
+ {
+ return _success
+ ? Success
+ : ExecuteNextFunction(nextFunction, onSuccess, onFail);
+ }
+
+ ///
+ /// Executes another attempt function, if the previous one succeeded, with callbacks.
+ ///
+ /// The type of the attempted operation result.
+ /// The attempt function to execute, returning an attempt.
+ /// An action to execute in case the attempt succeeds.
+ /// An action to execute in case the attempt fails.
+ /// If it executes, returns the outcome of the attempt, else returns a failed outcome.
+ ///
+ /// Executes only if the previous attempt succeeded, else does not execute and return a success outcome.
+ /// If it executes, then runs or depending on the
+ /// whether the attempt function reports a success or a failure.
+ ///
+ public Outcome OnSuccess(Func> nextFunction, Action onSuccess, Action onFail = null)
+ {
+ return _success
+ ? ExecuteNextFunction(nextFunction, onSuccess, onFail)
+ : Failure;
+ }
+
+ private static Outcome ExecuteNextFunction(Func> nextFunction, Action onSuccess, Action onFail = null)
+ {
+ var attempt = nextFunction();
+
+ if (attempt.Success)
+ {
+ onSuccess(attempt.Result);
+ return Success;
+ }
+
+ if (onFail != null)
+ {
+ onFail(attempt.Exception);
+ }
+
+ return Failure;
+ }
+ }
+
+ }
}
\ No newline at end of file
diff --git a/src/Umbraco.Core/Attempt{T}.cs b/src/Umbraco.Core/Attempt{T}.cs
new file mode 100644
index 0000000000..4a348247d4
--- /dev/null
+++ b/src/Umbraco.Core/Attempt{T}.cs
@@ -0,0 +1,164 @@
+using System;
+using Umbraco.Core.Dynamics;
+
+namespace Umbraco.Core
+{
+ ///
+ /// Represents the result of an operation attempt.
+ ///
+ /// The type of the attempted operation result.
+ [Serializable]
+ public struct Attempt
+ {
+ private readonly bool _success;
+ private readonly T _result;
+ private readonly Exception _exception;
+
+ ///
+ /// Gets a value indicating whether this was successful.
+ ///
+ public bool Success
+ {
+ get { return _success; }
+ }
+
+ ///
+ /// Gets the exception associated with an unsuccessful attempt.
+ ///
+ public Exception Exception { get { return _exception; } }
+
+ ///
+ /// Gets the exception associated with an unsuccessful attempt.
+ ///
+ /// Keep it for backward compatibility sake.
+ [Obsolete(".Error is obsolete, you should use .Exception instead.", false)]
+ public Exception Error { get { return _exception; } }
+
+ ///
+ /// Gets the attempt result.
+ ///
+ public T Result
+ {
+ get { return _result; }
+ }
+
+ // optimize, use a singleton failed attempt
+ private static readonly Attempt Failed = new Attempt(false, default(T), null);
+
+ ///
+ /// Represents an unsuccessful attempt.
+ ///
+ /// Keep it for backward compatibility sake.
+ [Obsolete(".Failed is obsolete, you should use Attempt.Fail() instead.", false)]
+ public static readonly Attempt False = Failed;
+
+ // private - use Succeed() or Fail() methods to create attempts
+ private Attempt(bool success, T result, Exception exception)
+ {
+ _success = success;
+ _result = result;
+ _exception = exception;
+ }
+
+ ///
+ /// Initialize a new instance of the struct with a result.
+ ///
+ /// A value indicating whether the attempt is successful.
+ /// The result of the attempt.
+ /// Keep it for backward compatibility sake.
+ [Obsolete("Attempt ctors are obsolete, you should use Attempt.Succeed(), Attempt.Fail() or Attempt.If() instead.", false)]
+ public Attempt(bool success, T result)
+ : this(success, result, null)
+ { }
+
+ ///
+ /// Initialize a new instance of the struct representing a failed attempt, with an exception.
+ ///
+ /// The exception causing the failure of the attempt.
+ /// Keep it for backward compatibility sake.
+ [Obsolete("Attempt ctors are obsolete, you should use Attempt.Succeed(), Attempt.Fail() or Attempt.If() instead.", false)]
+ public Attempt(Exception exception)
+ : this(false, default(T), exception)
+ { }
+
+ ///
+ /// Creates a successful attempt.
+ ///
+ /// The successful attempt.
+ public static Attempt Succeed()
+ {
+ return new Attempt(true, default(T), null);
+ }
+
+ ///
+ /// Creates a successful attempt with a result.
+ ///
+ /// The result of the attempt.
+ /// The successful attempt.
+ public static Attempt Succeed(T result)
+ {
+ return new Attempt(true, result, null);
+ }
+
+ ///
+ /// Creates a failed attempt.
+ ///
+ /// The failed attempt.
+ public static Attempt Fail()
+ {
+ return Failed;
+ }
+
+ ///
+ /// Creates a failed attempt with an exception.
+ ///
+ /// The exception causing the failure of the attempt.
+ /// The failed attempt.
+ public static Attempt Fail(Exception exception)
+ {
+ return new Attempt(false, default(T), exception);
+ }
+
+ ///
+ /// Creates a failed attempt with a result.
+ ///
+ /// The result of the attempt.
+ /// The failed attempt.
+ public static Attempt Fail(T result)
+ {
+ return new Attempt(false, result, null);
+ }
+
+ ///
+ /// Creates a failed attempt with a result and an exception.
+ ///
+ /// The result of the attempt.
+ /// The exception causing the failure of the attempt.
+ /// The failed attempt.
+ public static Attempt Fail(T result, Exception exception)
+ {
+ return new Attempt(false, result, exception);
+ }
+
+ ///
+ /// Creates a successful or a failed attempt.
+ ///
+ /// A value indicating whether the attempt is successful.
+ /// The attempt.
+ public static Attempt SucceedIf(bool condition)
+ {
+ return condition ? new Attempt(true, default(T), null) : Failed;
+ }
+
+ ///
+ /// Creates a successful or a failed attempt, with a result.
+ ///
+ /// A value indicating whether the attempt is successful.
+ /// The result of the attempt.
+ /// The attempt.
+ public static Attempt SucceedIf(bool condition, T result)
+ {
+ return new Attempt(condition, result, null);
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Umbraco.Core/Configuration/UmbracoSettings/ObjectExtensions.cs b/src/Umbraco.Core/Configuration/UmbracoSettings/ObjectExtensions.cs
index fce45a0cd3..614e23ea13 100644
--- a/src/Umbraco.Core/Configuration/UmbracoSettings/ObjectExtensions.cs
+++ b/src/Umbraco.Core/Configuration/UmbracoSettings/ObjectExtensions.cs
@@ -50,14 +50,14 @@ namespace Umbraco.Core.Configuration.UmbracoSettings
try
{
var converted = (T)input;
- return new Attempt(true, converted);
+ return Attempt.Succeed(converted);
}
catch (Exception e)
{
- return new Attempt(e);
+ return Attempt.Fail(e);
}
}
- return !result.Success ? Attempt.False : new Attempt(true, (T)result.Result);
+ return !result.Success ? Attempt.Fail() : Attempt.Succeed((T)result.Result);
}
///
@@ -69,22 +69,22 @@ namespace Umbraco.Core.Configuration.UmbracoSettings
///
public static Attempt