RAFY domain Entity Framework-tree entity function (self-correlating table)

Source: Internet
Author: User

In the RAFY domain Entity Framework, the self-associated entity structure has been specially processed, and this function is explained below.

Scene

When you develop a database application, you often encounter scenarios in which you have self-correlating tables. For example, classification information, organizational structure of the Department, folder information, etc., are not hierarchical. such as the folder in the operating system:

When developing such a program, it is often designed with a table in which a nullable foreign key directly references the table itself. The corresponding entity such as:

For such scenarios, many ORM frameworks do not do the default processing, developers often have to do the repetitive work: create a similar structure of the table, write relational processing code, write query code ... This scenario often occurs, so the Rafy Entity Framework, by default, supports a series of functions of tree entities to reduce duplication of effort.

features and instructions for use

The tree entity feature in Rafy, where the developer uses a single line of code to turn this feature on for the specified entity, the framework automatically completes the following tasks:

    • Automatically adds a self-referential relationship for an entity. Automatically generate database self-correlating tables.
    • The Treeindex index of the tree node is maintained automatically.
    • Comes with multiple queries for querying tree nodes.
    • The query results are automatically transformed into the structure of the tree.
    • Supports on-demand loading of tree nodes.

Below, we will explain each of them individually.

Open the Tree entity feature

Developers can use a single line of code to turn a specified entity into a tree entity. In the configuration code for the specified entity, add the following line of code:

Automatically add a self-referential relationship for an entity

The following properties have been defaulted on the entity base class to express the relationship between tree nodes.

These properties make sense when an entity type is configured as a tree entity.
Supporttree: Indicates whether the entity is a tree-type entity.
Treeindex: The encoding and indexing of tree nodes. This property is mapped to a field in the database.
Treepid: The Id of the parent node of the tree node. This property is mapped to a field in the database.
Treeparent: The parent node entity of the tree node.
Treechildren: The collection of all child nodes of the tree node.

Automatically generate database self-correlating tables

After you run the program, the table for that entity will automatically add two fields: Treeindex, treepid, such as:

Treeindex index of the Automatic Maintenance tree node

Treeindex is the system number of the tree node, which is maintained automatically by the framework. Shows the format of the treeindex of a tree being used:

This property can not only be used for display, but more importantly, it is a structural basis for a large number of functions of tree entities. For example, when querying all nodes under a node, the fuzzy match is done by Treeindex. So the value of this property is very important and can only be maintained by the framework itself, and cannot be set by the developer.

Developers can change the parent-child relationship between nodes and nodes by Treeparent, Treechildren, Treepid, and so on, the treeindex of the corresponding nodes will be changed automatically at the same time.

Representation of a tree structure

The structure of the tree is very important, and I have drawn a sketch to show that:

Mainly consists of three types of the entire tree: Entitylist, Entity, Entitytreechildren. This structure can represent a complete tree, or it can represent a part of a tree. Where entitylist is used to store the root node of the tree (or the topmost node if it is a partial tree); The Entity represents each node in the tree, and the Entitytreechildren collection represents a child node under a node.

In addition, the Entitytreechildren collection can be loaded on demand. When it has not yet been loaded, traversing the entire tree can only traverse the tree node that is currently in memory. For example, ROOT3 's child nodes are not loaded, and 1.2.2 's child nodes are not loaded.

So, what if the finer has not yet loaded into the memory node? This requires the use of the Loadallnodes method in the Itreecomponent interface. Entitylist, Entity, Entitytreechildren these three types all implement the Itreecomponent interface, the following is the definition of this interface:

In addition, the Eachnode method can be used to traverse the whole tree with a depth-first algorithm.

comes with multiple queries for querying tree nodes

There are many query methods in the entity warehouse, some of which are specifically designed for tree entities:

    • Gettreeroots: Queries all root nodes.
    • Getbytreepid: Finds the immediate child node of the specified tree node.
    • Getbytreeparentindex: Recursively finds all child nodes under the node that specify the parent index number.
    • Loadalltreeparents: Recursively loads all the parent nodes of a node. With this method, the parent node of the specified node is assigned a value to its Treeparent property.
    • Getalltreeparents: Gets all the parent nodes of the tree node corresponding to the specified index. The queried parent node is also returned as a partial tree.

In addition, some non-tree entity query methods are also available for tree entities. such as GetAll, Getbyparentid and so on. But there are also differences, for example, when querying a non-tree entity, the GetAll method contains all the entities in the queried entity list, but when querying the tree entity, the results are loaded according to the structure of the tree, that is, only the root node is in the list, and the other nodes are in the subordinate nodes of the root node.

At the same time, these queries often support the use of greedy loaded parameters. As an example of the Gettreeroots method, its interface is this: public entitylist gettreeroots (eagerloadoptions eagerload = null);. It returns only the root node by default, and the child nodes in the root node are not loaded. However, we can specify by Eagerload in the parameters that all child nodes are loaded at the same time as the root node is loaded.

These are just some of the interface to do some necessary explanations, the specific use of methods and other interfaces, please refer to the comments and the source of the unit test.

Limit

There are so many features on it, but the design of the tree entity in Rafy also has this limitation: in a data table corresponding to a tree entity type, only one tree can be stored. The Treeindex of all nodes in the tree must be unique.

Well, given the length, this article simply explains the key concepts and functions in the tree-type entity and does not have an in-depth explanation. This is because, in the process of using you will find that in general, it is very easy to use, just open the tree entity function, and invoke the desired query on it, with no particularly complex API. If you really need to know more, then after understanding the structure of the whole tree design, and then combined with help, comments and unit testing in the source code, I believe it will be relatively simple.

RAFY domain Entity Framework-tree entity function (self-correlating table)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.