Videos from ALT.NET Sydney Usergroup, 30 July 2013

Continuing my experiments of recording the few events I attend in Sydney using a simple Galaxy S4 phone, this time I’m posting videos from Sydney ALT.NET usergroup gathering in July.

Of course if you are interested in all the videos I put online, including a few tutorials I have created myself instead of just recording, check out my channel on YouTube.

Now to the videos…

Applications of the Reactive Extensions framework

By Niall Connaughton, @nconnaughton on twitter

Moving to HTTPS

By James Crisp, @jtcrisp on twitter

Final Note

Please let me know if you find these videos useful. I may not be able to do much about the quality in the short term, so, it’s worth knowing if the videos as-is are helping, or I need to pause until I get better tooling than just my phone camera and a simple webcam.

So, check out all the videos on YouTube, leave comments ion them, and let me know the topics that interest you, which may in the future turn into tutorials I create myself, or suggestions to ask usergroup leaders to look for presenters to talk about.

Which ORM? LINQ To SQL, Entity Framework? LLBLGen? NHibernate?…?

While I was planning to write about the same topic and have the draft ready in my Windows Live Writer waiting to complete, I found an interesting question in StackOVerflow and couldn’t just resist to answer:

ORM/Persistence layer AdviceORM

The question starts with:

I’m starting a new project and I’m looking around for either a very good ORM or for a non-SQL-based persistence layer.

Then follows up with a REALLY GOOD summary of what he believes about each known ORM he knew out of his own findings and search. I advice you to go read it.

However, all this investigation didn’t get him to a single choice answer. And I can’t blame him. This is one fo the questions that will remain for so long without a single answer, or maybe having the popular “It depends” answer.

I have had a LONG research in this topic as well. I have read for so long (and watched videos/casts) to make sure of the best usage of many ORMs and then used them sometimes in test projects sometimes in production, and I wanted to share my thoughts based on this. I posted a long answer there on the question in StackOverflow, and I want to share this answer with you here. I may also have a second part of this post based on my existing Windows Live Writer draft, but, based on my previous times, I think I won’t!

Let me first quote some parts from the question itself:

