// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web.Http;
namespace Microsoft.Web.Http.Data
{
///
/// The data contract of an error that has occurred
/// during the execution of an operation on the server.
/// This is sent back along with the action
/// result(s) to the client.
///
[DataContract]
public sealed class ValidationResultInfo : IEquatable
{
///
/// Constructor accepting a localized error message and and collection
/// of the names of the members the error originated from.
///
/// The localized message
/// A collection of the names of the members the error originated from.
public ValidationResultInfo(string message, IEnumerable sourceMemberNames)
{
if (message == null)
{
throw Error.ArgumentNull("message");
}
if (sourceMemberNames == null)
{
throw Error.ArgumentNull("sourceMemberNames");
}
Message = message;
SourceMemberNames = sourceMemberNames;
}
///
/// Constructor accepting a localized error message, error code, optional stack trace,
/// and collection of the names of the members the error originated from.
///
/// The localized error message
/// The custom error code
/// The error stack trace
/// A collection of the names of the members the error originated from.
public ValidationResultInfo(string message, int errorCode, string stackTrace, IEnumerable sourceMemberNames)
{
if (message == null)
{
throw Error.ArgumentNull("message");
}
if (sourceMemberNames == null)
{
throw Error.ArgumentNull("sourceMemberNames");
}
Message = message;
ErrorCode = errorCode;
StackTrace = stackTrace;
SourceMemberNames = sourceMemberNames;
}
///
/// Gets or sets the error message
///
[DataMember(EmitDefaultValue = false)]
public string Message { get; set; }
///
/// Gets or sets custom error code
///
[DataMember(EmitDefaultValue = false)]
public int ErrorCode { get; set; }
///
/// Gets or sets the stack trace of the error
///
[DataMember(EmitDefaultValue = false)]
public string StackTrace { get; set; }
///
/// Gets or sets the names of the members the error originated from.
///
[DataMember(EmitDefaultValue = false)]
public IEnumerable SourceMemberNames { get; set; }
///
/// Returns the hash code for this object.
///
/// The hash code for this object.
public override int GetHashCode()
{
return Message.GetHashCode();
}
#region IEquatable Members
///
/// Test the current instance against the specified instance for equality
///
/// The ValidationResultInfo to compare to
/// True if the instances are equal, false otherwise
bool IEquatable.Equals(ValidationResultInfo other)
{
if (Object.ReferenceEquals(this, other))
{
return true;
}
if (Object.ReferenceEquals(null, other))
{
return false;
}
return ((Message == other.Message) &&
(ErrorCode == other.ErrorCode) &&
(StackTrace == other.StackTrace) &&
(SourceMemberNames.SequenceEqual(other.SourceMemberNames)));
}
#endregion
}
}