C#(032):lazy延迟加载(C# (032): lazy delay loading)

.在.NET4.0中,可以使用Lazy 来实现对象的延迟初始化,从而优化系统的性能。

正如我们所知,对象的加载是需要消耗时间的,特别是对于大对象来说消耗的时间更多.lazy可以实现对象的延迟加载。延时加载,意思是对象在使用的时候创建而不是在实例化的的时候才创建。

Lazy 对象初始化默认是线程安全的,在多线程环境下,第一个访问 Lazy 对象的 Value 属性的线程将初始化 Lazy
对象,以后访问的线程都将使用第一次初始化的数据。

一、延时加载主要应用的场景:

  • 数据层(ADO.NET或Entity Framework等ORM,Java里面的Hibernate也用到了这种技术)
  • 反射(加载assemblier,type,MEF)
  • 缓存对象,领域实体
  • 单例模式

二、简单用法

如下:其中IsValueCreated属相显示其是否被创建。

    static void Main(string[] args)
    {
     Lazy lazyBig = new Lazy(); 
     Console.WriteLine("对象是否创建" + lazyBig.IsValueCreated);
     lazyBig.Value.Test(); 
     Console.WriteLine("对象是否创建" + lazyBig.IsValueCreated); 
    } 
    
    class Big {
     public Big() { } 
     public void Test() {
       Console.WriteLine("Test...."); 
    }
     } //结果如下: //对象是否创建False //Test.... //对象是否创建True

由此可见,根据lazy创建的对象,只有当第一次使用到它时才会初始化.

另,lazy可使用委托来创建。

    static void Main(string[] args)
    {
        Lazy lazyBig = new Lazy( () => new Big(100) );
        lazyBig.Value.Test();
    } 
    
    class Big { 
      public Big(int id) { this.ID = id; }
      public int ID { get; set; }
      public void Test()
        {
            Console.WriteLine("ID = " + ID.ToString());
        }
    }
————————

. yes NET4. 0, lazy can be used to realize the delayed initialization of objects, so as to optimize the performance of the system.

As we know, object loading takes time, especially for large objects Lazy can realize the delayed loading of objects. Delayed loading means that objects are created when they are used, not when they are instantiated.

Lazy object initialization is thread safe by default. In a multithreaded environment, the first thread accessing the value property of a lazy object will initialize lazy
Object, and all threads accessed in the future will use the data initialized for the first time.

1、 Main application scenarios of delayed loading:

  • 数据层(ADO.NET或Entity Framework等ORM,Java里面的Hibernate也用到了这种技术)
  • 反射(加载assemblier,type,MEF)
  • Cache object, domain entity
  • Singleton mode

2、 Simple usage

As follows: the isvaluecreated dependency shows whether it has been created.

    static void Main(string[] args)
    {
     Lazy lazyBig = new Lazy(); 
     Console.WriteLine("对象是否创建" + lazyBig.IsValueCreated);
     lazyBig.Value.Test(); 
     Console.WriteLine("对象是否创建" + lazyBig.IsValueCreated); 
    } 
    
    class Big {
     public Big() { } 
     public void Test() {
       Console.WriteLine("Test...."); 
    }
     } //结果如下: //对象是否创建False //Test.... //对象是否创建True

It can be seen that the object created according to lazy will be initialized only when it is used for the first time

Alternatively, lazy can be created using delegates.

    static void Main(string[] args)
    {
        Lazy lazyBig = new Lazy( () => new Big(100) );
        lazyBig.Value.Test();
    } 
    
    class Big { 
      public Big(int id) { this.ID = id; }
      public int ID { get; set; }
      public void Test()
        {
            Console.WriteLine("ID = " + ID.ToString());
        }
    }