About non-persistent Domain Components. Part 2. Many-To-Many | Xafari Framework

This post explains the nuances of how to implement many-to-many relationship between a non-persistent Domain Components when working with Xafari x07 framework. To see the first part of this investigation, refer to the About non-persistent domain components - Xafari Framework post.

There are 3 combinations of relationship from the viewpoint of persistence:

Persistent, Persistent

This is the standard situation, it will not be considered, since this situation is described in detail in the XAF documentation.

NonPersistent, NonPersistent

The figure below illustrates this situation.

As you can see, there are two non-persistent entities named ObjectM and ObjectN, and entities participate in a N:M (Many-To-Many) relationship. The problem is to implement these relationship between ObjectM and ObjectN.

Problem will be solved in different ways depending on the several variants of relationship:

Strongly-typed persistence collections

An example below demonstrates how to use typed persistence collections to implement the relationship.

This method assumes that the relationship between the original objects ObjectM and ObjectN may be transformed into the relationship between the heirs PersistentObjectM and PersistentObjectN. This is a limitation, because in this case it is impossible to add other direct ObjectM/ObjectN descendants to the PersistentTermObjectNs/ PersistentTermObjectMs collections.

The code snippet below demonstrates implementation for such variant:

The main idea is to describe ObjectN:ObjectM relation using calculated properties (TermObjectNs /TermObjectMs), add persistent collections (PersistentTermObjectNs /PersistentTermObjectMs) to descendants and use these collections when calculate TermObjectNs /TermObjectMs.

Xafari provides a standard way to describe calculated fields using Xafari Terms mechanism.

Non-typed persistence collections

An example below demonstrates how to use non-typed persistence collections to implement the relationship.

To implement relationship between ObjectM and ObjectN objects, you can use a special class that exposes non-typed reference (weak reference) to persistent descendants. Unlike the previous variant, in this case it is possible to add any heirs of ObjectM / ObjectN to the collection. However, when you edit these collections, there are limitations in the UI.

Relation objects are stored in one table

The code snippet below demonstrates implementation for such variant:

You also need to update the metadata by adding the following code to the module:

XPWeakManyToMany.CustomizeTypesInfo() method adds the required metadata to correct work of the Many-To-Many relationship.

Relation objects are stored in separate tables

There may be situations when it is impossible to store all the relationships in one table. In this case, we need to describe additional persistent object to store N: M objects. The example below demonstrates this.

The code above contains a special persistent OwnIntermediateTable class. The corresponding table will store only WeakObjectMs / WeakObjectNs relationship. You also need to update the metadata by adding the following code to the module:

XPWeakManyToMany.CustomizeTypesInfo() method adds the required metadata to correct work of the Many-To-Many relationship.

Persistent, NonPersistent

The figure below illustrates this situation.

As you can see, there are PersistentObjectM2 persistent entity and ObjectN non-persistent entity. These entities participate in a N:M (Many-To-Many) relationship.

The code snippet below shows how to implement this relationship in the application.

As you can see, we propose to use a special intermediate persistent class to implement the N: M relationship between PersistentObjectM2 and ObjectN2. This class exposes the non-typed references (weak reference) to a persistent descendants. There are UI limitations when edit these collections.

As in the case of NonPersistent-NonPersistent, you can use a separate table to store relation objects.

You also need to update the metadata by adding the following code to the module:

XPWeakManyToMany.CustomizeTypesInfo() method adds the required metadata to correct work of the Many-To-Many relationship.
Load NonPersistentCollections solution.