I also want to avoid at all cost having to mess with string-based queries so tools supporting LINQ or otherwise intuitive and possibly strongly typed queries get a big bonus.
Finally working with POCO objects is another thing I’d really want to do
Here’s a list of products I’ve evaluated and why they don’t fit, just so that I don’t see any advice about using those:

  • NHibernate: crazy xml stuff, too much set up, high maintenance complexity and cost for model changes, session factories are messy and don’t fit well with my needs
  • Castle ActiveRecord: NHibernate based, little documentation plus some problems related to NHibernate still apply. Furthermore, to get decent models it takes so many attributes that one is better off creating the schema manually, and the way relations are handled is a shame.
  • Linq To SQL: missing POCO objects and according to MS it won’t improve much overtime (EF is what they’re committed to)
  • Entity Framweork: although in v4 POCO objects are possible, they’re still pretty hacky and force you into doing too much manual work to set things up. Besides, v4 is just a beta
  • LLBLGen Pro: good, especially with SelfServicing adapters, but not POCO. Also, the LINQ provider isn’t perfect yet. Finally, deleting a group of objects is not possible via LINQ which results in mixing APIs (one of which is far from intuitive) and that I don’t like.
  • SubSonic SimpleRepository: for a few minutes I thought I was dreaming. The deam came to an end as I figured out how the thing didn’t handle relationships
  • Here was my answer:

    If you can afford LLBLGen license, go for it.

    I seriously don’t like LINQ query-syntax the more I work with it (although I LOVE the language features related to it like Extension Methods and Expression Trees).

    I loved it at first like everybody else, but being uncertain whether [[ where employee.Name.StartsWith(“John Smit”) ]] in that XYZ LINQ provider will be done in SQL statement or in LINQ to Objects (after the SQL returns all results), and whether [[ user.Roles.Contains(role) ]] will at all work or not is a big step behind.

    LLBLGen can make deleting all items without loading them as easy as

    <code>MyEntityCollection.DeleteAll( new MyEntity {Condition = value} );</code>

    This is pretty simple and I like it. You get lazy loading and you set eager/deep loading by default and/or per query using Prefetch API. You can compose and construct dynamically (and easily) any filter/sort/loading at infinite levels. It’s very nice.

    There are only two problems about LLBLGen: first, it’s price not all companies would love to pay especially given the hype Microsoft alternatives have. Second, the naming convention although standard in RDBMS theories) like PredicateFactory instead of “where” or “filter” and Prefetch instead of deep loading and even SortExpression instead of orderby, those all are a little scary to a developer working with it for the first times, but soon you learn to love them, given the power and ease they give. There are talks about POCO support in LLBLGen 3.0. I cannot tell about it because I don’t know.

    Now given I no longer work in a company that uses LLBLGen, the company uses LINQ to SQL mainly because it’s “proven” in so many projects without big failures (unlike EF 1, which is lacking even LINQ features in LINQ to SQL and has very bad performance and can be quite limiting in advanced mapping – which it should have been best for!). This website StackOVerflow itself runs on top of it!!! I used both in this company (EF after L2S) and hated both. The decision for new projects remained LINQ to SQL, and doing all we can to overcome it’s limitations. You can work around it to do SEMI-POCO (you still need to use some L2S related types when it comes to associations).

    I also do some small projects at home. Since I nolonger have LLBLGen license, I decided to learn NHibernate and use it along with Fluent NHibernate and LINQ To NHibernate. I have learned through this that NHibernate is VERY strong. It changed how I work by some features like updating DB schema automatically (I never touched the DB almost when using it). LINQ provider (in NHibernate Contrib project) is quite lacking sometimes but there is the unreleased source code of NHibernate itself contains a better LINQ provider (haven’t tried it yet). The “Session” in NHibernate has problems when you are doing web development similar to those related to DataContext in L2S or ObjectContext in EF (LLBLGen doesn’t suffer from those thanks to self tracking entities).

    The biggest problems I had with NHibernate though was ability to find information. Too many pieces that should be put together in certain way and not much guidance can include advanced information for both mapping and querying. If not I had a friend (Tuna Toksoz , @tehlike on twitter) who happended to be a committer in NHibernate project source code, I’d really be in serious trouble.

    The moral I learned was: If you want something that just works and a bit basic use Linq To Sql or SubSonic, if you want something in the middle and your production environment can afford BETA .NET version (given golive exists) use Entity Framework 4.0, if you want something very powerful and can afford the hard learning process go to NHibernate, AND, BEST OF ALL, if you can afford LLBLGen, USE IT.

    Let me know your own thoughts on the topic.

    Listen to me talking about ORM in Arabic – DotNetArabi Podcast

    Emad Ashi (@splashup on twitter) interviewed me in the 5th episode of his first Arabic podcast series DotNetArabi to talk about Object Relational Mapping in .NET in Arabic.
    السلام عليكم
    أصدقائي العرب ممن يتابعون هذه المدونة.. يسعدني أن أعلن عن أول حديث لي على الانترنت – و كذلك أول حديث لي على الانترنت بالعربية، عن الـ Object Relational Mappers – ORMs
    شكرا جزيلا لـ “عماد العشي” (splashup@ على تويتر) على استضافته لي في موقع “دوت نت عربي DotNetArabi”، و هو موقع عربي يتضمن لقاءات صوتية مع العديد من المطورين في مجال الدوت نت، تماما على غرار DotNetRocks ، HanselMinutes و سواها، و هو في حد ذاته فخر لي أن أكون ضيفا للحلقة الخامسة في برنامج كهذا، خاصة عندما يكون ضيف أولى الحلقات هو عمر قعدان (omarq@ على تويتر).

    الحلقة 5: محمد مليجي يتكلم عن الـ ORM (Object Relational Mapping)

    وصف الحلقة من دوت نت عربي:
    محمد مليجي تكلم عن الـ ORM (Object Relational Mapping) و هي برامج مساعدة تستطيع من خلالها نقل المعلومات و تحويلها من طبيعة قاعدة البيانات إلى طبيعة البرامج المبنية بأسلوب الـ Object Oriented. حلقة غنية بالتفاصيل و المعلومات القيمة جدا.
    Listen to the episode now (in Arabic):
    الشكر خاص جدا لعماد الذي أتى لي في وقت قل فيه اتصالي بمجتمع المطورين في مصر، و اقتصر نشاطي في مجتمع المطورين بصفة عامة على بعض المحاضرات خارج نطاق الالتزامات الرسمية في شركتي الحالية، و مشاركات قليلة في المنتديات و المجموعات البريدية خاصة ALT.NET و مدونتي هذه، (و هو ما يقل مع الوقت) و متابعة أخبار المجتمعات العربية على تويتر كـ Mohamed_Meligy@ و على Google Reader.

    Related Links:

    [Link List] Recent (and non recent) Articles on Various Topics

    Allow me to quote here some emails I sent to the the Dot NET developers group in my company, Injazat, or, as we call ourselves, Ninjazat. I thought it’ll be useful to share some with you as well.

    ·         ASP.NET MVC – 20 Hours of FREE Video Tutorials

    ·         LINQ FAQ

    o   LINQ FAQ for Newbie’s

    o   LINQ FAQ Part 2

    ·         How we handle application configuration

    ·         ScottGu ASPNETMVC Session Video Available Now (Part 1/2 & 2/2)

    ·         Web Validation: Best Practices and Tutorials

    ·         Building a Single Sign On Provider Using ASPNET and WCF

    o   Part 1

    o   Part 2

    o   Part 3

    ·         NxtGenUG Cambridge: Creating extendable applications using MEF slides and code

    ·         Dynamic Languages and .NET – Developer Day Scotland slides and code samples

    ·         patterns & practices: Data Access Guidance (VS 2010 Stuff)

    ·         Refactoring challenge

    o   Part 1 – Examination

    o   Part 2 – Preparation

    ·         LINQ is not LINQ To SQL

    ·         Visual Studio Programmer Themes Gallery

    ·         Connecting to Mailbox on Gmail and Fetching List of Emails using POP3 and ASP.NET

    ·         Optimize your page for UpdatePanel

    ·         CMS in the .NET World

    ·         Getting jQuery Goodness into ASP.NET AJAX – Take II

    ·         What to do when SELECT query is Running Slowly?


    As usual most of them coming from my Google Reader Shared Items

    Previous Email:

    ASP.NET AJAX Control Toolkit ModalPopupExtender Control in Action

    Previous Email:

    [jQuery] Animate Image Filling Up Using jQuery

    Make Sure To See The Demo First:

    See the article then:


    Have Fun!


    YQL – Yahoo Query Language

    Just in case you missed the news, Yahoo has created it’s own query thingy. No, no LINQ provider, it’s a “query language”. Check it out!


    What is YQL?

    Yahoo! makes a lot of structured data available to developers, primarily through its web services. These services require developers to locate the right URLs and documentation to access and query them which can result in a very fragmented experience. The YQL platform provides a single endpoint service that enables developers to query, filter and combine data across Yahoo! and beyond. YQL exposes a SQL-like SELECT syntax that is both familiar to developers and expressive enough for getting the right data. Through the SHOW and DESC commands we enable developers to discover the available data sources and structure without opening another web browser.

    How Do I Get Started?
    1. Check out the YQL Console.
    2. Read how to access YQL from your application.
    3. Get your Access Keys to sign your requests if you need them.
    Usage limits

    YQL has the following API usage restrictions:

    Per application limit (identified by your Access Key):
    • 100,000 calls per day.
    Per IP limits:
    • /v1/public/* 1000 calls per hour
    • /v1/yql/* 10000 calls per hour

    All rates are subject to change. In addition, you may also be subject to the underlying rate limits of other Yahoo and 3rd party web services.

    Try the console Read Documentation Download PDF Documentation



    You can also check this for more details.


    Yes, I’m thinking about a LINQ provider to encapsulate this.


    Happy YQLing ;)


    Typed Eager Loading Using Entity Framework (& What is Eager Loading vs Deferred Loading)

    If you don’t know what eager loading is, Jump to “What’s eager loading?”.

    Eager Loading Syntax

    If you are eager loading Products for example in a typical (Categories 1<->* Products) relation, the standard syntax would like:


    What is the problem with that?

    The “Products” part. The word “Products” is a string. If I rename the Products table to ShopProducts or whatever or even remove it from this data diagram and have it elsewhere, or even something wrong happens and the relation is removed from DB/diagram by mistake, my code will still compile, but will fail when it runs. This is BAD BAD BAD.

    How to solve this?

    Since I always believe that if something exists somewhere you shouldn’t do it yourself unless its totally broken (and I mean REALLY REALLY BROKEN), I started searching inside the Entity Framework itself for something to get the entity name from.

    At first it seemed super easy. Every entity class has a static property “EntityKeyPropertyName”, so, I thought I can write something like:

    DbDataContext.Categories.Include(Product.EntityKeyPropertyName); // But this didn’t work

    Where Product is the entity class generated for table “Products”. Note that singularizing the name (Products becomes Product) does not happen automatically like in Linq-To Sql, you’ll have to change it manually, which is not required for the code here of course.

    As you an see n the comment, this didn’t work. The value of property was always “-EntityKey-”, the default value of the abstract class “StructuralObject” which all entity classes inherit.

    I kept searching all over until I found that the only place I can get the name from was an Attribute generated on the class somewhat like this:

    [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName=”DatabaseNameFlowModel”, Name=”Products”)]

    My requirement was simple. if the diagram has something wrong that the relation between ParentTable and ChildTable tables but not about the entity classes themselves, My code should not still compile and fail on run. I need to use some code that depends on the relation so that if something is wrong with the relation this code fails early and I know about it the problem the next time I build the VS project.

    The Final Solution

    I tried badly to get the entity name from the API, after frustration, I ended up writing this code:

    <span class="cb1">using</span> System;

    <span class="cb1">using</span> System<span class="cb2">.</span>Collections<span class="cb2">.</span>Generic;

    <span class="cb1">using</span> System<span class="cb2">.</span>Data<span class="cb2">.</span>Objects;

    <span class="cb1">using</span> System<span class="cb2">.</span>Data<span class="cb2">.</span>Objects<span class="cb2">.</span>DataClasses;

    <span class="cb1">using</span> System<span class="cb2">.</span>Linq<span class="cb2">.</span>Expressions;

    <span class="cb1">using</span> System<span class="cb2">.</span>Reflection;

    <span class="cb1">namespace</span> Meligy<span class="cb2">.</span>Samples<span class="cb2">.</span>EntityFramework


        <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb1">class</span> <span class="cb3">LinqExenstions</span>


            <span class="cb4">//Used for child entities.</span>

            <span class="cb4">//Example: Order.Customer</span>

            <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb3">ObjectQuery</span><span class="cb2"><</span>T<span class="cb2">></span> Include<span class="cb2"><</span>T<span class="cb2">></span>(<span class="cb1">this</span> <span class="cb3">ObjectQuery</span><span class="cb2"><</span>T<span class="cb2">></span> parent,

                                                    <span class="cb3">Expression</span><span class="cb2"><</span><span class="cb5">Func</span><span class="cb2"><</span>T, <span class="cb3">StructuralObject</span><span class="cb2">>></span> expression)

                <span class="cb1">where</span> T : <span class="cb3">StructuralObject</span>


                <span class="cb1">return</span> Include(parent, (<span class="cb3">LambdaExpression</span>) expression);


            <span class="cb4">//Used for child collections of entities.</span>

            <span class="cb4">//Example: Order.OrderLines</span>

            <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb3">ObjectQuery</span><span class="cb2"><</span>T<span class="cb2">></span> Include<span class="cb2"><</span>T<span class="cb2">></span>(<span class="cb1">this</span> <span class="cb3">ObjectQuery</span><span class="cb2"><</span>T<span class="cb2">></span> parent,

                                                    <span class="cb3">Expression</span><span class="cb2"><</span><span class="cb5">Func</span><span class="cb2"><</span>T, <span class="cb3">RelatedEnd</span><span class="cb2">>></span> expression)

                <span class="cb1">where</span> T : <span class="cb3">StructuralObject</span>


                <span class="cb1">return</span> Include(parent, (<span class="cb3">LambdaExpression</span>) expression);


            <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb3">ObjectQuery</span><span class="cb2"><</span>T<span class="cb2">></span> Include<span class="cb2"><</span>T<span class="cb2">></span>(<span class="cb3">ObjectQuery</span><span class="cb2"><</span>T<span class="cb2">></span> parent,

                                                     <span class="cb3">LambdaExpression</span> expression)

                <span class="cb1">where</span> T : <span class="cb3">StructuralObject</span>


                <span class="cb4">//There must be only one root entity to load related entities to it.</span>

                <span class="cb1">if</span> (expression<span class="cb2">.</span>Parameters<span class="cb2">.</span>Count <span class="cb2">!=</span> <span class="cb6">1</span>)


                    <span class="cb1">throw</span> <span class="cb1">new</span> <span class="cb3">NotSupportedException</span>();


                <span class="cb4">//We'll store entity names here in order then join them at the end.</span>

                <span class="cb1">var</span> entityNames <span class="cb2">=</span> <span class="cb1">new</span> <span class="cb3">List</span><span class="cb2"><</span><span class="cb1">string</span><span class="cb2">></span>();

                <span class="cb4">//We split the calls ... Entity.MemberOfTypeChild.ChildMemberOfChildMember etc..</span>

                <span class="cb4">//Example: (Order ord) => ord.Customer.Address</span>

                <span class="cb1">string</span>[] childTypesMembers <span class="cb2">=</span> expression<span class="cb2">.</span>Body<span class="cb2">.</span>ToString()<span class="cb2">.</span>Split(<span class="cb7">'.'</span>);

                <span class="cb4">//Get the root entity type to start searching for the types of the members inside it.</span>

                <span class="cb4">//In prev. example: Find: Order</span>

                <span class="cb3">Type</span> parentType <span class="cb2">=</span> expression<span class="cb2">.</span>Parameters[<span class="cb6">0</span>]<span class="cb2">.</span>Type;

                <span class="cb4">//entityNames.Add(GetEntityNameFromType(parentType));</span>

                <span class="cb4">//The first word in the expression is just a variable name of the root entity. </span>

                <span class="cb4">//  Skip it and start next.</span>

                <span class="cb4">//In example: First part is: ord</span>

                <span class="cb1">for</span> (<span class="cb1">int</span> i <span class="cb2">=</span> <span class="cb6">1</span>; i <span class="cb2"><</span> childTypesMembers<span class="cb2">.</span>Length; i<span class="cb2">++</span>)


                    <span class="cb1">string</span> memberName <span class="cb2">=</span> childTypesMembers[i];

                    <span class="cb4">//Get the member from the root entity to get its entity type.</span>

                    <span class="cb3">MemberInfo</span> member <span class="cb2">=</span> parentType<span class="cb2">.</span>GetMember(memberName)[<span class="cb6">0</span>];

                    <span class="cb4">//We cannot get the type of the entity except by knowing</span>

                    <span class="cb4">//  whether it's property or field (most likely will be property).</span>

                    <span class="cb4">//Bad catch in the reflection API? Maybe!</span>

                    <span class="cb3">Type</span> memberType <span class="cb2">=</span> member<span class="cb2">.</span>MemberType <span class="cb2">==</span> <span class="cb8">MemberTypes</span><span class="cb2">.</span>Property

                                          <span class="cb2">?</span> ((<span class="cb3">PropertyInfo</span>) member)<span class="cb2">.</span>PropertyType

                                          : ((<span class="cb3">FieldInfo</span>) member)<span class="cb2">.</span>FieldType;

                    <span class="cb4">//Add the eneity name got from entity type to the list.</span>

                    entityNames<span class="cb2">.</span>Add(GetEntityNameFromType(memberType));

                    <span class="cb4">//The next member is belong to the child entity, so,</span>

                    <span class="cb4">//  the root entity to seach for members should be the child entity type.</span>

                    parentType <span class="cb2">=</span> memberType;


                <span class="cb4">//Join the entity names by "." again.</span>

                <span class="cb1">string</span> includes <span class="cb2">=</span> <span class="cb1">string</span><span class="cb2">.</span>Join(<span class="cb7">"."</span>, entityNames<span class="cb2">.</span>ToArray());

                <span class="cb4">//Simulate the original Include(string) call.</span>

                <span class="cb1">return</span> parent<span class="cb2">.</span>Include(includes);


            <span class="cb1">private</span> <span class="cb1">static</span> <span class="cb1">string</span> GetEntityNameFromType(<span class="cb3">Type</span> type)


                <span class="cb4">// We didn't just use the Entity type names because maybe</span>

                <span class="cb4">//  the table is called Orders and the class is Order or OrderEntity.</span>

                <span class="cb1">if</span> (type<span class="cb2">.</span>HasElementType) <span class="cb4">//For arrays, like: OrderLines[]</span>


                    <span class="cb4">//The type of the element of the array is what we want.</span>

                    type <span class="cb2">=</span> type<span class="cb2">.</span>GetElementType();


                <span class="cb1">else</span> <span class="cb1">if</span> (type<span class="cb2">.</span>IsGenericType) <span class="cb4">// for collections, like: EntityCollection<OrderLines></span>


                    <span class="cb1">var</span> genericClassTypeParameters <span class="cb2">=</span> type<span class="cb2">.</span>GetGenericArguments();

                    <span class="cb4">//The generic class must have one entity type only to load it.</span>

                    <span class="cb1">if</span> (genericClassTypeParameters<span class="cb2">.</span>Length <span class="cb2">!=</span> <span class="cb6">1</span>)

                        <span class="cb1">throw</span> <span class="cb1">new</span> <span class="cb3">NotSupportedException</span>();

                    <span class="cb4">//The type of the element of the collection is what we want.</span>

                    type <span class="cb2">=</span> genericClassTypeParameters[<span class="cb6">0</span>];


                <span class="cb4">//Get the attributes that have the entity name in them.</span>

                <span class="cb1">var</span> entityTypeAttributes <span class="cb2">=</span>

                    type<span class="cb2">.</span>GetCustomAttributes(<span class="cb1">typeof</span> (<span class="cb3">EdmEntityTypeAttribute</span>), <span class="cb1">true</span>) <span class="cb1">as</span> <span class="cb3">EdmEntityTypeAttribute</span>[];

                <span class="cb4">//Make sure there IS one and ONLY one attribute to get the only entity name.</span>

                <span class="cb1">if</span> (entityTypeAttributes <span class="cb2">==</span> <span class="cb1">null</span> <span class="cb2">||</span> entityTypeAttributes<span class="cb2">.</span>Length <span class="cb2">!=</span> <span class="cb6">1</span>)

                    <span class="cb1">throw</span> <span class="cb1">new</span> <span class="cb3">NotSupportedException</span>();

                <span class="cb4">//Return the entity name.</span>

                <span class="cb1">return</span> entityTypeAttributes[<span class="cb6">0</span>]<span class="cb2">.</span>Name;




    This enables you to write:

    DbDataContext.Categories.Include( (cat)=> cat.Prodycts);


    DbDataContext.Prodycts.Include( (prod)=> prod.Category);

    According to your need.

    For things like: Order.Customer.Address (multiple levels), you’ll have to write code like:

    DbDataContext.Orders.Include( order => order.Customer ).Include( customer => Customer.Address );

    What’s Eager Loading? (in case you don’t know)

    Let’s say you have tables Products, and Categories with relation 1<->* between them (Any category has many products; one product has one category). Let’s say you want to display a page of all products grouped by categories. Something like the following list but with much more information of course:

      • Category 1
        • Product A
        • Product B
        • Product C
      • Category 2
        • Product X
        • Product Y
        • Product Z

    If you are using some ORM / Code generator that creates for you classes like “Product”, “Category” and gives you properties like “myCategory.Products” , “myProduct.Category”, how would you create such page?

    Normally you’ll put a repeater or such for products inside a repeater for categories.


    The products repeater will have its data source set to the current category item of the Categories repeater, something like “( (CategoryEntity)Container.DataItem ).Products”. Fine with that? Familiar?

    OK. Now, if the code generator that generated the “Products” property has something like that:

    public List<PRoduct> _Products;

    public List<PRoduct> Products




    if (_Products == null)


    _Products = (from products in DB.Products

    where products.CategoryID == this.ID

    select products)



    return _Products;



    • Nevermind the LINQ syntax. It’s just like writing “SELECT * FROM [Products] WHERE …” with all the dataset/datareader stuff.

    Lazy Loading (AKA. Deferred Loading)

    If the generated code (or your code) looks like this, this means that that for every category in the database, you’ll have a separate DB call to get the products of this category.

    It also means that the products of each category will not be loaded until someone writes code that calls the getter of the Products property. That’s why this style of coding (not loading the related objects until they’re asked to be loaded) is called Lazy Loading.

    This is good for a single category where you may be seeking just the basic information of the category and will not try to load products, since then they will not be requested when you don’t ask for it.

    However, this is very bad for our example page. Because it means that a new DB call for each category. Imagine that you have 20 or 50 or 100 Category there, this will give you how many DB calls? (Hint: N Categories + 1 call for the category list itself).

    Eager Loading

    What if the code in the getter above was in the constructor?. Imaging something like:

    public Category(int categoryID)


    // Code that laods category info from DB. Does not matter here.

    //Could be in factory method or such. Not our topic

    _CategoryID = categoryID;

    // …. …. …. Continue Loading Logic

    //The important part

    _Products = (from products in DB.Products

    where products.CategoryID == this.ID

    select products)



    This is good in case you know that in every situation when you use the category, the Products will be needed. This is probably not useful in a Product/category Scenario but think of a Person table and Address table where most likely whenever you load a Person you’re going to load his Addresses.

    This is also useful especially when using ORM/code generator as in the first example. Lets get back to the Repeater example. If you use Entity framework or similar ORM, and you set the Categories query to load the Products eager loading (meaning each Category is created with its Products loaded already), Entity Framework can have a single connection and only TWO database hits, one for the Categories, and one for the Products. This is very useful in many listing scenarios. It also help especially when you have many parent objects (say Categories) or if the parent object needs to load entities of many different classes (say User needs to load Roles and Permissions and Personal Information and History and …. (if such case is applicable for you of course.

    Now that you know what eager loading is, you can go up and check how the Entity Framework does that.

    LINQKit: “C# 3.0 In A Nutshell” Extras

    The "C# 3.0 in a nutshell" book has some neat free extras that are worth mentioning for those who haven’t already heard of (they have been released for long). Those are like must-have LINQ tools and helpers.

    The homepage of LINQKit (the major part of the extras I’m going to cover here) provides great information and short code samples about the components:

    • LINQPad This is a snippet compiler (application to run/try small codes in separation than big VS projects, like this), that’s customized for LINQ queries. You can use the SQL Server Query Analyzer -like application to try your codes on the tables in the DB, and see the resultant output and SQL query. Very useful for learning LINQ and/for for making up complex LINQ Queries

    • PredicateBuilder

      This is a dynamic query library. If you ever tried LLBLGen (one of the best .NET OR mappers), it’s equivalent to PredicateExpression and PredicateFactory stuff. If not, then it’s a dynamic way of creating queries. Sometimes you want to add/remove conditions to/from your query based on runtime decisions, and you can’t do that with the standard query way, so, this library allows you to write code like this:

      var predicate = PredicateBuilder.False<Product>();
      <span class="kwrd">foreach</span> (<span class="kwrd">string</span> keyword <span class="kwrd">in</span> keywords)
          <span class="kwrd">string</span> temp = keyword;
          predicate = predicate.Or(p => p.Description.Contains(temp));

      Of course you could wrap the ".And" and ".Or" parts in an "if" statement based on any check you like, which is the real power here!

    • Improvements / Workarounds

      When you deal with LINQ much enough, you’ll get many limitations (as everything else :D). For example, sometimes you cannot set conditions on related entities (like setting conditions on the "Pruchase"s of a "Customer". Also, you’ll want to do more with the expressions you use, mainly combine two different expressions together, use advanced queries with related entities, doing more stuff asynchronously, etc. The LINQKit has many small extension methods you use in your normal query or your dynamic query to work around that and more.

    • LINQBridge

      If you find that you have to work on some .NET 2.0 project, it doesn’t mean you lose LINQ. LINQBridge allows you to write queries like this one.

      <span class="kwrd">int</span>[] numbers = { 5, 15, 7, 12 };
      var query = numbers
        .Where (n => n > 10)
        .OrderBy (n => n)
        .Select (n => n * 10);

    Check out the extras page for more code samples and the library download with demo solution and library source code.

    Technorati Tags: C#,LINQ,LINQPad,C# 3.0