diff --git a/src/Umbraco.Core/Attempt.cs b/src/Umbraco.Core/Attempt.cs index c184c5670a..ed7c978b52 100644 --- a/src/Umbraco.Core/Attempt.cs +++ b/src/Umbraco.Core/Attempt.cs @@ -2,65 +2,55 @@ using System; namespace Umbraco.Core { - /// - /// Represents the result of an operation attempt - /// - /// - /// - [Serializable] - public struct Attempt - { - private readonly bool _success; - private readonly T _result; - private readonly Exception _error; + /// + /// Provides ways to create attempts. + /// + public static class Attempt + { + /// + /// 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 Succ(T result) + { + return Attempt.Succ(result); + } - /// - /// Gets a value indicating whether this represents a successful operation. - /// - /// - public bool Success - { - get { return _success; } - } + /// + /// Creates a failed attempt with a result. + /// + /// 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); + } - /// - /// Gets the error associated with an unsuccessful attempt. - /// - /// The error. - public Exception Error { get { return _error; } } + /// + /// 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); + } - /// - /// Gets the parse result. - /// - /// - public T Result - { - get { return _result; } - } - - /// - /// Represents an unsuccessful parse operation - /// - public static readonly Attempt False = new Attempt(false, default(T)); - - /// - /// 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; - } - - public Attempt(Exception error) - { - _success = false; - _result = default(T); - _error = error; - } - } + /// + /// 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.If(success, result); + } + } } \ 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..a5dce955c7 --- /dev/null +++ b/src/Umbraco.Core/Attempt{T}.cs @@ -0,0 +1,129 @@ +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 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); + + // private - use Succ() or Fail() methods to create attempts + private Attempt(bool success, T result, Exception exception) + { + _success = success; + _result = result; + _exception = exception; + } + + /// + /// Creates a successful attempt. + /// + /// The successful attempt. + public static Attempt Succ() + { + 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 Succ(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 If(bool success) + { + return success ? 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 If(bool success, T result) + { + return new Attempt(success, result, null); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Dynamics/DynamicInstanceHelper.cs b/src/Umbraco.Core/Dynamics/DynamicInstanceHelper.cs index a7bb20ef5b..b0e5981bff 100644 --- a/src/Umbraco.Core/Dynamics/DynamicInstanceHelper.cs +++ b/src/Umbraco.Core/Dynamics/DynamicInstanceHelper.cs @@ -82,7 +82,7 @@ namespace Umbraco.Core.Dynamics null, thisObject, args); - return new Attempt(true, new TryInvokeMemberResult(result, TryInvokeMemberSuccessReason.FoundProperty)); + return Attempt.Succ(new TryInvokeMemberResult(result, TryInvokeMemberSuccessReason.FoundProperty)); } catch (MissingMethodException) { @@ -97,7 +97,7 @@ namespace Umbraco.Core.Dynamics null, thisObject, args); - return new Attempt(true, new TryInvokeMemberResult(result, TryInvokeMemberSuccessReason.FoundMethod)); + return Attempt.Succ(new TryInvokeMemberResult(result, TryInvokeMemberSuccessReason.FoundMethod)); } catch (MissingMethodException) { @@ -106,13 +106,13 @@ namespace Umbraco.Core.Dynamics try { result = FindAndExecuteExtensionMethod(thisObject, args, binder.Name, findExtensionMethodsOnTypes); - return new Attempt(true, new TryInvokeMemberResult(result, TryInvokeMemberSuccessReason.FoundExtensionMethod)); + return Attempt.Succ(new TryInvokeMemberResult(result, TryInvokeMemberSuccessReason.FoundExtensionMethod)); } catch (TargetInvocationException ext) { //don't log here, we return this exception because the caller may need to do something specific when //this exception occurs. - return new Attempt(ext); + return Attempt.Fail(ext); } catch (Exception ex) { @@ -124,16 +124,16 @@ namespace Umbraco.Core.Dynamics sb.Append(t + ","); } LogHelper.Error(sb.ToString(), ex); - return new Attempt(ex); + return Attempt.Fail(ex); } } - return Attempt.False; + return Attempt.Fail(); } } catch (Exception ex) { LogHelper.Error("An unhandled exception occurred in method TryInvokeMember", ex); - return new Attempt(ex); + return Attempt.Fail(ex); } } diff --git a/src/Umbraco.Core/Dynamics/DynamicXml.cs b/src/Umbraco.Core/Dynamics/DynamicXml.cs index 0e0d14d9c9..0e02e44db9 100644 --- a/src/Umbraco.Core/Dynamics/DynamicXml.cs +++ b/src/Umbraco.Core/Dynamics/DynamicXml.cs @@ -201,7 +201,7 @@ namespace Umbraco.Core.Dynamics //this is the result of an extension method execution gone wrong so we return dynamic null if (attempt.Result.Reason == DynamicInstanceHelper.TryInvokeMemberSuccessReason.FoundExtensionMethod - && attempt.Error != null && attempt.Error is TargetInvocationException) + && attempt.Exception != null && attempt.Exception is TargetInvocationException) { result = new DynamicNull(); return true; @@ -261,7 +261,7 @@ namespace Umbraco.Core.Dynamics var attributes = xmlElement.Attributes(name).Select(attr => attr.Value).ToArray(); if (attributes.Any()) { - return new Attempt>(true, attributes); + return Attempt>.Succ(attributes); } if (!attributes.Any() && xmlElement.Name == "root" && xmlElement.Elements().Count() == 1) @@ -271,12 +271,12 @@ namespace Umbraco.Core.Dynamics if (childElements.Any()) { //we've found a match by the first child of an element called 'root' (strange, but sure) - return new Attempt>(true, childElements); + return Attempt>.Succ(childElements); } } //no deal - return Attempt>.False; + return Attempt>.Fail(); } /// @@ -293,7 +293,7 @@ namespace Umbraco.Core.Dynamics //Check if we've got any matches, if so then return true if (elements.Any()) { - return new Attempt>(true, elements); + return Attempt>.Succ(elements); } if (!elements.Any() && xmlElement.Name == "root" && xmlElement.Elements().Count() == 1) @@ -303,12 +303,12 @@ namespace Umbraco.Core.Dynamics if (childElements.Any()) { //we've found a match by the first child of an element called 'root' (strange, but sure) - return new Attempt>(true, childElements); + return Attempt>.Succ(childElements); } } //no deal - return Attempt>.False; + return Attempt>.Fail(); } private bool HandleIEnumerableXElement(IEnumerable elements, out object result) diff --git a/src/Umbraco.Core/ObjectExtensions.cs b/src/Umbraco.Core/ObjectExtensions.cs index 434f71cb18..7726af50ff 100644 --- a/src/Umbraco.Core/ObjectExtensions.cs +++ b/src/Umbraco.Core/ObjectExtensions.cs @@ -58,14 +58,14 @@ namespace Umbraco.Core try { var converted = (T) input; - return new Attempt(true, converted); + return Attempt.Succ(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.Succ((T)result.Result); } /// @@ -77,11 +77,11 @@ namespace Umbraco.Core /// public static Attempt TryConvertTo(this object input, Type destinationType) { - if (input == null) return Attempt.False; + if (input == null) return Attempt.Fail(); - if (destinationType == typeof(object)) return new Attempt(true, input); + if (destinationType == typeof(object)) return Attempt.Succ(input); - if (input.GetType() == destinationType) return new Attempt(true, input); + if (input.GetType() == destinationType) return Attempt.Succ(input); if (!destinationType.IsGenericType || destinationType.GetGenericTypeDefinition() != typeof(Nullable<>)) { @@ -94,11 +94,11 @@ namespace Umbraco.Core try { var casted = Convert.ChangeType(input, destinationType); - return new Attempt(true, casted); + return Attempt.Succ(casted); } catch (Exception e) { - return new Attempt(e); + return Attempt.Fail(e); } } } @@ -109,11 +109,11 @@ namespace Umbraco.Core try { var converted = inputConverter.ConvertTo(input, destinationType); - return new Attempt(true, converted); + return Attempt.Succ(converted); } catch (Exception e) { - return new Attempt(e); + return Attempt.Fail(e); } } @@ -125,11 +125,11 @@ namespace Umbraco.Core try { var converted = boolConverter.ConvertFrom(input); - return new Attempt(true, converted); + return Attempt.Succ(converted); } catch (Exception e) { - return new Attempt(e); + return Attempt.Fail(e); } } } @@ -140,11 +140,11 @@ namespace Umbraco.Core try { var converted = outputConverter.ConvertFrom(input); - return new Attempt(true, converted); + return Attempt.Succ(converted); } catch (Exception e) { - return new Attempt(e); + return Attempt.Fail(e); } } @@ -154,15 +154,15 @@ namespace Umbraco.Core try { var casted = Convert.ChangeType(input, destinationType); - return new Attempt(true, casted); + return Attempt.Succ(casted); } catch (Exception e) { - return new Attempt(e); + return Attempt.Fail(e); } } - return Attempt.False; + return Attempt.Fail(); } internal static void CheckThrowObjectDisposed(this IDisposable disposable, bool isDisposed, string objectname) @@ -351,11 +351,11 @@ namespace Umbraco.Core try { var output = value.ToXmlString(type); - return new Attempt(true, output); + return Attempt.Succ(output); } catch (NotSupportedException ex) { - return new Attempt(ex); + return Attempt.Fail(ex); } } diff --git a/src/Umbraco.Core/Persistence/Mappers/MappingResolver.cs b/src/Umbraco.Core/Persistence/Mappers/MappingResolver.cs index cb5d7c95a1..8c576f1e63 100644 --- a/src/Umbraco.Core/Persistence/Mappers/MappingResolver.cs +++ b/src/Umbraco.Core/Persistence/Mappers/MappingResolver.cs @@ -67,19 +67,19 @@ namespace Umbraco.Core.Persistence.Mappers if (mapper == null) { - return Attempt.False; + return Attempt.Fail(); } try { var instance = Activator.CreateInstance(mapper) as BaseMapper; return instance != null - ? new Attempt(true, instance) - : Attempt.False; + ? Attempt.Succ(instance) + : Attempt.Fail(); } catch (Exception ex) { LogHelper.Error(typeof(MappingResolver), "Could not instantiate mapper of type " + mapper, ex); - return new Attempt(ex); + return Attempt.Fail(ex); } } diff --git a/src/Umbraco.Core/Persistence/Repositories/RepositoryBase.cs b/src/Umbraco.Core/Persistence/Repositories/RepositoryBase.cs index 3967aef5e6..b9f20cd706 100644 --- a/src/Umbraco.Core/Persistence/Repositories/RepositoryBase.cs +++ b/src/Umbraco.Core/Persistence/Repositories/RepositoryBase.cs @@ -117,9 +117,9 @@ namespace Umbraco.Core.Persistence.Repositories var rEntity = _cache.GetById(typeof(TEntity), key); if (rEntity != null) { - return new Attempt(true, (TEntity) rEntity); + return Attempt.Succ((TEntity) rEntity); } - return Attempt.False; + return Attempt.Fail(); } protected abstract IEnumerable PerformGetAll(params TId[] ids); diff --git a/src/Umbraco.Core/PluginManager.cs b/src/Umbraco.Core/PluginManager.cs index f798c2a454..1db052396a 100644 --- a/src/Umbraco.Core/PluginManager.cs +++ b/src/Umbraco.Core/PluginManager.cs @@ -242,7 +242,7 @@ namespace Umbraco.Core { var filePath = GetPluginListFilePath(); if (!File.Exists(filePath)) - return Attempt>.False; + return Attempt>.Fail(); try { @@ -262,7 +262,7 @@ namespace Umbraco.Core if (xml.Root == null) - return Attempt>.False; + return Attempt>.Fail(); var typeElement = xml.Root.Elements() .SingleOrDefault(x => @@ -272,18 +272,16 @@ namespace Umbraco.Core //return false but specify this exception type so we can detect it if (typeElement == null) - return new Attempt>(new CachedPluginNotFoundInFile()); + return Attempt>.Fail(new CachedPluginNotFoundInFile()); //return success - return new Attempt>( - true, - typeElement.Elements("add") + return Attempt.Succ(typeElement.Elements("add") .Select(x => (string)x.Attribute("type"))); } catch (Exception ex) { //if the file is corrupted, etc... return false - return new Attempt>(ex); + return Attempt>.Fail(ex); } } @@ -621,7 +619,7 @@ namespace Umbraco.Core //here we need to identify if the CachedPluginNotFoundInFile was the exception, if it was then we need to re-scan //in some cases the plugin will not have been scanned for on application startup, but the assemblies haven't changed //so in this instance there will never be a result. - if (fileCacheResult.Error != null && fileCacheResult.Error is CachedPluginNotFoundInFile) + if (fileCacheResult.Exception != null && fileCacheResult.Exception is CachedPluginNotFoundInFile) { //we don't have a cache for this so proceed to look them up by scanning LoadViaScanningAndUpdateCacheFile(typeList, resolutionType, finder); diff --git a/src/Umbraco.Core/Profiling/WebProfiler.cs b/src/Umbraco.Core/Profiling/WebProfiler.cs index 3a1974279e..6dae4cde0c 100644 --- a/src/Umbraco.Core/Profiling/WebProfiler.cs +++ b/src/Umbraco.Core/Profiling/WebProfiler.cs @@ -143,16 +143,16 @@ namespace Umbraco.Core.Profiling private Attempt TryGetRequest(object sender) { var app = sender as HttpApplication; - if (app == null) return Attempt.False; + if (app == null) return Attempt.Fail(); try { var req = app.Request; - return new Attempt(true, new HttpRequestWrapper(req)); + return Attempt.Succ(new HttpRequestWrapper(req)); } catch (HttpException ex) { - return new Attempt(ex); + return Attempt.Fail(ex); } } } diff --git a/src/Umbraco.Core/PropertyEditors/TinyMcePropertyEditorValueConverter.cs b/src/Umbraco.Core/PropertyEditors/TinyMcePropertyEditorValueConverter.cs index fec80ddc85..e6acbf36e0 100644 --- a/src/Umbraco.Core/PropertyEditors/TinyMcePropertyEditorValueConverter.cs +++ b/src/Umbraco.Core/PropertyEditors/TinyMcePropertyEditorValueConverter.cs @@ -20,7 +20,7 @@ namespace Umbraco.Core.PropertyEditors /// public virtual Attempt ConvertPropertyValue(object value) { - return new Attempt(true, new HtmlString(value.ToString())); + return Attempt.Succ(new HtmlString(value.ToString())); } } } \ No newline at end of file diff --git a/src/Umbraco.Core/PublishedContentHelper.cs b/src/Umbraco.Core/PublishedContentHelper.cs index 86513ede02..db8d2dbbe4 100644 --- a/src/Umbraco.Core/PublishedContentHelper.cs +++ b/src/Umbraco.Core/PublishedContentHelper.cs @@ -93,7 +93,7 @@ namespace Umbraco.Core /// internal static Attempt ConvertPropertyValue(object currentValue, Guid dataType, string docTypeAlias, string propertyTypeAlias) { - if (currentValue == null) return Attempt.False; + if (currentValue == null) return Attempt.Fail(); //First lets check all registered converters for this data type. var converters = PropertyEditorValueConvertersResolver.Current.Converters @@ -105,7 +105,7 @@ namespace Umbraco.Core .Select(p => p.ConvertPropertyValue(currentValue)) .Where(converted => converted.Success)) { - return new Attempt(true, converted.Result); + return Attempt.Succ(converted.Result); } //if none of the converters worked, then we'll process this from what we know @@ -118,17 +118,17 @@ namespace Umbraco.Core decimal dResult; if (decimal.TryParse(sResult, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.CurrentCulture, out dResult)) { - return new Attempt(true, dResult); + return Attempt.Succ(dResult); } } //process string booleans as booleans if (sResult.InvariantEquals("true")) { - return new Attempt(true, true); + return Attempt.Succ(true); } if (sResult.InvariantEquals("false")) { - return new Attempt(true, false); + return Attempt.Succ(false); } //a really rough check to see if this may be valid xml @@ -147,16 +147,16 @@ namespace Umbraco.Core if (!UmbracoSettings.NotDynamicXmlDocumentElements.Any( tag => string.Equals(tag, documentElement, StringComparison.CurrentCultureIgnoreCase))) { - return new Attempt(true, new DynamicXml(e)); + return Attempt.Succ(new DynamicXml(e)); } - return Attempt.False; + return Attempt.Fail(); } catch (Exception) { - return Attempt.False; + return Attempt.Fail(); } } - return Attempt.False; + return Attempt.Fail(); } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Publishing/PublishingStrategy.cs b/src/Umbraco.Core/Publishing/PublishingStrategy.cs index 5f874a00b0..d115bb1a10 100644 --- a/src/Umbraco.Core/Publishing/PublishingStrategy.cs +++ b/src/Umbraco.Core/Publishing/PublishingStrategy.cs @@ -25,7 +25,7 @@ namespace Umbraco.Core.Publishing { LogHelper.Info( string.Format("Content '{0}' with Id '{1}' will not be published, the event was cancelled.", content.Name, content.Id)); - return new Attempt(false, new PublishStatus(content, PublishStatusType.FailedCancelledByEvent)); + return Attempt.Fail(new PublishStatus(content, PublishStatusType.FailedCancelledByEvent)); } @@ -35,7 +35,7 @@ namespace Umbraco.Core.Publishing LogHelper.Info( string.Format("Content '{0}' with Id '{1}' has expired and could not be published.", content.Name, content.Id)); - return new Attempt(false, new PublishStatus(content, PublishStatusType.FailedHasExpired)); + return Attempt.Fail(new PublishStatus(content, PublishStatusType.FailedHasExpired)); } //Check if the Content is Awaiting Release to verify that it can in fact be published @@ -44,7 +44,7 @@ namespace Umbraco.Core.Publishing LogHelper.Info( string.Format("Content '{0}' with Id '{1}' is awaiting release and could not be published.", content.Name, content.Id)); - return new Attempt(false, new PublishStatus(content, PublishStatusType.FailedAwaitingRelease)); + return Attempt.Fail(new PublishStatus(content, PublishStatusType.FailedAwaitingRelease)); } //Check if the Content is Trashed to verify that it can in fact be published @@ -53,7 +53,7 @@ namespace Umbraco.Core.Publishing LogHelper.Info( string.Format("Content '{0}' with Id '{1}' is trashed and could not be published.", content.Name, content.Id)); - return new Attempt(false, new PublishStatus(content, PublishStatusType.FailedIsTrashed)); + return Attempt.Fail(new PublishStatus(content, PublishStatusType.FailedIsTrashed)); } content.ChangePublishedState(PublishedState.Published); @@ -62,7 +62,7 @@ namespace Umbraco.Core.Publishing string.Format("Content '{0}' with Id '{1}' has been published.", content.Name, content.Id)); - return new Attempt(true, new PublishStatus(content)); + return Attempt.Succ(new PublishStatus(content)); } /// @@ -125,7 +125,7 @@ namespace Umbraco.Core.Publishing //We're going to populate the statuses with all content that is already published because below we are only going to iterate over // content that is not published. We'll set the status to "AlreadyPublished" statuses.AddRange(fetchedContent.Where(x => x.Published) - .Select(x => new Attempt(true, new PublishStatus(x, PublishStatusType.SuccessAlreadyPublished)))); + .Select(x => Attempt.Succ(new PublishStatus(x, PublishStatusType.SuccessAlreadyPublished)))); int? firstLevel = null; @@ -169,7 +169,7 @@ namespace Umbraco.Core.Publishing //the publishing has been cancelled. LogHelper.Info( string.Format("Content '{0}' with Id '{1}' will not be published, the event was cancelled.", item.Name, item.Id)); - statuses.Add(new Attempt(false, new PublishStatus(item, PublishStatusType.FailedCancelledByEvent))); + statuses.Add(Attempt.Fail(new PublishStatus(item, PublishStatusType.FailedCancelledByEvent))); //Does this document apply to our rule to cancel it's children being published? CheckCancellingOfChildPublishing(item, parentsIdsCancelled, includeUnpublishedDocuments); @@ -183,7 +183,7 @@ namespace Umbraco.Core.Publishing LogHelper.Info( string.Format("Content '{0}' with Id '{1}' will not be published because some of it's content is not passing validation rules.", item.Name, item.Id)); - statuses.Add(new Attempt(false, new PublishStatus(item, PublishStatusType.FailedContentInvalid))); + statuses.Add(Attempt.Fail(new PublishStatus(item, PublishStatusType.FailedContentInvalid))); //Does this document apply to our rule to cancel it's children being published? CheckCancellingOfChildPublishing(item, parentsIdsCancelled, includeUnpublishedDocuments); @@ -197,7 +197,7 @@ namespace Umbraco.Core.Publishing LogHelper.Info( string.Format("Content '{0}' with Id '{1}' has expired and could not be published.", item.Name, item.Id)); - statuses.Add(new Attempt(false, new PublishStatus(item, PublishStatusType.FailedHasExpired))); + statuses.Add(Attempt.Fail(new PublishStatus(item, PublishStatusType.FailedHasExpired))); //Does this document apply to our rule to cancel it's children being published? CheckCancellingOfChildPublishing(item, parentsIdsCancelled, includeUnpublishedDocuments); @@ -211,7 +211,7 @@ namespace Umbraco.Core.Publishing LogHelper.Info( string.Format("Content '{0}' with Id '{1}' is awaiting release and could not be published.", item.Name, item.Id)); - statuses.Add(new Attempt(false, new PublishStatus(item, PublishStatusType.FailedAwaitingRelease))); + statuses.Add(Attempt.Fail(new PublishStatus(item, PublishStatusType.FailedAwaitingRelease))); //Does this document apply to our rule to cancel it's children being published? CheckCancellingOfChildPublishing(item, parentsIdsCancelled, includeUnpublishedDocuments); @@ -225,7 +225,7 @@ namespace Umbraco.Core.Publishing LogHelper.Info( string.Format("Content '{0}' with Id '{1}' is trashed and could not be published.", item.Name, item.Id)); - statuses.Add(new Attempt(false, new PublishStatus(item, PublishStatusType.FailedIsTrashed))); + statuses.Add(Attempt.Fail(new PublishStatus(item, PublishStatusType.FailedIsTrashed))); //Does this document apply to our rule to cancel it's children being published? CheckCancellingOfChildPublishing(item, parentsIdsCancelled, includeUnpublishedDocuments); @@ -239,7 +239,7 @@ namespace Umbraco.Core.Publishing string.Format("Content '{0}' with Id '{1}' has been published.", item.Name, item.Id)); - statuses.Add(new Attempt(true, new PublishStatus(item))); + statuses.Add(Attempt.Succ(new PublishStatus(item))); } } @@ -349,7 +349,7 @@ namespace Umbraco.Core.Publishing { LogHelper.Info( string.Format("Content '{0}' with Id '{1}' will not be published, the event was cancelled.", item.Name, item.Id)); - result.Add(new Attempt(false, new PublishStatus(item, PublishStatusType.FailedCancelledByEvent))); + result.Add(Attempt.Fail(new PublishStatus(item, PublishStatusType.FailedCancelledByEvent))); continue; } @@ -370,7 +370,7 @@ namespace Umbraco.Core.Publishing string.Format("Content '{0}' with Id '{1}' has been unpublished.", item.Name, item.Id)); - result.Add(new Attempt(true, new PublishStatus(item))); + result.Add(Attempt.Succ(new PublishStatus(item))); } return result; diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index d4bf0b546d..76ee81f369 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -1452,7 +1452,7 @@ namespace Umbraco.Core.Services string.Format( "Content '{0}' with Id '{1}' could not be published because its parent or one of its ancestors is not published.", content.Name, content.Id)); - result.Add(new Attempt(false, new PublishStatus(content, PublishStatusType.FailedPathNotPublished))); + result.Add(Attempt.Fail(new PublishStatus(content, PublishStatusType.FailedPathNotPublished))); return result; } @@ -1463,7 +1463,7 @@ namespace Umbraco.Core.Services string.Format("Content '{0}' with Id '{1}' could not be published because of invalid properties.", content.Name, content.Id)); result.Add( - new Attempt(false, + Attempt.Fail( new PublishStatus(content, PublishStatusType.FailedContentInvalid) { InvalidProperties = ((ContentBase) content).LastInvalidProperties @@ -1569,7 +1569,7 @@ namespace Umbraco.Core.Services { if (Saving.IsRaisedEventCancelled(new SaveEventArgs(content), this)) { - return new Attempt(false, new PublishStatus(content, PublishStatusType.FailedCancelledByEvent)); + return Attempt.Fail(new PublishStatus(content, PublishStatusType.FailedCancelledByEvent)); } } @@ -1639,7 +1639,7 @@ namespace Umbraco.Core.Services Audit.Add(AuditTypes.Publish, "Save and Publish performed by user", userId, content.Id); - return new Attempt(publishStatus.StatusType == PublishStatusType.Success, publishStatus); + return Attempt.If(publishStatus.StatusType == PublishStatusType.Success, publishStatus); } } diff --git a/src/Umbraco.Core/TypeHelper.cs b/src/Umbraco.Core/TypeHelper.cs index 26dd2a8402..71e558e55c 100644 --- a/src/Umbraco.Core/TypeHelper.cs +++ b/src/Umbraco.Core/TypeHelper.cs @@ -95,11 +95,11 @@ namespace Umbraco.Core { if (types.Length == 0) { - return Attempt.False; + return Attempt.Fail(); } if (types.Length == 1) { - return new Attempt(true, types[0]); + return Attempt.Succ(types[0]); } foreach (var curr in types) @@ -112,11 +112,11 @@ namespace Umbraco.Core //if this type is the base for all others if (isBase) { - return new Attempt(true, curr); + return Attempt.Succ(curr); } } - return Attempt.False; + return Attempt.Fail(); } /// diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 43b8891c92..2cc6a8fbd4 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -105,7 +105,7 @@ - + @@ -137,6 +137,7 @@ + diff --git a/src/Umbraco.Web/Models/DynamicPublishedContent.cs b/src/Umbraco.Web/Models/DynamicPublishedContent.cs index 3ff0732f99..57f5981385 100644 --- a/src/Umbraco.Web/Models/DynamicPublishedContent.cs +++ b/src/Umbraco.Web/Models/DynamicPublishedContent.cs @@ -131,7 +131,7 @@ namespace Umbraco.Web.Models //this is the result of an extension method execution gone wrong so we return dynamic null if (attempt.Result.Reason == DynamicInstanceHelper.TryInvokeMemberSuccessReason.FoundExtensionMethod - && attempt.Error != null && attempt.Error is TargetInvocationException) + && attempt.Exception != null && attempt.Exception is TargetInvocationException) { result = new DynamicNull(); return true; @@ -150,7 +150,7 @@ namespace Umbraco.Web.Models { if (binder.Name.InvariantEquals("ChildrenAsList") || binder.Name.InvariantEquals("Children")) { - return new Attempt(true, Children); + return Attempt.Succ(Children); } if (binder.Name.InvariantEquals("parentId")) @@ -160,9 +160,9 @@ namespace Umbraco.Web.Models { throw new InvalidOperationException(string.Format("The node {0} does not have a parent", Id)); } - return new Attempt(true, parent.Id); + return Attempt.Succ(parent.Id); } - return Attempt.False; + return Attempt.Fail(); } /// @@ -182,10 +182,10 @@ namespace Umbraco.Web.Models .ToArray(); if (filteredTypeChildren.Any()) { - return new Attempt(true, + return Attempt.Succ( new DynamicPublishedContentList(filteredTypeChildren.Select(x => new DynamicPublishedContent(x)))); } - return Attempt.False; + return Attempt.Fail(); } /// @@ -201,9 +201,7 @@ namespace Umbraco.Web.Models ? reflectedProperty.Value : null; - return result == null - ? Attempt.False - : new Attempt(true, result); + return Attempt.If(result != null, result); } /// @@ -225,7 +223,7 @@ namespace Umbraco.Web.Models if (userProperty == null) { - return Attempt.False; + return Attempt.Fail(); } var result = userProperty.Value; @@ -249,7 +247,7 @@ namespace Umbraco.Web.Models result = converted.Result; } - return new Attempt(true, result); + return Attempt.Succ(result); } @@ -384,7 +382,7 @@ namespace Umbraco.Web.Models { try { - return new Attempt(true, + return Attempt.Succ( content.GetType().InvokeMember(memberAlias, System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance | @@ -395,7 +393,7 @@ namespace Umbraco.Web.Models } catch (MissingMethodException ex) { - return new Attempt(ex); + return Attempt.Fail(ex); } }; diff --git a/src/Umbraco.Web/Models/DynamicPublishedContentList.cs b/src/Umbraco.Web/Models/DynamicPublishedContentList.cs index 3b323b004a..d937a31bbe 100644 --- a/src/Umbraco.Web/Models/DynamicPublishedContentList.cs +++ b/src/Umbraco.Web/Models/DynamicPublishedContentList.cs @@ -273,7 +273,7 @@ namespace Umbraco.Web.Models //this is the result of an extension method execution gone wrong so we return dynamic null if (attempt.Result.Reason == DynamicInstanceHelper.TryInvokeMemberSuccessReason.FoundExtensionMethod - && attempt.Error != null && attempt.Error is TargetInvocationException) + && attempt.Exception != null && attempt.Exception is TargetInvocationException) { result = new DynamicNull(); return true; diff --git a/src/Umbraco.Web/Mvc/SurfaceController.cs b/src/Umbraco.Web/Mvc/SurfaceController.cs index 23caa16619..0011718b3c 100644 --- a/src/Umbraco.Web/Mvc/SurfaceController.cs +++ b/src/Umbraco.Web/Mvc/SurfaceController.cs @@ -81,7 +81,7 @@ namespace Umbraco.Web.Mvc var routeDefAttempt = TryGetRouteDefinitionFromAncestorViewContexts(); if (!routeDefAttempt.Success) { - throw routeDefAttempt.Error; + throw routeDefAttempt.Exception; } var routeDef = routeDefAttempt.Result; @@ -105,7 +105,7 @@ namespace Umbraco.Web.Mvc var currentRouteData = currentContext.RouteData; if (currentRouteData.DataTokens.ContainsKey("umbraco-route-def")) { - return new Attempt(true, (RouteDefinition) currentRouteData.DataTokens["umbraco-route-def"]); + return Attempt.Succ((RouteDefinition) currentRouteData.DataTokens["umbraco-route-def"]); } if (currentContext.IsChildAction) { @@ -118,7 +118,7 @@ namespace Umbraco.Web.Mvc currentContext = null; } } - return new Attempt( + return Attempt.Fail( new InvalidOperationException("Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request")); } diff --git a/src/Umbraco.Web/PropertyEditors/RteMacroRenderingPropertyEditorValueConverter.cs b/src/Umbraco.Web/PropertyEditors/RteMacroRenderingPropertyEditorValueConverter.cs index 6f435c3d19..2448ce0578 100644 --- a/src/Umbraco.Web/PropertyEditors/RteMacroRenderingPropertyEditorValueConverter.cs +++ b/src/Umbraco.Web/PropertyEditors/RteMacroRenderingPropertyEditorValueConverter.cs @@ -35,7 +35,7 @@ namespace Umbraco.Web.PropertyEditors //needs to be explicitly casted to Dictionary macroAttributes.ConvertTo(x => (string)x, x => (object)x)).ToString())); - return new Attempt(true, new HtmlString(sb.ToString())); + return Attempt.Succ(new HtmlString(sb.ToString())); } } } \ No newline at end of file diff --git a/src/Umbraco.Web/UmbracoModule.cs b/src/Umbraco.Web/UmbracoModule.cs index a1482c08e1..79f029c4a7 100644 --- a/src/Umbraco.Web/UmbracoModule.cs +++ b/src/Umbraco.Web/UmbracoModule.cs @@ -262,7 +262,7 @@ namespace Umbraco.Web reason = EnsureRoutableOutcome.NoContent; } - return new Attempt(reason == EnsureRoutableOutcome.IsRoutable, reason); + return Attempt.If(reason == EnsureRoutableOutcome.IsRoutable, reason); } /// diff --git a/src/Umbraco.Web/WebApi/UmbracoApiController.cs b/src/Umbraco.Web/WebApi/UmbracoApiController.cs index b8ba3f9a7b..ca334d6371 100644 --- a/src/Umbraco.Web/WebApi/UmbracoApiController.cs +++ b/src/Umbraco.Web/WebApi/UmbracoApiController.cs @@ -35,15 +35,15 @@ namespace Umbraco.Web.WebApi var httpContext = context as HttpContextBase; if (httpContext != null) { - return new Attempt(true, httpContext); + return Attempt.Succ(httpContext); } } if (HttpContext.Current != null) { - return new Attempt(true, new HttpContextWrapper(HttpContext.Current)); + return Attempt.Succ(new HttpContextWrapper(HttpContext.Current)); } - return Attempt.False; + return Attempt.Fail(); } /// diff --git a/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNode.cs b/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNode.cs index d804f97899..5ffacf8bfb 100644 --- a/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNode.cs +++ b/src/umbraco.MacroEngines/RazorDynamicNode/DynamicNode.cs @@ -649,7 +649,7 @@ namespace umbraco.MacroEngines { try { - return new Attempt(true, + return Attempt.Succ( n.GetType().InvokeMember(memberAlias, System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance | @@ -660,7 +660,7 @@ namespace umbraco.MacroEngines } catch (MissingMethodException ex) { - return new Attempt(ex); + return Attempt.Fail(ex); } }; diff --git a/src/umbraco.cms/businesslogic/web/Document.cs b/src/umbraco.cms/businesslogic/web/Document.cs index 66e6db5080..072d830e27 100644 --- a/src/umbraco.cms/businesslogic/web/Document.cs +++ b/src/umbraco.cms/businesslogic/web/Document.cs @@ -900,10 +900,7 @@ namespace umbraco.cms.businesslogic.web [Obsolete("Don't use! Only used internally to support the legacy events", false)] internal Attempt SaveAndPublish(int userId) { - var result = new Attempt(false, - new PublishStatus(Content, - PublishStatusType - .FailedCancelledByEvent)); + var result = Attempt.Fail(new PublishStatus(Content, PublishStatusType.FailedCancelledByEvent)); foreach (var property in GenericProperties) { Content.SetValue(property.PropertyType.Alias, property.Value); @@ -1028,10 +1025,10 @@ namespace umbraco.cms.businesslogic.web return result; } - return Attempt.False; + return Attempt.Fail(); } - return Attempt.False; + return Attempt.Fail(); } ///