Core.Attempt - refactor and cleanup

This commit is contained in:
Stephan
2013-09-04 17:14:06 +02:00
parent 1ba8ad5fbb
commit 85ab96f71a
23 changed files with 284 additions and 171 deletions

View File

@@ -58,14 +58,14 @@ namespace Umbraco.Core
try
{
var converted = (T) input;
return new Attempt<T>(true, converted);
return Attempt<T>.Succ(converted);
}
catch (Exception e)
{
return new Attempt<T>(e);
return Attempt<T>.Fail(e);
}
}
return !result.Success ? Attempt<T>.False : new Attempt<T>(true, (T)result.Result);
return !result.Success ? Attempt<T>.Fail() : Attempt<T>.Succ((T)result.Result);
}
/// <summary>
@@ -77,11 +77,11 @@ namespace Umbraco.Core
/// <returns></returns>
public static Attempt<object> TryConvertTo(this object input, Type destinationType)
{
if (input == null) return Attempt<object>.False;
if (input == null) return Attempt<object>.Fail();
if (destinationType == typeof(object)) return new Attempt<object>(true, input);
if (destinationType == typeof(object)) return Attempt.Succ(input);
if (input.GetType() == destinationType) return new Attempt<object>(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<object>(true, casted);
return Attempt.Succ(casted);
}
catch (Exception e)
{
return new Attempt<object>(e);
return Attempt<object>.Fail(e);
}
}
}
@@ -109,11 +109,11 @@ namespace Umbraco.Core
try
{
var converted = inputConverter.ConvertTo(input, destinationType);
return new Attempt<object>(true, converted);
return Attempt.Succ(converted);
}
catch (Exception e)
{
return new Attempt<object>(e);
return Attempt<object>.Fail(e);
}
}
@@ -125,11 +125,11 @@ namespace Umbraco.Core
try
{
var converted = boolConverter.ConvertFrom(input);
return new Attempt<object>(true, converted);
return Attempt.Succ(converted);
}
catch (Exception e)
{
return new Attempt<object>(e);
return Attempt<object>.Fail(e);
}
}
}
@@ -140,11 +140,11 @@ namespace Umbraco.Core
try
{
var converted = outputConverter.ConvertFrom(input);
return new Attempt<object>(true, converted);
return Attempt.Succ(converted);
}
catch (Exception e)
{
return new Attempt<object>(e);
return Attempt<object>.Fail(e);
}
}
@@ -154,15 +154,15 @@ namespace Umbraco.Core
try
{
var casted = Convert.ChangeType(input, destinationType);
return new Attempt<object>(true, casted);
return Attempt.Succ(casted);
}
catch (Exception e)
{
return new Attempt<object>(e);
return Attempt<object>.Fail(e);
}
}
return Attempt<object>.False;
return Attempt<object>.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<string>(true, output);
return Attempt.Succ(output);
}
catch (NotSupportedException ex)
{
return new Attempt<string>(ex);
return Attempt<string>.Fail(ex);
}
}