ice.NET Key Concepts: Relationships

Relationships connect objects. Information in ice.NET consists of object structures, i.e. objects that are connected by relationships in a meaningful way. Relationships share some common characteristics:

  • In ice.NET all relationships connect exactly two objects.
  • Every relationship conforms to exactly one relationship type. (The type of a relationship cannot be changed.)
  • Every relationship is identified by its relationship ID. This ID does not change and exists until the relationship is deleted.
  • Every relationship has a description.
  • Furthermore relationships can have attributes. The attribute structure of a relationship is determined by it's relationship type.

Example

This example shows how to create relationships.

IObject pBooking = Repository.GetObject(objectId);
IObject pVehicle = Repository.GetObject(vehicleId);

IRelType pRelType = Repository.GetRelTypeByName("PDTec.ICR.BookedVehicle");

Repository.ExecuteTransaction(delegate()
{
    // Create a relationship between the two objects...
    IRelationship pRelationship = pRelType.CreateRelationship(pBooking, pVehicle, "");
    
    // ...[do something with the new relationship]...

});     

This example shows how to navigate relationships.

IObject pCustomer = Repository.GetObject(customerId);

IRelationship[] aRelationships = pCustomer.GetRelationships(
    "PDTec.ICR.CustomerBookings",
    RelDirection.Forward);

foreach (IRelationship pRelationship in aRelationships)
{
    IObject pBooking = pRelationship.ObjectTo;

    // ...[do something with the booking]...
    
    IRelationship pRelationshipVehicle = pBooking.GetSingleRelationship(
        "PDTec.ICR.BookedVehicle",
        RelDirection.Forward);
        
    IObject pVehicle = pRelationshipVehicle.ObjectTo;   
    
    // ...[do something with the booked vehicle]...
}

This example shows how to use the IObject.TryGetSingleRelationship(string, RelDirection, out IRelationship) method to avoid an exception when no matching relationship is attached to the object.

IObject pBooking = Repository.GetObject(bookingId);

// ...[do something with the booking]...

IRelationship pRelationshipVehicle;
if (pBooking.TryGetSingleRelationship("PDTec.ICR.BookedVehicle", RelDirection.Forward, out pRelationshipVehicle))
{
    IObject pVehicle = pRelationshipVehicle.ObjectTo;   

    // ...[do something with the booked vehicle]...
}

Important notice: These examples use the low-level ice.NET API. Implementing Business Objects and using the Business Objects Builder (icebob.exe) enables more type-safe access to attributes, relationships and domain-specific functionality.