All Related Entities Set

Posts   
 
    
Posts: 2
Joined: 23-Oct-2008
# Posted on: 23-Oct-2008 02:19:29   

I am looking for an event handler I can override, which is called once ALL the related entities have been set e.g. OnAllRelatedEntitiesSet.

I have found the OnRelatedEntitySet method, which is called each time a related entity is set, but there is no way to guarantee that all related entities have been set without specifically checking each one.

Ideally I want to be able to do something like the following:

public partial class MyEntity { protected override void OnAllRelatedEntitiesSet() { base.OnAllRelatedEntitiesSet(); OnLoad(); }

private void OnLoad()
{
    _field.SetEntity(this);
}

}

At the point when I send the entity reference into the field method I need all related entities to be set.

Is there some way I can do this as I can't find it at the moment?

Thanks.

Adrian

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Oct-2008 07:52:36   

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.

David Elizondo | LLBLGen Support Team
Posts: 2
Joined: 23-Oct-2008
# Posted on: 23-Oct-2008 10:33:41   

Thanks for the reply.

However it doesn't really suit our need as it is possible that some of the related entities won't be set on purpose.

So I guess what we really need is the ability to check that all entities we intended to set i.e. via the prefetch path etc. have been set and yes I do realize this is probably not easy to implement in a generic way...

As such I have implemented the following solution, where we check to see if each of the related entities we care about have been set, which has helped us get over our current problem and we are going to have more of a think about how we might like to make this more generic in the future:


    public partial class SaleLineProductDataEntity
    {
        protected override void OnRelatedEntitySet(IEntity2 relatedEntity, string fieldName)
        {
            base.OnRelatedEntitySet(relatedEntity, fieldName);

            if (!IsCreated &&
                AllRelatedEntitiesLoaded())
            {
                OnLoad();
            }
        }

        private bool AllRelatedEntitiesLoaded()
        {
            bool result = false;

            if (RelatedEntity != null)
            {
                result = true;
            }

            return result;
        }

        private void OnLoad()
        {
            _field.SetEntity(this);
        }
    }

Thanks again for your reply.

Adrian