-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Entity validation infrastructure. related #1
- Loading branch information
1 parent
9e72834
commit d5a1d39
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace PetStore.Core.DomainObjects | ||
{ | ||
/// <summary> | ||
/// Must be implemented by entities that would be validated. | ||
/// </summary> | ||
/// <typeparam name="TEntity">Entity</typeparam> | ||
public interface IValidatable<out TEntity> | ||
{ | ||
/// <summary> | ||
/// Validates the entity | ||
/// </summary> | ||
/// <param name="validator">Validator responsible for validating the given entity (ref: TEntity).</param> | ||
/// <param name="brokenRules">Errors list.</param> | ||
/// <returns>True if the entity is valid. Otherwise, false.</returns> | ||
bool Validate(IValidator<TEntity> validator, out IList<string> brokenRules); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace PetStore.Core.DomainObjects | ||
{ | ||
/// <summary> | ||
/// Validates an entity | ||
/// </summary> | ||
/// <typeparam name="TEntity">Entity</typeparam> | ||
public interface IValidator<in TEntity> | ||
{ | ||
/// <summary> | ||
/// Validates an entity and returns the errors list. | ||
/// (Or an empty list if there is no error). | ||
/// </summary> | ||
/// <param name="obj">Entity to be validated.</param> | ||
/// <returns>Errors list.</returns> | ||
IList<string> BrokenRules(TEntity obj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using System.Text; | ||
using PetStore.Resources; | ||
|
||
namespace PetStore.Core.DomainObjects | ||
{ | ||
/// <summary> | ||
/// Exception that represents an error on an entity's validation. | ||
/// </summary> | ||
public class ValidationException : DomainException | ||
{ | ||
#region Constructors | ||
|
||
public ValidationException() | ||
: this(GeneralResources.ValidationExceptionDefaultMessage) | ||
{ | ||
} | ||
|
||
public ValidationException(string message) | ||
: base(message) | ||
{ | ||
} | ||
|
||
public ValidationException(string message, Exception innerException) | ||
: base(message, innerException) | ||
{ | ||
} | ||
|
||
public ValidationException(SerializationInfo info, StreamingContext context) | ||
: base(info, context) | ||
{ | ||
} | ||
|
||
public ValidationException(string mainMessage, IList<string> brokenRules) | ||
: this(CreateBrokenRulesMessage(mainMessage, brokenRules)) | ||
{ | ||
BrokenRules = brokenRules; | ||
} | ||
|
||
#endregion | ||
|
||
#region Properties | ||
|
||
/// <summary> | ||
/// Validation rules that were broken. | ||
/// </summary> | ||
public IList<string> BrokenRules { get; private set; } | ||
|
||
#endregion | ||
|
||
#region Private Methods | ||
|
||
private static string CreateBrokenRulesMessage(string mainMessage, IEnumerable<string> brokenRules) | ||
{ | ||
StringBuilder sbMessage = new StringBuilder(); | ||
sbMessage.Append(mainMessage); | ||
foreach (var brokenRule in brokenRules) | ||
{ | ||
sbMessage.Append(" "); | ||
sbMessage.Append(brokenRule); | ||
} | ||
|
||
return sbMessage.ToString(); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters