Friday, 19 October 2012

What is lazy/deffer loading and eagar loading?



lazy loading: requirement is matched/reached then only query reaches the data source.and coming to    eager loading –all the data is feach from the datasource.


yeild is a keyword which is used to returns collection of  Elements ----of  typeà IEnumerable<T>  elements. 

Yield by default returns IEnumerable which is generated by complier.
Yield returns elements in type safe manner.
yield makes demand-driven programming.means our requirement/condition  is satisfied then  it will return elements.  Which is also called as lazy loading. Which is benefit. Why because instead of  loading all the elements form  collection or datasource  to in-memory. So linq makes memory utilization.
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int j;
            IEnumerable<int> k=Gettwo();
            IEnumerator<int> s = k.GetEnumerator();
            while (s.MoveNext())
            {
                 j = s.Current;
                Console.WriteLine(j.ToString());
            }          
            Console.ReadKey();
       
        }
        public static IEnumerable<int> Gettwo()
        {
            for (int i = 0; i < 5; i++)
            {
                if (i % 2 == 0)
                {
                    yield return i;  // yield return type is IEnumerable<T>
                }

            }
       
        }
    }
}

 lazy(deffered) loading// also called as demand-driven programming(based on requirement is reached)

// if our requirement is reached the our query touch the datasource. this is done through IEnumerable<T> interface.

Based on the select query requirement

Return type of 

IEnumerable<T> will be changes
Ex: select {n=>n.ID}----------------Eid
Select{ n=> n.Name}--------Ename;
Select { n=>n.Dept}----Department……….ect.

As well as
Based on select query
IEnumerable<T> will changes
Like
IEnumerable<string> slist = student1.Where(n => n.Name == "charan").Select(n=>n.Name);
IEnumerable<int> slist = student1.Where(n => n.Name == "charan").Select(n=>n.Age);
IEnumerable<Students> slist = student1.Where(n => n.Name == "charan").Select(n=>n);

As well

Deffered Execution[lazy loading]

// lazy(deffered) loading// also called as demand-driven programming(based on requirement is reached)
if our requirement is reached the our query touch the datasource. this is done through IEnumerable<T> interface.

List<Students> student1 = new List<Students>();
student1.Add(new Students { Name = "anand", Age = 20 });
student1.Add(new Students { Name = "balu", Age = 21 });
student1.Add(new Students { Name = "charan", Age = 19 });

IEnumerable<int> slist = student1.Where(n => n.Name == "charan").Select(n=>n.Age);

student1.Add(new Students { Name = "charan", Age = 47 });
            int i = 1;
            foreach (int item in slist)
            {                               
                Console.WriteLine(item + "\n");
                Console.WriteLine("Total Iteration is:     " +i.ToString());
                i++;
            }
            Console.ReadKey();

Output:

four students are available but two students(iterations) only meet our requirement. so query also executed 2 times only.

Total iteration are 2 .

Incase of dubugging 3 step and 4 step of F11 only the n.Age will be featched from the datasource==ènow here slist.

Remaining means à 1st 2nd times foreach is not be executed.
So finally I says

This is called deffered/on demand/demand-drive programming……

On-demand[ our requirement is name=”charan” ] satisfies then only foreach will executes.


Here students are 4 but iteration is 2 times only.

Eagerloading:

    List<Students> student1 = new List<Students>();
            student1.Add(new Students { Name = "anand", Age = 20 });
            student1.Add(new Students { Name = "balu", Age = 21 });
            student1.Add(new Students { Name = "charan", Age = 19 });

  IEnumerable<int> slist = student1.Where(n => n.Name == "charan").Select(n=>n.Age);

            //int j = slist.Count<int>(); //----here count is 1

            student1.Add(new Students { Name = "charan", Age = 47 });

            int j = slist.Count<int>();   //----here count is 2

            int i = 1;
            foreach (int item in slist)
            {                               
                Console.WriteLine(item + "\n");
                Console.WriteLine("Total Iteration is:     " +i.ToString());
                i++;
            }

            Console.Write(j.ToString());

So count is done once all the students are loaded.// or slist is prepared.

Default in Linq to Sql is Differed/lazy loading


using (wcfDataContext b=new wcfDataContext())
            {
               
                  
 IQueryable<emp> data = from n in b.emps where n.ename == "sai11" select n;

this is executed as differed loading/ lazy loading(not early loading.

{SELECT [t0].[eid], [t0].[ename], [t0].[eloc], [t0].[deptr], [t0].[uploadstatus]
FROM [dbo].[emp] AS [t0]
WHERE [t0].[ename] = @p0



before control moves to foreach stament this exection is done so count is 3 only

in database with the name : Sai11 availble records are 3 only.

                foreach (emp item in data)
                {
                    Console.WriteLine(item.eloc);
                }
                Console.ReadKey();
            
            }
Here is:




No comments:

Post a Comment