Xpo Study Notes (1)

Source: Internet
Author: User
Xpo usage record

Xpo usage record
Http://www.rainsts.net/article.asp? Id = 50

1. The entity class generally inherits from xpobject directly. If you need to customize a primary key or an auto-increment field, you can inherit from xpcustomobject.

2. The object class inherited from xpbaseobject will be physically deleted after the delete operation. The inheritance class of xpcustomobject & xpobject is soft deletion.
(1) soft object Deletion
Customer. Delete ();
Customer. Save ();

(2) Physical Deletion
Session. defaultsession. purgeobject (customer );

(3) Physically delete all marked soft-deletion records.
Session. defaultsession. purgedeletedobjects ();

(4) Clear all data in the database.
Session. defaultsession. cleardatabase ();

(5) restore the deleted records.
While (true)
{
O = (myobject) Session. defaultsession. findobject (typeof (myobject), new notoperator (New nulloperator ("gcrecord"), true );
If (O = NULL)
{
Break;
}
O. gcrecord = NULL;
O. Save ();
}

(6) Batch Delete

Xpmers = new xpcollection (typeof (product), new binaryoperator ("customer", "Tom "));
Session. defaultsession. Delete (MERS mers );
Session. defaultsession. Save (customers); // persist deletion my 1.58 session. Save () does not reload this method ????

3. The class inherited from xpcustomeobject must use keyattribute to specify the primary key, and only one field (???) can be specified (??????).
Public Class Customer: xpcustomobject
{
[Key]
Public string name;
}

4. Specify the field type and size.
Public Class Customer: xpobject
{
[Size (20)]
Public string name;

[Dbtype ("nvarchar (6)"]
Public String postcode;

[Dbtype ("text")]
Public String summary;
}

5. field verification. By default, public field is used to publish fields. You can use public property to assign values to fields.
Public Class Customer: xpobject
{
Private string postcode;
Public String postcode
{
Get {return postcode ;}
Set
{
//... Verification Code omitted...
Postcode = value;
}
}
}

6. Read-Only fields. By default, xpo only creates writable public fields or attributes. You can use the persistentattribute to create read-only fields. Nonpersistentattribute is opposite, indicating that the field or attribute is not written to the database.
Public Class Customer: xpobject
{
[Persistent]
Public datetime date
{
Get {return datetime. Now ;}
}

[Persistent ("date")]
Private datetime date;
}

7. One to multiple. The following DEMO code shows that each consumer has multiple shipping addresses.

"Customeraddress" Link name.
"Aggregated" cascade operations (delete and update ).
"Addresses" set property name.

Public Class Customer: xpobject
{
Public string name;

[Association ("customeraddress", typeof (Address), aggregated]
Public xpcollection addresses
{
Get {return getcollection ("addresses ");}
}
}

Public Class address: xpobject
{
[Association ("customeraddress")]
Public customer;
}

8. During one-to-one matching, the corresponding object is not automatically created.

Public Class Customer: xpobject
{
Public customer ()
{
// Do not place any code here.
}

Public customer (session): Base (Session)
{
// Do not place any code here.
}

Public Address;

Public override void afterconstruction ()
{
Base. afterconstruction ();
// Initialize object properties here

Address = new address ();
}
...
}

9. Many to many. Each consumer has multiple types, and each type contains consumers.
Note that the link names of the two classes are the same.
Public Class Customer: xpobject
{
Public string name;

[Association ("customertype", typeof (type), aggregated]
Public xpcollection types
{
Get {return getcollection ("types ");}
}
}

Public class type: xpobject
{
[Association ("customertype", typeof (customer)]
Public xpcollection MERs
{
Get {return getcollection ("MERs ");}
}
}

10. latency field. The delayedattribute attribute allows a field to load data only when it is operated. It is suitable for big data fields.
Public class goods: xpobject
{
Public string name;
Public decimal price;
Public String catalogid;
...
Private xpdelayedproperty image1 = new xpdelayedproperty ();

[Delayed ("image1")]
Public byte [] image1
{
Get {return (byte []) image1.value ;}
Set {image1.value = value ;}
}
}

11. Paging. Note that the xpcursor function of xpo is paging cache, rather than paging. Unlike xpcursor, xpcursor only loads a specified number of object objects (pagesize) into the memory each time. When the objects in the enumeration operation (ienumerable) exceed the current cache, the next page is automatically read for further operations, this method saves more memory than the xpcollection. Because of this, xpcursor only provides the foreach one-way loop operation, instead of using the indexer to obtain object objects. As for the paging method we usually use, we can use xppageselector to implement it based on xpcollection.

12. Connect to SQL Server
Session. defaultsession. Connection = new system. Data. sqlclient. sqlconnection ("Server = (local); uid = sa; Pwd =; database = test ");
Session. defaultsession. autocreateoption = autocreateoption. schemaonly;

13. The nullvalueattribute attribute only replaces the value with dbnull when the field is equal to a specific value. If the value is dbnull, this indicator is used instead.
Public Class Customer: xpobject
{
Public string name;

[Nullvalue ("unknown email! ")]
Public String email;
}

14. indexedattribute index Field
Public Class Customer: xpobject
{
[Indexed (unique = true)] // No duplicate index. "[indexed]" has duplicate indexes.
Public string name;
}

15. xpobject. Reload () only refreshes the attributes of the current object and does not refresh the attributes of its associated sub-objects.

16. Monitor xpo to automatically generate SQL statements. Add the following configuration in APP. config. 1.58 my test is invalid .?????

[XML]

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. Diagnostics>
<Trace autoflush = "true" indentsize = "4">
<Listeners>
<Add name = "logfiletracelistener" type = "system. Diagnostics. textwritertracelistener"
Initializedata = "trace. log"/>
<Remove name = "default"/>
</Listeners>
</Trace>
<Switches>
<Add name = "xpo" value = "3"/>
</Switches>
</System. Diagnostics>
</Configuration>

17. explicitloadingattribute can specify the recursive layers of multi-layer references to reduce database queries and improve performance.

Class A: xpobject
{
Public string name = "";
}

Class B: xpobject
{
Public string name = "B ";
[Explicitloading (3)]
Public;
}

Class C: xpobject
{
Public string name = "C ";
[Explicitloading]
Public B;
}

Class D: xpobject
{
Public string name = "D ";
[Explicitloading]
Public C;
}

Class E: xpobject
{
Public string name = "E ";
Public d;
}

By default, when we retrieve the E object, the system will automatically obtain (construct) the referenced object. When the reference layers are deep, multiple SQL statements will be generated. In this case, you can use the [explicitloading] attribute to merge multiple queries. In the above example, if D is marked with an attribute, it will automatically Recursively search for references with this tag. Therefore, the queries of D, C, and B are constructed into the same SQL statement, while a is still constructed separately. We can add parameter 3 to attribute tags of B, you can add a to recursion, so that D, C, B, And a are merged into the same SQL statement. Note that this depth parameter indicates the depth of the referenced type from the root, and A is 3 layers away from E.

18. xpo is used in multiple Asp.net applications. The recommended devexpress solution is to create a session for each request, that is:
Stateless + session for each request + connection pool

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.