How to implement deferred loading in Framework4.0

Source: Internet
Author: User

Deferred loading, also known as deferred instantiation, delayed initialization, and so on, the main idea is that the creation of objects will be deferred to the time of use, rather than creating objects when the object is instantiated, that is, loading. This approach helps improve the performance of your application, avoid wasteful computing, and save memory usage. For this kind of practice, it seems that it is more appropriate to create it.

Let's take a look at how to implement deferred loading in Framework4.0.

FRAMEWORK4.0 provides a wrapper class Lazy that can be easily implemented for lazy loading.

  1. ///This line of code indicates: to create a deferred load string object s
  2. ///prototype for lazyt> object name =new lazyt> (funt>)
  3. ///is constructed with a generic delegate that requires a method that must be a return value T type when instantiating this delegate
  4. ///As in this case, T is string, the return value of the Testlazy.getstring method must also be a string type
  5. lazystring> s = new lazystring> (testlazy.getstring);

The Testlazy.getstring () method in this example is as follows:

  1. Public class Testlazy
  2. {
  3. Public Static string GetString ()
  4. {
  5. return DateTime.Now.ToLongTimeString ();
  6. }
  7. }

You can use the IsValueCreated property to determine whether an object has been created, and to get the value of the current object through the Value property.

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

The following is a complete code for testing:

  1. class Program
  2. {
  3. Static void Main (string[] args)
  4. {
  5. ///This line of code indicates: to create a deferred load string object s
  6. ///prototype for Lazy object name =new Lazy (Fun)
  7. ///is constructed with a generic delegate that requires a method that must be a return value T type when instantiating this delegate
  8. ///As in this case, T is string, the return value of the Testlazy.getstring method must also be a string type
  9. Lazy s = new Lazy (testlazy.getstring);
  10. Console.WriteLine (s.isvaluecreated); //return False
  11. Console.WriteLine (s.isvaluecreated); //return True
  12. }
  13. }
  14. Public class Testlazy
  15. {
  16. Public Static string GetString ()
  17. {
  18. return DateTime.Now.ToLongTimeString ();
  19. }
  20. }

Let's use an example to demonstrate deferred loading:

In this example, the Bloguser object is used, which contains more than one article object, and when the Bloguser object is loaded, the article object is not loaded and is loaded when the article object needs to be used.

  1. class Program
  2. {
  3. Static void Main (string[] args)
  4. {
  5. Bloguser Bloguser = new bloguser (1);
  6. Console.WriteLine ("Bloguser has been initialized");
  7. {
  8. Console.WriteLine (article. Title);}
  9. }
  10. }
  11. Public class Bloguser
  12. {
  13. Public int Id { get; Private set; }
  14. Public Lazy> Articles { get; Private set; }
  15. Public Bloguser (int ID)
  16. {
  17. this. id = ID;
  18. Articles =new lazy> (() =>articleservices.getarticesbyid (id));
  19. Console.WriteLine ("Bloguser initializer");
  20. }
  21. }
  22. Public class Article
  23. {
  24. Public int Id { get; set; }
  25. Public string title{get; Set;}
  26. Public DateTime publishdate { get; set;}
  27. Public class Articleservices
  28. {
  29. Public Static List Getarticesbyid (int bloguserid)
  30. {
  31. List articles = new list {
  32. New article{id=1,title="Lazy Load", Publishdate=datetime.parse ("2011-4-20")},
  33. New article{id=2,title="Delegate", Publishdate=datetime.parse ("2011-4-21")},
  34. New article{id=3,title="Event", Publishdate=datetime.parse ("2011-4-22")},
  35. New article{id=4,title="Thread", Publishdate=datetime.parse ("2011-4-23}"
  36. };
  37. Console.WriteLine ("Article initalizer");
  38. return articles;
  39. }
  40. }

Run the results as shown:

Finally, the main application scenario is deferred loading:

When you create a child object that is expensive, and you might not be able to use it in your program, you can consider creating a child object in a deferred load way. Another scenario is that when a program starts, it needs to create more than one object, but only a few objects need to be used immediately, so that some unnecessary initialization work can be delayed to use, which can be very effective in increasing the startup speed of the program.

This technology is widely used in ORM frameworks and is not unique to C #, such as the Hibernate framework in Java.



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.