Skip to content

Instantly share code, notes, and snippets.

@thecodejunkie
Created November 7, 2013 21:10
Show Gist options
  • Save thecodejunkie/7361905 to your computer and use it in GitHub Desktop.
Save thecodejunkie/7361905 to your computer and use it in GitHub Desktop.
Revised ModelValidationResult spike
/// <summary>
/// A validation error.
/// </summary>
public class ModelValidationError
{
private readonly Func<IEnumerable<string>, string> errorMessageFormatter;
/// <summary>
/// Initializes a new instance of the <see cref="ModelValidationError"/> class.
/// </summary>
/// <param name="memberName">Name of the member.</param>
/// <param name="errorMessageFormatter">The error message formatter.</param>
public ModelValidationError(string memberName, Func<IEnumerable<string>,string> errorMessageFormatter)
: this(new[] { memberName }, errorMessageFormatter)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModelValidationError"/> class.
/// </summary>
/// <param name="memberNames">The member names.</param>
/// <param name="errorMessageFormatter">The error message formatter.</param>
public ModelValidationError(IEnumerable<string> memberNames, Func<IEnumerable<string>, string> errorMessageFormatter)
{
this.MemberNames = memberNames;
this.errorMessageFormatter = errorMessageFormatter;
}
/// <summary>
/// Gets the member names that are a part of the error.
/// </summary>
/// <value>An <see cref="IEnumerable{T}"/> that contains the name of the members.</value>
public IEnumerable<string> MemberNames { get; private set; }
/// <summary>
/// Gets the error message.
/// </summary>
/// <returns>The error message.</returns>
public string GetMessage()
{
return this.errorMessageFormatter(this.MemberNames);
}
/// <summary>
///
/// </summary>
/// <param name="error"></param>
/// <returns></returns>
public static implicit operator string(ModelValidationError error)
{
return error.GetMessage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment