Thursday, September 10, 2009

Referencing objects finding task

IMO, The feature to find objects that reference a concrete Entity instance is essential for any O/RM. So DataObjects.Net 4 also has the feature but is it really usable for developers? I’m afraid, no.

What DataObjects.Net 4 can offer for now:

public static class ReferentialHelper
{
  public static IEnumerable<Entity> FindReferencingEntities(Entity entity);

  public static IEnumerable<Entity> FindReferencingEntities(Entity entity, AssociationInfo association);
}

This assumes that developer must know that somewhere there is a static class ReferentialHelper that is responsible for this functionality. Without such knowledge developer has zero chances to achieve his goals in finding referencing entities. Too bad both for developer and for our product.
Moreover, this pattern doesn’t cover the scenario when developer wants to receive additional information about concrete property that holds a reference to specified Entity instance.

Luckily, we have a chance to make it more usable. With the help of our community, we are inventing the updated referencing objects finder pattern:

1. Structure, that describes a reference, has been introduced:

public struct ReferenceInfo
{
  public Entity ReferencingEntity { get; private set; }
  public Entity ReferencedEntity { get; private set; }
  public AssociationInfo Association { get; private set; }
}

It contains enough information about concrete reference: the AssociationInfo instance that describes connection between 2 objects, and references to objects at both ends of the connection.

2. ReferentialHelper method’s signature has been changed to the following:

public static IEnumerable<ReferenceInfo> FindReferencingObjects(this Entity target)
public static IEnumerable<ReferenceInfo> FindReferencingObjects(this Entity target, AssociationInfo association)

As you see, these extension methods have been targeted the Entity type. The reason for extension methods instead of regular ones is obvious: finding referencing entities is not the responsibility of Entity class. But in order to make them more usable for developers we should somehow bind them to some type that are well-known in DataObjects.Net 4 domain context and are familiar to developer. This type could be Session, Domain or Entity. So after some discussion we choose Entity type because the first place to search for such kind of methods is the Entity type itself.

3. Another extension method that quickly answers to the question “Is Entity instance referenced at all? “ has been added:

public static bool IsReferenced(this Entity target)

 

So, I think that from time to time we should make a review of existing code base in order to find such kind of issues and refactor them into more usable and understandable code.

P.S.
Special thank to Havasvölgyi Ottó – one of the most valuable DataObjects.Net partner and contributor for his bright ideas and constant willingness to participate in numerous DataObjects.Net-related discussions.

CodeProject

1 comment:

  1. I just described nice ReferentialHelper enhancement:
    http://code.google.com/p/dataobjectsdotnet/issues/detail?id=397

    ReplyDelete