Skip to content

Commit

Permalink
Entity validation infrastructure. related #1
Browse files Browse the repository at this point in the history
  • Loading branch information
andreazevedo committed Aug 8, 2011
1 parent 9e72834 commit d5a1d39
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/PetStore.Core/DomainObjects/IValidatable.cs
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);
}
}
22 changes: 22 additions & 0 deletions src/PetStore.Core/DomainObjects/IValidator.cs
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);
}
}
71 changes: 71 additions & 0 deletions src/PetStore.Core/DomainObjects/ValidationException.cs
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
}
}
3 changes: 3 additions & 0 deletions src/PetStore.Core/PetStore.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DomainException.cs" />
<Compile Include="DomainObjects\IValidatable.cs" />
<Compile Include="DomainObjects\IValidator.cs" />
<Compile Include="DomainObjects\ValidationException.cs" />
<Compile Include="Extension\StringExtension.cs" />
<Compile Include="Helper\Check.cs" />
<Compile Include="Helper\DataValidator.cs" />
Expand Down

0 comments on commit d5a1d39

Please sign in to comment.