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.
- ///This line of code indicates: to create a deferred load string object s
- ///prototype for lazyt> object name =new lazyt> (funt>)
- ///is constructed with a generic delegate that requires a method that must be a return value T type when instantiating this delegate
- ///As in this case, T is string, the return value of the Testlazy.getstring method must also be a string type
- lazystring> s = new lazystring> (testlazy.getstring);
|
The Testlazy.getstring () method in this example is as follows:
- Public class Testlazy
- {
- Public Static string GetString ()
- {
- return DateTime.Now.ToLongTimeString ();
- }
- }
|
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.
- Console.WriteLine (s.isvaluecreated); //return False
- Console.WriteLine (s.isvaluecreated); //return True
|
The following is a complete code for testing:
- class Program
- {
- Static void Main (string[] args)
- {
- ///This line of code indicates: to create a deferred load string object s
- ///prototype for Lazy object name =new Lazy (Fun)
- ///is constructed with a generic delegate that requires a method that must be a return value T type when instantiating this delegate
- ///As in this case, T is string, the return value of the Testlazy.getstring method must also be a string type
- Lazy s = new Lazy (testlazy.getstring);
- Console.WriteLine (s.isvaluecreated); //return False
- Console.WriteLine (s.isvaluecreated); //return True
- }
- }
- Public class Testlazy
- {
- Public Static string GetString ()
- {
- return DateTime.Now.ToLongTimeString ();
- }
- }
|
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.
- class Program
- {
- Static void Main (string[] args)
- {
- Bloguser Bloguser = new bloguser (1);
- Console.WriteLine ("Bloguser has been initialized");
- {
- Console.WriteLine (article. Title);}
- }
- }
- Public class Bloguser
- {
- Public int Id { get; Private set; }
- Public Lazy> Articles { get; Private set; }
- Public Bloguser (int ID)
- {
- this. id = ID;
- Articles =new lazy> (() =>articleservices.getarticesbyid (id));
- Console.WriteLine ("Bloguser initializer");
- }
- }
- Public class Article
- {
- Public int Id { get; set; }
- Public string title{get; Set;}
- Public DateTime publishdate { get; set;}
- Public class Articleservices
- {
- Public Static List Getarticesbyid (int bloguserid)
- {
- List articles = new list {
- New article{id=1,title="Lazy Load", Publishdate=datetime.parse ("2011-4-20")},
- New article{id=2,title="Delegate", Publishdate=datetime.parse ("2011-4-21")},
- New article{id=3,title="Event", Publishdate=datetime.parse ("2011-4-22")},
- New article{id=4,title="Thread", Publishdate=datetime.parse ("2011-4-23}"
- };
- Console.WriteLine ("Article initalizer");
- return articles;
- }
- }
|
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.