Relationships maps can be placed into an object's DataDefinition to instruct QuickDAL in building its queries.

Basic Mapping

A DataDefinition can be given Parents and Children. In terms of query generation, there's little difference. They both exist primarily to grant the developer a conceptual distinction.

Relationships are represented using the DataRelationship class. It requires two "sample" entities and and field names used to join them. For example:

Orders have LineItem's
In the proper context, this tells QuickDAL that the id field on Order's relates to the orderID field on LineItem.
new DataRelationship(new Order(), "id", new LineItem(), "orderID");

In reality, that definition needs to exist in the proper context of an either an Order, a LineItem, or both. And, in a typical schema the fields on both entities will are the same. So, the definition can usually be simplified slightly:

new DataRelationship(this, "orderID", new LineItem());

And, in the full context of your Order definition:

public class Order : DataObject<Order>
{
  public Guid OrderID { get; set; }
  public override DataDefinition GetDefinition()
  {
    return new DataDefinition()
    {
      DataEntity = "order",
      PrimaryKey = "orderID",
      Maps = new Dictionary<string, IReference>() {
        {"orderID", new Reference<Guid>(
          () => OrderID, v => OrderID= v
        )},
      },
      Children = new List<DataRelationship>() {
        new DataRelationship(this, "orderID", new LineItem())
      }
    };
  }
}

But be careful! These relationships are unidirectional!

In some cases that's OK. The relationship above will allow you to find Order's from or through LineItem's.

var orders = Order.Get(new LineItem() { SKU = "ABC123" });

But, it won't allow us to query in the other direction. That's useful if the relationship is only meaningful in one direction. Usually, we want to add a corresponding relationship.

In our example, we'd add one from LineItem to Order to enable bidirectional joins.

public class LineItem : DataObject<LineItem>
{
  public Guid LineItemID { get; set; }
  public Guid OrderID { get; set; }
  public String SKU { get; set; }
  public override DataDefinition GetDefinition()
  {
    return new DataDefinition()
    {
      DataEntity = "lineItem",
      PrimaryKey = "lineItemID",
      Maps = new Dictionary<string, IReference>() {
        {"lineItemID", new Reference<Guid>(
          () => LineItemID, v => LineItemID= v
        )},
        {"orderID", new Reference<Guid>(
          () => OrderID, v => OrderID= v
        )},
        {"sku", new Reference<Guid>(
          () => SKU, v => SKU= v
        )},
      },
      Parents = new List<DataRelationship>() {
        new DataRelationship(this, "orderID", new Order())
      }
    };
  }
}

And now we can also fetch the LineItem's for a given Order.

var lines = LineItem.Get(someOrder);

And other neat things.

Last edited Aug 7, 2014 at 3:07 AM by svidgen, version 4