Try fast search NHibernate

08 October 2009

OSS has users, commercial products has customers, ergo p&p is commercial product

This is a long story and I will try to make it short.

In the JAVA ecosystem some projects developers are trying to find a common way to define a specification for a specific problem. After the definition each project can/will implement specification in his way.

For example, have a look : JSR 303 Bean Validation

I know that ask the same attitude in .NET is something really hard… hopefully the CodePlex Foundation will change this fact (if they will extend a little bit their base mission).

My request to Microsoft patterns&practices (Enterprise Library) Validation Application Block was, for far, less than define a new standard… have a look to this thread (don’t forget to pay attention to the dates).

What is that ? It is something so easy as:

namespace uNhAddIns.Adapters
{
///<summary>
///
Contract for the common entity validator.
///</summary>
public interface IEntityValidator
{
///<summary>Returns true if the entity is valid.</summary>
///<param name="entityInstance">
The entity instance.</param>
///<returns>
///
true if the <paramref name="entityInstance"/>
///
has, at least, one invalid value
/// </returns>
bool IsValid(object entityInstance);

///<summary>
///
Validates an entity and returns the information about invalid values.
/// </summary>
///<param name="entityInstance">
The entity instance.</param>
///<returns>
The list of invalid values for the entity.</returns>
IList<IInvalidValueInfo> Validate(object entityInstance);

///<summary>
///
Validates a property of the entity and returns the information about invalid values.
///</summary>
///<param name="entityInstance">
The entity instance.</param>
///<param name="property">
The property. (getter)</param>
///<typeparam name="T">
Type of the entity.</typeparam>
///<typeparam name="TP">
Type of the property</typeparam>
///<returns>
The list of invalid values for the given property.</returns>
IList<IInvalidValueInfo> Validate<T, TP>(T entityInstance, Expression<Func<T, TP>> property) where T : class;

///<summary>
///
Validates a property of the entity and returns the information about invalid values.
///</summary>
///<param name="entityInstance">
The entity instance.</param>
///<param name="propertyName">
The name of one property of the <paramref name="entityInstance"/> </param>
///<returns>
The list of invalid values for the given property.</returns>
IList<IInvalidValueInfo> Validate(object entityInstance, string propertyName);
}
}
namespace uNhAddIns.Adapters
{
///<summary>
///
Contract for the invalid values resulting from a validation.
///</summary>
public interface IInvalidValueInfo
{
/// <summary>
///
This is the class type that the validation result is applicable to. For instance,
/// if the validation result concerns a duplicate record found for an employee, then
/// this property would hold the typeof(Employee). It should be expected that this
/// property will never be null.
/// </summary>
Type EntityType { get; }

/// <summary>
///
If the validation result is applicable to a specific property, then this
/// <see cref="PropertyInfo" /> would be set to a property name.
/// </summary>
string PropertyName { get; }

/// <summary>
///
Holds the message describing the validation result
/// for the EntityType and/or PropertyContext
/// </summary>
string Message { get; }
}
}


Obviously would be nice work together with others teams to create something else than that… for example define some commons interfaces to allow the access to the metadata of validation definitions, for what ? for example the work done in xVal would be a lot more easy.

My dear Alt.NET friends… the team of Microsoft patterns&practices is out.

If you are part of a Validation framework team, would you like to start a real “common validation” project ?

5 comments:

  1. great idea, tired of get stuck with a concrete validation library

    in fact, almost all validation frameworks and libraries share a similar API for validate

    +1

    ReplyDelete
  2. Yes, similar API is very impotent!
    But, I think IInvalidValueInfo should has ErrorLevel property.
    And another fact: there are complex rules that validate entity, but don't single property.

    ReplyDelete
  3. You forgot to mention that for now is in the uNhAddins repository, in case someone want to contribute with an adapter. For now we've only NHV, CastleValidation and DataAnnotations.

    ReplyDelete
  4. @denv :
    >there are complex rules that validate entity, but don't single property.

    I think that those validations will be returned when you call IsValid(entity)... without specifying the property.

    ReplyDelete
  5. @denv
    Validate(object entityInstance);

    ReplyDelete