Lazy Loading C#.Net

By | March 26, 2018

Some of the interviewer’s favourate question or we can say common interview question is  What is Lazy Loading in C#.Net and when to use it 

Lazy Loading in C#.Net

Lazy loading is the concept in which we delay the loading of the object until the point where we need it.

In simple terms loading the objects on demand instead of loading it unnecessarily.

So the logic of lazy loading is to load object/data when it actually needed.

Lazy loading is essential when the cost of object creation is very high and the use of the object is vey rare. So, this is the scenario where it’s worth implementing lazy loading.

First, we will check how traditional program works without lazy loading, then we will understand the problem and solve it by using lazy loading.

Lets check below example,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LazyLoading
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Customer c = new Customer();

            List<Order> orders = c.Orders;
        }
    }

    public class Customer
    {
        public string CustomerName { get; set; }

        public int CustId { get; set; }

        private List<Order> _orders = null;

        public List<Order> Orders
        {
            get { return _orders; }
        }

        public Customer()
        {
            this.CustId = 1;

            this.CustomerName = "Jatin";

            Order order = new Order();
            _orders = order.GetOrders();
        }
    }

    public class Order
    {
        public int OrderId { get; set; }
        
        public List<Order> GetOrders()
        {
            List<Order> orderDetails = new List<Order>();
            Order order = new Order();
            order.OrderId = 123;
            orderDetails.Add(order);

            order = new Order();
            order.OrderId = 345;
            orderDetails.Add(order);

            return orderDetails;
        }
    }
}

When we run the code,

Lazy Loading in c#.Net

                                                    Lazy Loading in c#.Net

In above code, On first statement only  Customer c = new Customer();

Customer as well as Orders details getting loaded. [Here Order details are not needed]

So suppose if we have 2ok of orders or even more than that, in that situation it will degrade the performance.

Lazy loading implementation,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LazyLoading
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Customer c = new Customer();

            List<Order> orders = c.Orders.Value;
        }
    }

    public class Customer
    {
        public string CustomerName { get; set; }

        public int CustId { get; set; }

        private Lazy<List<Order>> _orders = null;

        public Lazy<List<Order>> Orders
        {
            get { return _orders; }
        }

        public Customer()
        {
            this.CustId = 1;

            this.CustomerName = "Jatin";

            Order order = new Order();
            _orders = new Lazy<List<Order>>(() => order.GetOrders());
        }
    }

    public class Order
    {
        public int OrderId { get; set; }
        
        public List<Order> GetOrders()
        {
            List<Order> orderDetails = new List<Order>();
            Order order = new Order();
            order.OrderId = 123;
            orderDetails.Add(order);

            order = new Order();
            order.OrderId = 345;
            orderDetails.Add(order);

            return orderDetails;
        }
    }
}

In above code, it does not load order object while creation of customer object,

Lazy Loading in c#

Lazy Loading in c# it loads the order object, when it was actually requires,

Advatages

  • Prevent the unnecessary loading of the heavy object.
  • Minimizes the start up time of the application.
  • Less memory consumes because of the on-demand loading.
  • DB Call only when it requires.

DisAdvantage: Need to check loading is requires or not, so slight decrease in performance.

So when to use it? Whenever child object details are very heavy in nature, then prefer to use lazy loading.

 

Leave a Reply

Your email address will not be published. Required fields are marked *