- Home
- LLBLGen Pro
- Architecture
Build a recursive validation for nested entity collection and entities
Joined: 30-May-2013
Hi,
I am trying to build a entity validation at a entity collection level that will look through the child collections under the entity collection as well as child entities and validate each one of it. Say i have a ReferenceEntity collection and i build a custom validation logic for this, then the child collection like ReferenceNotesEntity collections and other child entities must be validated.
Below is the code i attempted:
I attempted to build an abstract class "EntityCollectionValidator" of generic type "TEntity" which will be of type "IEntityCollection2". This has a ValidateChildEntities method which will loop through all collections in IEntityCollection2 based on whether the child is of type "List<EntityCollectionBase2<>" or "EntityBase2", an appropriate validator will be called. This code is not working for me to achieve my objective of creating a recursive validation as "((IEntity2)EntityCollection).GetMemberEntityCollections()" wont return me either a type of "List<EntityCollectionBase2<>>" or "EntityBase2". Please suggest me on ways to get this working and if i need to be changing the approach.
public abstract class EntityCollectionValidator<TEntity> : IEntityValidator where TEntity : IEntityCollection2 { protected void ValidateChildEntities() { // get all child entities, find the validator and validate // use reflection, get all members that implement EntityCollectionBase2, check on IOC - inversion var childCollections = ((IEntity2)EntityCollection).GetMemberEntityCollections();
foreach (IEntityCollection2 collection in childCollections)
{
if (collection.GetType() == typeof(List<EntityCollectionBase2<>>))
{
var entityCollection = collection.;
var collectionValidator = entityCollection.GetValidator<TEntity>();
if (collectionValidator != null)
{
collectionValidator.ValidateEntity();
ValidationErrors.AddRange(collectionValidator.GetValidationErrors());
}
}
else
{
// otherwise, loop through the entities in the collection and validate per entity
foreach (EntityBase2 childEntity in collection)
{
// don't validate the root type (could be an issue if the same type is also a child)
if (childEntity.GetType() != typeof(TEntity))
{
// get the validator for the child entity
var childValidator = childEntity.GetValidator();
childValidator.ValidateEntity();
// add errors to the parent
ValidationErrors.AddRange(childValidator.GetValidationErrors());
}
}
}
}
/// <summary> /// The entity collection being validated. /// </summary> private readonly TEntity _entityCollection;
/// <summary>
/// Initializes a new instance of the <see cref="EntityCollectionValidator{TEntity}"/> class.
/// </summary>
/// <param name="entityCollection"> The entity collection. </param>
protected EntityCollectionValidator(TEntity entityCollection)
{
_entityCollection = entityCollection;
ValidationErrors = new EntityValidationErrorList<TEntity>();
}
/// <summary>
/// Gets the collection of validation errors
/// </summary>
protected EntityValidationErrorList<TEntity> ValidationErrors { get; private set; }
/// <summary>
/// Gets the entity collection being validated.
/// </summary>
protected TEntity EntityCollection
{
get { return _entityCollection; }
}
/// <summary>
/// The validate entity.
/// </summary>
/// <returns> The <see cref="bool"/>. </returns>
public abstract bool ValidateEntity();
/// <summary>
/// Gets the validation errors.
/// </summary>
/// <returns> The <see cref="List{EntityValidationError}"/>. </returns>
public List<EntityValidationError> GetValidationErrors()
{
return ValidationErrors;
}
/// <summary>
/// Gets a value indicating whether the entity is valid
/// </summary>
/// <returns>Bool indicating if the entity is valid</returns>
public bool IsValid()
{
return ValidationErrors.Count == 0;
}
}
}
Thanks and regards, Damodar
Joined: 28-Nov-2005
If you save a collection recursively, all the childs will get validated eventually, if one fails, you can rollback the whole process. As I see you are trying to implement a generic validation logic for hierarchies, but maybe you don't need that.
Also, I don't understand where exactly do you have problems. Besides, this line:
var childCollections = ((IEntity2)EntityCollection).GetMemberEntityCollections();
... won't compile as you cannot cast EntityCollection to IEntity2.