Oct282008

Lazy loading, immediate loading, performance

While i am still reading the linq book i mentioned yesterday, i also  keep working on the projects at work (i need money to pay the bills). One of the pages i did using linq was very slow, and i ran the profile and guess what, lot's queries were sent to the  database by linq. I decided to read more about linq :) before i use it in the production system. At chapter 4, i read about lazy loading and immediate loading, and decided to share it here. I am sure all of you know about linq and its lazy loading mechanism, although this is most of the time performance saver, if you dont really look into your code, this also could kill your app. Here is a simple code example using northwind database (the code is from "Programming LINQ" Ms Press)

[code:c#]

var query = from c in Customers
                  where c.Orders.Count>20
                  select c;

foreach(var row in query){
    Console.WriteLine(row.CompanyName);
    foreach(var order in row.Orders){
         Console.WriteLine(order.OrderID);
    }

[/code] 

The bold part is the performance killing part, each time you ask for row.Orders, LINQ makes a query to the Orders table to get the related Orders, and this row.Count times inner query. However if you have loaded the Orders while you are loding the Customers, then you dont need this inner query. So you should really look at your code and decide if you need lazy loading or immediate loading. BTW here is the immediate loading:

[code:c#]
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.AssociateWith<Customer>(c=>from o in c.Orders
                                                               where o.OrderDate.Value....);
loadOptions.LoadWith(Customer>(c=>c.Orders);
db.LoadOptions = loadOptions;

[/code] 

You should really buy and read this book btw :)

 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Oct272008

Store? Linq?

Today while i was reading "Programming Microsoft LINQ" book, i learnt a very interesting attribute of an entity that i like to share.  So let's assume you have Student class which has FirstName, LastName etc members, and let's assume this class is a entity in LINQ; the pseudo code could like this:

[code:c#]

[Table] public class Student
{
     [Column]
      public string FirstName{
             get{ return this.FirstName_.ToLower();     }
             set{ this.FirstName_ = value.ToLower();}
     [Column}
     public string LastName{....

[/code] 

 When you create an instance of this class, and try to change the value of FirstName from the class instance, the getter and setter functions will be called. However when this is a linq entity, this change a little bit. If you try to update a value using linq over here, the scenario changes a little bit. If you use the above code and use linq to update the entity, linq will use getter setter functions. However if you decorate the member with "Storage" such as:

 

[code:c#]

[Table] public class Student
{
     [Column(Storage="FirstName_"]
      public string FirstName{
             get{ return this.FirstName_.ToLower();     }
             set{ this.FirstName_ = value.ToLower();}
     [Column}
     public string LastName{....

[/code] 

Now Linq will skip the getter and setter functions and will directly access to the private member to update it :)

so watch out.

 

 



Tags:

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses

Oct262008

MVC Presentation at CodeCamp

oday i was a speaker at the socalcodecamp for the first time. First let me talk about what i think about this codecamp as a listener :).

I saw that the presentation titles do not really match with presentation contents. Usually the titles are too much promising, raising the bar, and ... content is totally different. Another interesting thing is that, i was expecting lots of talks about MVC and Silverlight but didnt find much. It was usually patterns, methodoligies. So hopefully next codecamp will be full of mvc and silverlight :). 

Besides this, the campus was nice, though there was a parking problem as some of the gates were closed, and i think when you host an event that you expect a few hundred people to few thousand people, you should give a map that tells you where the restrooms and restaurants are.

As a speaker, i was very much excited i guess during the presentation, but i think i did good :). I will upload the slides and some of the photos too, this presentation motivates me to come up with a few more presentations for the next codecamp.

 Here is my presentation file in powerpoint format:

Introduction to MVC.pptx (224.09 kb)

 I find this MVC thing really fun to play with, it is simple, easy but requires sometime to learn it.  Once you get the concept, i am sure you will enjoy it.

Especially when you start writing unit tests, not only it gives you more confidence about the stability of your code, but also you start learning how to code loosely coupled  objects. I am a newbie in unit testing, but even myself, when i write the function( i apply TAD sometimes), i think, ok how am i going to do test this, when there is these dependicies, and i start refactoring the code. Anyways :) you will find tens of photos at my facebook profile :)

have fun 

 



Tags: ,

E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses