protected bool ValidateOrRelationships(ContextEntity ce, List <ContextEntity> entities) { bool ret = true; var relationshipsToCheck = relationships. Where(r => r.RelationshipType == ce.RelationshipType && r.TargetType == ce.ConcreteEntity.GetType() && (r.SourceType == ce.RelatedTo.GetType() || r.OrSourceTypes.Any(ot => ot == ce.RelatedTo.GetType()))); foreach (var rcheck in relationshipsToCheck) { var possibleRelatedToTypes = new List <Type>() { rcheck.SourceType }; possibleRelatedToTypes.AddRange(rcheck.OrSourceTypes); var entitiesInThisRelationship = entities.Where(e => e.RelationshipType == ce.RelationshipType && e.ConcreteEntity == ce.ConcreteEntity && possibleRelatedToTypes.Any(rt => rt == ce.RelatedTo.GetType())); int count = entitiesInThisRelationship.Count(); if (rcheck.Minimum > count || count > rcheck.Maximum) { ret = false; break; } } return(ret); }
/// <summary> /// Adds a root entity to which other entities may be in relationship. /// </summary> public void Add(IEntity entity) { var ce = ContextEntity.Create <NullRelationship>(entity, NullEntity.Instance); entities.Add(ce); ValidateContextDeclaration(entity, () => entities.Remove(ce)); ValidateRelationshipDeclaration <NullRelationship>(entity, NullEntity.Instance, () => entities.Remove(ce)); }
/// <summary> /// Adds a target entity that has the specified relationship to the source entitiy. /// </summary> public void Add <R>(IEntity target, IEntity source) where R : IRelationship { ValidateRelationship <R>(target, source); var ce = ContextEntity.Create <R>(target, source); entities.Add(ce); ValidateContextDeclaration(target, () => entities.Remove(ce)); ValidateRelationshipDeclaration <R>(target, source, () => entities.Remove(ce)); }
public bool Validate(ContextEntity ce, List <ContextEntity> entities) { bool ret = ValidateOrRelationships(ce, entities); if (ret) { ret &= ValidateAndRelationships(ce, entities); } return(ret); }
protected bool ValidateAndRelationships(ContextEntity ce, List <ContextEntity> entities) { bool ret = true; // All relationships of the relationshtype having the same target must match the "and" requirements. var relationshipsToCheck = relationships. Where(r => r.RelationshipType == ce.RelationshipType && r.TargetType == ce.ConcreteEntity.GetType()); foreach (var rcheck in relationshipsToCheck) { if (rcheck.AndSourceTypes.Count > 0) { var requiredRelatedToTypes = new List <Type>() { rcheck.SourceType }; requiredRelatedToTypes.AddRange(rcheck.AndSourceTypes); var entitiesInThisRelationship = entities.Where(e => e.RelationshipType == ce.RelationshipType && e.ConcreteEntity == ce.ConcreteEntity && requiredRelatedToTypes.Any(rt => rt == ce.RelatedTo.GetType())); ret = entitiesInThisRelationship.Count() == requiredRelatedToTypes.Count; if (!ret) { ret = false; break; } } } return(ret); }