Wednesday, April 25, 2007

Digging DLINQ

Just gave DLINQ a quick shot and it indeed is quite impressive. The way Table and Column attributes are used for database mapping is similar to that of XML Serialization:

    [Table(Name="Products")]

    public class Product

    {

        [Column]

        public int ProductID { get; set; }

 

        [Column]

        public string ProductName { get; set; }

 

        [Column]

        public decimal UnitPrice { get; set; }

    }

Now, you can use LINQ syntax to perform strong typed database query:

public static void DLinqQuery()

{

    string ConnStr =

        @"Server=localhost;Database=Northwind;";

 

    DataContext ctx = new DataContext(ConnStr);

    Table<Product> prods = ctx.GetTable<Product>();

 

    //this is COOL!

    var list = from p in prods

               where p.UnitPrice > 30

               select p;

 

    foreach (var p in list)

    {

        Console.WriteLine("{0,5}\t{1,-32}\t{2,-6}",

            p.ProductID, p.ProductName, p.UnitPrice);

    }

}


Pretty neat, agree? I only wish this were available eailier so that I didn’t have to deal with typed dataset. That was aweful… Some further point of interests:

  • Is the underlying SQL generated by DLINQ effecient? (IQueryable and the expression tree do deserve their own blog entries).
  • Mapping between foreign key relationship to object graphs.
  • Stored Proc and Transaction support