XY-ORM tutorial 4: delayed Loading

Source: Internet
Author: User

Latency loading. In other words, an SQL statement is initiated to query the server only when necessary. Delayed loading can effectively improve the query efficiency. Some excellent ORM, such as Nh, can support delayed loading. The xiangyi platform is no inferior. It certainly supports delayed loading. Of course, apart from entity support for delayed loading, we can also define delayed loading.

First, let's take a look at how to configure the object's delayed loading:

/// <Summary>
/// Master table
/// </Summary>
[Table ("LAZY_LOAD_A1")]
Public class A1: BaseEo
{
/// <Summary>
/// Slave table set
/// </Summary>
[ForeignColumn ("LAZY_LOAD_A2", "A1_ID", "Id", "ID, A1_ID", ForeignResultType. EoList, ForeignApplyType. Load,True)] // True indicates that delayed loading is enabled
Public EoList <A2> A2List
{
Get {return this. GetLazyValue <EoList <A2> ("A2List");} // The value method here is slightly different from the general attribute definition.
}
}

/// <Summary>
/// Slave table
/// </Summary>
[Table ("LAZY_LOAD_A2")]
Public class A2: BaseEo
{
[Column ("ID")]
Public string Id {get; set ;}

[Column ("A1_ID")]
Public string A1Id {get; set ;}
}

Next, for data access, You need to register data access with delayed loading attributes:

/// <Summary>
/// Master table data access
/// </Summary>
Public class A1Dao: BaseDao <A1>
{
// Rewrite the registration of external attribute data access, registration of delayed loading attributes
Protected override void RegisterForeignDao (Dictionary <string, BaseDao> foreignDao)
{
ForeignDao. add ("A2List", new BaseDao <A2> () {Db = this. db}); // This Code indicates that for loading A2List, The LoadEoList method is called from BaseDao <A2>.
}
}

Alternatively, you can directly call the BaseDao <A1>. RegisterForeignDao ("A2List", new BaseDao <A2> () {Db = this. Db}) method.

However, the second method calls multiple requests if there are multiple external attributes, because only one external attribute can be registered at a time.

Of course, external attributes are not only used for delayed loading, but also for saving, finer, loading of a single object, and other functions.

I hope you can join the group: 222515272 if you have any suggestions. Thank you!

You can download XySoftDemo4 from here.

Xiangyi software, All Rights Reserved

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.