Delayed loading, also known as delayed instantiation and delayed initialization, mainly expresses the idea that object creation will be delayed until it is created during use, instead of creating an object during Object Instantiation, load only when it is ready for use. This method helps improve the performance of applications, avoid wasting computing resources, and save memory usage. In this way, it seems that it is more appropriate to create an out-of-the-box instance.
First, let's take a look at how to implement delayed loading in Framework4.0.
Framework4.0 provides a packaging class Lazy, which can easily implement delayed loading.
- /// This line of code indicates that you want to create a delayed loading String object s
- /// The prototype is LazyT> Object Name = new LazyT> (FunT>)
- /// Use a generic delegate for construction. The T-type method must be returned when the delegate is instantiated.
- /// In this example, if T is string, the return value of TestLazy. GetString must be of the string type.
- Lazystring> s =NewLazystring> (TestLazy. GetString );
|
In this example, the TestLazy. GetString () method is as follows:
- Public ClassTestLazy
- {
- Public Static StringGetString ()
- {
- ReturnDateTime. Now. ToLongTimeString ();
- }
- }
|
You can use the IsValueCreated attribute to determine whether an object has been created and use the Value attribute to obtain the Value of the current object.
- Console. WriteLine (s. IsValueCreated); // return False
- Console. WriteLine (s. IsValueCreated); // return True
|
The complete code is provided below for testing:
- ClassProgram
- {
- Static VoidMain (String[] Args)
- {
- /// This line of code indicates that you want to create a delayed loading String object s
- /// The prototype is Lazy object name = new Lazy (Fun)
- /// Use a generic delegate for construction. The T-type method must be returned when the delegate is instantiated.
- /// In this example, if T is string, the return value of TestLazy. GetString must be of the string type.
- Lazy s =NewLazy (TestLazy. GetString );
- Console. WriteLine (s. IsValueCreated); // return False
- Console. WriteLine (s. IsValueCreated); // return True
- }
- }
- Public ClassTestLazy
- {
- Public Static StringGetString ()
- {
- ReturnDateTime. Now. ToLongTimeString ();
- }
- }
|
The following is an example to demonstrate delayed loading:
In this example, the BlogUser object is used. This object contains multiple Article objects. When the BlogUser object is loaded, the Article object is not loaded. It is loaded only when the Article object is used.
- ClassProgram
- {
- Static VoidMain (String[] Args)
- {
- BlogUser blogUser =NewBlogUser (1 );
- Console. WriteLine ("blogUser has been initialized ");
- {
- Console. WriteLine (article. Title );}
- }
- }
- Public ClassBlogUser
- {
- Public IntId {Get;Private Set;}
- PublicLazy> Articles {Get;Private Set;}
- PublicBlogUser (IntId)
- {
- This. Id = id;
- Articles =NewLazy> () => ArticleServices. GetArticesByID (id ));
- Console. WriteLine ("BlogUser Initializer ");
- }
- }
- Public ClassArticle
- {
- Public IntId {Get;Set;}
- Public StringTitle {Get;Set;}
- PublicDateTime PublishDate {Get;Set;}
- Public ClassArticleServices
- {
- Public StaticList GetArticesByID (IntBlogUserID)
- {
- List articles =NewList {
- NewArticle {Id = 1, Title = "Lazy Load", PublishDate = DateTime. Parse ("2011-4-20 ")},
- NewArticle {Id = 2, Title = "Delegate", PublishDate = DateTime. Parse ("2011-4-21 ")},
- NewArticle {Id = 3, Title = "Event", PublishDate = DateTime. Parse ("2011-4-22 ")},
- NewArticle {Id = 4, Title = "Thread", PublishDate = DateTime. Parse ("2011-4-23}
- };
- Console. WriteLine ("Article Initalizer ");
- ReturnArticles;
- }
- }
|
Running result:
Finally, the main application scenarios of delayed loading are as follows:
When the sub-object overhead of an object is large, and this sub-object may not be used in the program, you can consider using delayed loading to create the sub-object. Another scenario is that when the program starts, multiple objects need to be created, but only a few objects need to be used immediately, so that unnecessary initialization work can be delayed for use, this can effectively increase the startup speed of the program.
This technology is widely used in the ORM framework and is not unique to C #. For example, the Hibernate framework in Java also uses this technology.