C # latency Loading

Source: Internet
Author: User

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.

  1. /// This line of code indicates that you want to create a delayed loading String object s
  2. /// The prototype is LazyT> Object Name = new LazyT> (FunT>)
  3. /// Use a generic delegate for construction. The T-type method must be returned when the delegate is instantiated.
  4. /// In this example, if T is string, the return value of TestLazy. GetString must be of the string type.
  5. Lazystring> s =NewLazystring> (TestLazy. GetString );

In this example, the TestLazy. GetString () method is as follows:

  1. Public ClassTestLazy
  2. {
  3. Public Static StringGetString ()
  4. {
  5. ReturnDateTime. Now. ToLongTimeString ();
  6. }
  7. }

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.

  1. Console. WriteLine (s. IsValueCreated); // return False
  2. Console. WriteLine (s. IsValueCreated); // return True

The complete code is provided below for testing:

 

  1. ClassProgram
  2. {
  3. Static VoidMain (String[] Args)
  4. {
  5. /// This line of code indicates that you want to create a delayed loading String object s
  6. /// The prototype is Lazy object name = new Lazy (Fun)
  7. /// Use a generic delegate for construction. The T-type method must be returned when the delegate is instantiated.
  8. /// In this example, if T is string, the return value of TestLazy. GetString must be of the string type.
  9. Lazy s =NewLazy (TestLazy. GetString );
  10. Console. WriteLine (s. IsValueCreated); // return False
  11. Console. WriteLine (s. IsValueCreated); // return True
  12. }
  13. }
  14. Public ClassTestLazy
  15. {
  16. Public Static StringGetString ()
  17. {
  18. ReturnDateTime. Now. ToLongTimeString ();
  19. }
  20. }

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.

 

  1. ClassProgram
  2. {
  3. Static VoidMain (String[] Args)
  4. {
  5. BlogUser blogUser =NewBlogUser (1 );
  6. Console. WriteLine ("blogUser has been initialized ");
  7. {
  8. Console. WriteLine (article. Title );}
  9. }
  10. }
  11. Public ClassBlogUser
  12. {
  13. Public IntId {Get;Private Set;}
  14. PublicLazy> Articles {Get;Private Set;}
  15. PublicBlogUser (IntId)
  16. {
  17. This. Id = id;
  18. Articles =NewLazy> () => ArticleServices. GetArticesByID (id ));
  19. Console. WriteLine ("BlogUser Initializer ");
  20. }
  21. }
  22. Public ClassArticle
  23. {
  24. Public IntId {Get;Set;}
  25. Public StringTitle {Get;Set;}
  26. PublicDateTime PublishDate {Get;Set;}
  27. Public ClassArticleServices
  28. {
  29. Public StaticList GetArticesByID (IntBlogUserID)
  30. {
  31. List articles =NewList {
  32. NewArticle {Id = 1, Title = "Lazy Load", PublishDate = DateTime. Parse ("2011-4-20 ")},
  33. NewArticle {Id = 2, Title = "Delegate", PublishDate = DateTime. Parse ("2011-4-21 ")},
  34. NewArticle {Id = 3, Title = "Event", PublishDate = DateTime. Parse ("2011-4-22 ")},
  35. NewArticle {Id = 4, Title = "Thread", PublishDate = DateTime. Parse ("2011-4-23}
  36. };
  37. Console. WriteLine ("Article Initalizer ");
  38. ReturnArticles;
  39. }
  40. }

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.

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.