Xaf database data pre-filling

Source: Internet
Author: User

Xaf program design has an advantage that it can be separated from the details of the underlying database. You do not need to know whether MSSQL or objective L is used. You only need to know the business object or xpobject, the class definitions of these xpobject correspond to the table definitions of the database, and each persistence attribute (FIELD) of the xpobject corresponds to the fields of the data table, an xpobject object corresponds to a record in the database table. All the xpobject sets of the same type constitute a data table. In addition, xpobject uses the associationattribute attribute to indicate the foreign key relationship of a data table. This brings us a little trouble to pre-fill the database: The foreign key field in the database may be of the int type, in xpobject, the foreign key is represented as the xpobject type corresponding to the referenced table and modified with the associationattribute attribute. When we have a large amount of pre-filled data, we usually put the data in an independent file (such as XML) instead of hard-coded into the program, and then read the files and then fill the database. Now the problem is, if the reference foreign key saved in your XML document is 5, how can another xpobject save this foreign key as an object? Using underlying SQL statement display is not an ideal solution. Here, I wrote a special Filling Class databasedump to solve this problem.

1. How to Use databasedump

Ilist <dictionary <string, string> records = new list <dictionary <string, string> ();//...... put data in the records collection ...... databasedump <myperson> dump = new databasedump <myperson> (objectspace); dump. dumptable (records); objectspace. commitchanges ();

In this way, you only need to modify the definition of the data file and xpobject in the software/database design. In addition, you may need to modify the reading program of the data file.

The following shows my source code. The function is simple:

// Type T is the xpobject to be filled. After filling, You need to manually objectspace. commitchanges () Class databasedump <t> {iobjectspace objectspace; Public databasedump (iobjectspace objectspace) {This. objectspace = objectspace;} // determines whether the propertyname attribute of the mytype is a foreign key private bool isforeignkey (type mytype, string properyname) {system. reflection. propertyinfo Pi = mytype. getproperty (properyname); bool isassociation = attribute. getcustomattri Contains (PI, typeof (devexpress. xpo. associationattribute) is devexpress. xpo. associationattribute; return isassociation & Pi. propertytype. issubclassof (typeof (devexpress. xpo. xpbaseobject);} // fill in the table records. One record is a dictionary public void dumptable (ilist <dictionary <string, string> Records) {t obj = default (t); foreach (Dictionary <string, string> record in Records) {OBJ = objectspace. createobject <t> (); foreach (Keyvaluepair <string, string> cell in record) {If (isforeignkey (typeof (t), cell. key) {type foreigntype = typeof (t ). getproperty (cell. key ). propertytype; string foreignkeyname = objectspace. getkeypropertyname (foreigntype); criteriaoperator filter = new binaryoperator (foreignkeyname, cell. value, binaryoperatortype. equal); object foreign = objectspace. findobject (foreigntype, filter, true); If (Foreig N! = NULL) typeof (t ). getproperty (cell. key ). setvalue (OBJ, cell. value, null);} else {typeof (t ). getproperty (cell. key ). setvalue (OBJ, cell. value, null );}}}}}

Note that if you need to fill in many tables, and the primary key in your xpobject is identified as an auto-increment ID ([Key [True]) or the database is set to auto-increment, the ID assigned during object creation may be inconsistent with the ID of the final storage, so it is best to submit the changes together after all the data is filled (all saved in objectspace, avoid errors. Therefore, I used the objectspace. findobject method with the bool parameter in the class. Otherwise, the unsubmitted record cannot be found.

2. Data Files

Select an appropriate format to save the data file and write the corresponding reader program. I used a simple XML file and recently used the xmltextreader of C # to parse the file. Because there is no additional information, this method of reading without Fallback is also harmless.

Related Article

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.