// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
///
/// Contains the result of model binding.
///
public struct ModelBindingResult : IEquatable
{
///
/// A representing the lack of a result. The model binding
/// system will continue to execute other model binders.
///
public static readonly ModelBindingResult NoResult = new ModelBindingResult();
///
/// Returns a completed representing the lack of a result. The model
/// binding system will continue to execute other model binders.
///
public static readonly Task NoResultAsync = Task.FromResult(NoResult);
///
/// Creates a representing a failed model binding operation.
///
/// The key of the current model binding operation.
/// A representing a failed model binding operation.
public static ModelBindingResult Failed(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return new ModelBindingResult(key, model: null, isModelSet: false);
}
///
/// Creates a completed representing a failed model binding operation.
///
/// The key of the current model binding operation.
/// A completed representing a failed model binding operation.
public static Task FailedAsync(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return Task.FromResult(Failed(key));
}
///
/// Creates a representing a successful model binding operation.
///
/// The key of the current model binding operation.
/// The model value. May be null.
/// A representing a successful model bind.
public static ModelBindingResult Success(
string key,
object model)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return new ModelBindingResult(key, model, isModelSet: true);
}
///
/// Creates a completed representing a successful model binding
/// operation.
///
/// The key of the current model binding operation.
/// The model value. May be null.
/// A completed representing a successful model bind.
public static Task SuccessAsync(
string key,
object model)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
return Task.FromResult(Success(key, model));
}
private ModelBindingResult(string key, object model, bool isModelSet)
{
Key = key;
Model = model;
IsModelSet = isModelSet;
}
///
/// Gets the model associated with this context.
///
public object Model { get; }
///
///
/// Gets the model name which was used to bind the model.
///
///
/// This property can be used during validation to add model state for a bound model.
///
///
public string Key { get; }
///
///
/// Gets a value indicating whether or not the value has been set.
///
///
/// This property can be used to distinguish between a model binder which does not find a value and
/// the case where a model binder sets the null value.
///
///
public bool IsModelSet { get; }
///
public override bool Equals(object obj)
{
var other = obj as ModelBindingResult?;
if (other == null)
{
return false;
}
else
{
return Equals(other.Value);
}
}
///
public override int GetHashCode()
{
var hashCodeCombiner = HashCodeCombiner.Start();
hashCodeCombiner.Add(Key, StringComparer.OrdinalIgnoreCase);
hashCodeCombiner.Add(IsModelSet);
hashCodeCombiner.Add(Model);
return hashCodeCombiner.CombinedHash;
}
///
public bool Equals(ModelBindingResult other)
{
return
string.Equals(Key, other.Key, StringComparison.OrdinalIgnoreCase) &&
IsModelSet == other.IsModelSet &&
object.Equals(Model, other.Model);
}
///
public override string ToString()
{
if (Key == null)
{
return "No Result";
}
else if (IsModelSet)
{
return $"Success {Key} -> '{Model}'";
}
else
{
return $"Failed {Key}";
}
}
///
/// Compares objects for equality.
///
/// A .
/// A .
/// true if the objects are equal, otherwise false.
public static bool operator ==(ModelBindingResult x, ModelBindingResult y)
{
return x.Equals(y);
}
///
/// Compares objects for inequality.
///
/// A .
/// A .
/// true if the objects are not equal, otherwise false.
public static bool operator !=(ModelBindingResult x, ModelBindingResult y)
{
return !x.Equals(y);
}
}
}