SSDM - Sibilo Software Development Model

Search 
(by Google)
     

Developer's Corner - One-to-Many and Many-to-Many Relationships
  1. Introduction
    1. Overview (N-tier Architecture)
    2. Downloads
  2. Requirements Capture
    1. Natural Language Text to UML
  3. UML Static Data Model
    1. Object / Class
    2. Relationships
    3. Data Type / Enumeration
    4. Supported Data Types
  4. Application Design
    1. Database (UML-DB Mapping)
    2. Business Objects (UML-Class Mapping)
    3. User Interface (UML-User Control Mapping)
  5. Sibilo Software Development Environment (Sibilo.SDE)
    1. Sibilo.SDE User Guide
  6. Sibilo.DBL Programming
    1. Introduction
      1. Background Information
      2. The Clist Class
    2. Core Objects
      1. Common Constructors
      2. Common Destructors
      3. Common Properties
      4. Common Methods
        • Add
        • Update
        • Remove
        • GetDetails
        • Load
        • Equals
      5. Shared Methods
        • GetAll
        • Remove
        • RemoveAll
        • Exists
        • Search
        • ObjectCount
        • CreateInstance
      6. User Defined Attributes
        • AddAttribute
        • RemoveAttribute
        • GetAttributeList
        • GetClassList
        • GetAttribute
        • SetAttribute
    3. Relationships
      1. Overview
      2. One-to-One Relationships
      3. One-to-Many and Many-to-Many Relationships
        • Add
        • Remove
        • RemoveAll
        • Exists
      4. Default Object of a Relationship
        • SetDefault
        • GetDefault

6.3.3 One-to-Many and Many-to-Many Relationships

One-to-Many and Many-to-Many relationships default property is an ISList of the related class. Following are properties of both types of relationships:

Property NameTypeDescription
CountIntegerNumber of items in the relationship. E.g. oUser.Emails.Count. This property is Read Only.
Item (ByVal iIndex As Integer)Was.NetReturns an object, CEmail for example, which has the index iIndex. This property is Read Only.
RelationTypeSibiloDBL.EnumRelationTypeReturns the type of the relationship. Will return either SibiloDBL.EnumRelationType.OneToMany or SibiloDBL.EnumRelationType.ManyToMany. This property is readonly.
ParentIDIntegerReturns the ID of the parent object that the relationship belongs to. This property is only for system use and should not be changed.
RemoteTypeSystem.TypeType of the remote object.
SyncRootSystem.ObjectGets an object that can be used to synchronize access to the collection of the remote objects.

Both relationships have the same methods. When an item is removed from a One-to-Many relationship, its relationship to the parent object as well as the actual object ceases to exist in the case of a composite relationship, while it is not deleted (only the relation to the parent object ceases to exist) in the case of an aggregate relationship. When an item is removed from a Many-to-Many relationship,the relation ceases to exist but the item is not physically removed(aggregation).

1-n and n-n relationships have the following methods:
Add
Remove, RemoveAll
Exists
GetAll
SetDefault, GetDefault

We will now demonstrate how each of these methods can be used for the Emails relationship of the CUser class (One-to-Many). Note that the same methods can be used for the Companies property (Many-To-Many).

Sub Add(ByVal iID As Integer)

When you know the ID of the CEmail instance you would like to add, you can use this method.

Example:

Dim oUser As New CUser(103)

' Create an email
Dim oEmail As New CEmail()
oEmail.Address = "info@sibilo.co.uk"
Dim iEmailID As Integer = oEmail.Add()

' Add the email to the User
oUser.Emails.Add(iEmailID)

Sub Add(ByVal obj As CEmail)

You can also add an object to the relationship.

Example:

Dim oUser As New CUser(103)

' Create an email
Dim oEmail As New CEmail()
oEmail.Address = "info@sibilo.co.uk"
oEmail.Add()

' Add the email to the User
oUser.Emails.Add(oEmail)

If the oEmail object on the above code was not added before being added to the Emails relationship, the relationship object automatically adds it. This feature holds for all relationships.

Sub Add(ByVal col As CList)

You can also add a CList of IDs or objects to the relationship.

Example:

Dim oUser As New CUser(103)
oUser.Emails.Add(colEmails)

     or

Dim oUser As New CUser(103)
oUser.Emails.Add(colEmailIDs)

Sub Remove(ByVal iID As Integer)

You can remove an item from the relationship by its ID.

Example:

Dim oUser As New CUser(103)
oUser.Emails.Remove(18)

Sub Remove(ByVal obj As CEmail)

You can also remove an item by its object reference.

Example:

Dim oUser As New CUser(103)
Dim oEmail As New CEmail(350)

oUser.Emails.Remove(oEmail)

Sub Remove(ByVal col As ISList)

You can also remove a Clist of IDs or objects from the relationship.

Example:

Dim oUser As New CUser(103)
oUser.Emails.Remove(colEmails)

Function RemoveAll() As Boolean

Deletes all the relationships and returns True if successful.

Example:

Dim oUser As New CUser(103)
oUser.Emails.RemoveAll

Note that with One-to-Many and Many-to-Many relationships, you do not have to call the Update method of the base object since all the relationship methods update the database from inside.

 

Sub GetAll([Optional ByVal ReturnObjects As boolean=true], [Optional ByvalclOrder as CList=nothing])

Returns all the remote objects of the relationship.

ReturnObjects parameter specifies if the function will return all the remote objects of the relationship or just their IDs.

clOrder is a CList of CSearchSort parameters used for sorting

Example:

Dim oUser As New CUser(103)

dim clEmails as Clist = oUser.Emails.GetAll()

If not clEmails is nothing then 
  for each oEmail as Cemail in clEmails 
    'do something useful
    Response.Write(oEmail.Address)
  next
EndIf

Function Exists(Byval iId As Integer, [Optional Byval bCheckDB As Boolean = False]) As Boolean

Checks if an object exists in a relationship.

bCheckDB specifies if the database should be checked or just the already loaded remote objects.

Example:

Dim oUser As New CUser(103)

dim EmailExists as Boolean = oUser.Emails.exists(5)