Getting Lazy with .NET 4.0

.NET 2010 comes with lots of new features. Some relates to Technology while other relates to language enhancements. The huge class library that is there with .NET framework is also enriched with new classes. In .NET 4.0 there is a new set of classes which introduces a new concept called Lazy initializes. In this article I am going to discuss how simply you can use Lazy initialize to defer the execution of a method or property for values to whenever it is required.

Introduction

It is true that we often use Lazy initializer in our code by restricting the load of objects using Properties. Thus unless the property is called from the code, the object will not created. A sample of it is :

private List lazystrings = null;
public List LazyStrings
{
get
{
if (this.lazystrings == null)
this.lazystrings = this.GetLazyStrings();
return this.lazystrings;
}
}

private List GetLazyStrings()
{
List lazystrings = new List();
for (int i = 0; i < 30; i++)
{
lazystrings.Add(string.Format("Item {0}", i));
}
return lazystrings;
}

In this case we have wrapped the loading of a List inside a property and hence the object will be loaded only when the object calls the property. This is very common scenario of our daily programming needs.
Microsoft introduces a new technique called LazyInitializer that enables to do this in the same way as we do with the code above, but Microsoft recommends to use Lazy instead as becausee it is ThreadSafe. In this article we will see how to implement Microsoft’s Lazy initializers.

Lazy Initializers

Lets look how you can use Lazy intializers in .NET 4.0.

There are 3 types in .NET 4.0 which supports Lazy Initialization.

1. Lazy : It is just a wrapper class that supports lazy initialization.
2. ThreadLocal : It is the same as Lazy but the only difference is that it stores data on Thread Local basis.
3. LazyInitializer : Provides static implementation of Lazy initializer which eliminates the overhead of creation of Lazy objects.

Lets start one by one,

Lazy

Lazy creates a thread safe lazy initialization of objects. In case of critical scenarios where large number os objects to be created for large number of objects and each object by itself creates a lots of objects, for instance say there are large number of Customer and for each customer there are large number of payments, if Customer is an entity and Payment is also an entity, Customer will contain an array of Payment objects. Thus each entity require large number of databasee calls to ensure that the data is retrieved. This doesn’t makes sense. Using Lazy class you can eliminate this problem.

Let us look how to use it :

public class Customer
    {
        public string Name { get; set; }
        public Lazy<IList<Payment>> Payments
    {
            get
            {
                return new Lazy<IList<Payment>>(() => this.FetchPayments());
            }
        }

        private IList<Payment> FetchPayments()
        {
            List<Payment> payments = new List<Payment>();
            payments.Add(new Payment { BillNumber = 1, BillDate = DateTime.Now, PaymentAmount = 200 });
            payments.Add(new Payment { BillNumber = 2, BillDate = DateTime.Now.AddDays(-1), PaymentAmount = 540 });
            payments.Add(new Payment { BillNumber = 3, BillDate = DateTime.Now.AddDays(-2), PaymentAmount = 700 });
            payments.Add(new Payment { BillNumber = 4, BillDate = DateTime.Now, PaymentAmount = 500 });
            //Load all the payments here from database
            return payments;
        }

        public Payment GetPayment(int billno)
        {
            if (this.Orders.IsValueCreated)
            {
                var payments = this.Payments.Value;
                Payment p = payments.FirstOrDefault(pay => pay.BillNumber.Equals(billno));
                return p;
            }
            else
                throw new NotImplementedException("Object is not initialized");
        }

    }

    public class Payment
    {
        public int BillNumber {get;set;}
        public DateTime BillDate { get; set; }
        public double PaymentAmount { get; set; }
    }

Here I have created a class called Payment which has few properties. Each Customer has a list of Payments.You can see in the Customer class, I have created a Lazy>. This will ensure that the list will be populated only when the Delegate passed to the constructor of Lazy object is called, and it will only be called when this.Payments.Value is called. The property IsValueCreated will evaluate to true when the List is created.

Similar to list, you can also use Lazy binding for normal objects. Just instead of List, you need to create Lazy of Object like Lazy.

Note: System.Lazy creates a ThreadSafe object by default. The Default constructor creates object with LazyThreadSafetyMode.ExecutionAndPublication. Thus once an object is created by one thread, the object will be accessible to all other concurrent threads.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434

2 Comments to “Getting Lazy with .NET 4.0”

  1. Now this is called perfection. I was expecting that you might have already written something on Lazy initialization.. n here you go Google bring me to your post 🙂

    twitter – @vendettamit

Comments are closed.