This behavior is not built in the Framework because (IMHO) it's difficult to understand what "all entities are set" means. For example, could be m:1, 1:n, m:n, 1:1. So you could expect only m:1 but other people would expect 1:n as well. Besides, that's an uncommon request. So it could be an unnecessary check routine.
But, you could do that of course. I wrote a routine for you (place that method on a partial class of your entity or make it a HelperClass that you can use for any entity):
/// <summary>
/// Checks if all related objects (entities o entityCollections) had been set
/// </summary>
/// <param name="checkManyToOne">Whether or not check on ManyToOne members</param>
/// <param name="checkOneToMany">Whether or not check on OneToMany members</param>
/// <param name="checkManyToMany">Whether or not check on ManyToMay members</param>
/// <param name="checkOneToOne">Whether or not check on OneToOne members</param>
/// <returns>True is all related entities had been set, false otherwhise</returns>
private bool AreAllRelateedEntitySet(bool checkManyToOne, bool checkOneToMany, bool checkManyToMany, bool checkOneToOne)
{
// value to return
bool toReturn = true;
// get the relations of this entity
List<IEntityRelation> rels = this.GetAllRelations();
// get the related data of this entity
Dictionary<string, object> relatedEntities = this.GetRelatedData();
// check if the related objects had been set
foreach (KeyValuePair<string, object> o in relatedEntities)
{
// obtain the type of the relation
RelationType relType = RelationType.OneToMany;
foreach (IEntityRelation r in rels)
{
if (r.MappedFieldName == o.Key)
{
relType = r.TypeOfRelation;
}
}
// do the check
if ((checkManyToOne && relType == RelationType.ManyToOne)
|| (checkOneToMany && relType == RelationType.OneToMany)
|| (checkManyToMany && relType == RelationType.ManyToMany)
|| (checkOneToOne && relType == RelationType.OneToOne))
{
toReturn &= (o.Value != null);
}
}
return toReturn;
}
which you can call at OnRelatedEntitySet:
protected override void OnRelatedEntitySet(IEntity2 relatedEntity, string fieldName)
{
// checks if all m:1 related entities are set already
if ( AreAllRelateedEntitySet(true, false, false, false) )
// ... your call here
base.OnRelatedEntitySet(relatedEntity, fieldName);
}
or better, at your Business Layer
public void SendTheEntity(CustomerEntity customerToSend)
{
// checks if all m:1 related entities are set already
bool allSet = customerToSend.AreAllRelateedEntitySet(true, false, false, false);
if (allSet)
{
// ... send it
}
}
Hope helpful.