November 2007 - Posts

We just released the CTP of the Microsoft Parallel Extensions to .NET Framework 3.5 (remember PLINQ?). You can grab the bits over here.

Stay tuned for more technical posts on it. Enjoy!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Within a few seconds from now I'll hit the "Publish This Project" button in CodePlex. Back in the early Orcas ages (beta 1 and before) I cooked up this pretty simple example showing how to create custom LINQ query providers. As a matter of fact, it was the result of a blog series back in April entitled "The IQueryable tales". Here are the pointers but keep in mind things have changed in the meantime:

More specifically, the API to write custom LINQ query providers has changed a bit. Most notably is the change that happened in Orcas Beta 2 refactoring the IQueryable<T> interface into IQueryable<T> and IQueryProvider. The new design might be slightly harder to implement (or to wrap your head around initially) but it certainly helps to separate concerns (something that's "queryable" versus the "construction work" that needs to be performed when building up the expression tree). If time permits, I'll reiterate the blog series based on the Orcas RTM APIs.

Now renamed as LINQ to Active Directory (or short: LINQ to AD), this sample allows to write things like this:

var users = new DirectorySource<User>(ROOT, SearchScope.Subtree);
users.Log = Console.Out;

var res = from usr in users
          where usr.FirstName.StartsWith("B") && usr.Office == "10/2525"
         
select new { Name = usr.FirstName + " " + usr.LastName, usr.Office, usr.LogonCount };

foreach (var u in res)
{
   Console.WriteLine(u);
   u.Office = "10/5252";
   u.SetPassword(pwd);
}

users.Update();

That's right: we have an entity model in place (see samples in the release for more info), allow to do nice mappings (e.g. Office stands for "physicalDeliveryOffice" in AD's User object class), have a simple API that allows adding methods on entities (e.g. SetPassword is a one-line piece of code) and there's update support (change properties as much as you want, just call Update on the DirectorySource object to feed changes back). Cool huh?

So, here it is NOW: LINQ to AD. Enjoy!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

A while ago I posted about this new C# 3.0 feature. Although the IL code (aargh, yet another of these posts :-)) in that post reveals how object initializers are implemented internally, I wanted to make it slightly more explicit over here to avoid confusion.

Consider the following C# 3.0 fragment:

using System;

class Oini
{
   public static void Main()
   {
      Customer c = new Customer() { Name = "Bart", City = "Redmond", Age = 24 };
   }
}

This is not equivalent to:

using System;

class Oini
{
   public static void Main()
   {
      Customer c = new Customer();
      c.Name = "Bart";
      c.City = "Redmond";
      c.Age = 24;
   }
}

It is equivalent to the following however:

using System;

class Oini
{
   public static void Main()
   {
      Customer __t = new Customer();
      __t.Name = "Bart";
      __t.City = "Redmond";
      __t.Age = 24;
      Customer c = __t;
   }
}

I know it's subtle but there's a very good reason to do it this way: atomic assignment. Assignments should be read from right to left:

Customer c = new Customer() { Name = "Bart", City = "Redmond", Age = 24 };

means

  1. evaluate the right-hand side (rhs):

    new Customer() { Name = "Bart", City = "Redmond", Age = 24 }
  2. assign it to the left-hand side (lhs):

    Customer c

therefore, we need a temporary object to evaluate the right-hand side first prior to assigning it to the target variable.

One place where this subtlety is quite important is the use of multi-threading. It's a bit of a convoluted sample, I agree, since in multi-threaded circumstances you'll likely have a locking-strategy or so in place to avoid base states but anyhow, here's a sample:

using System;
using System.Threading;

class Customer
{
   public string Name { get; set; }
   public string City { get; set; }
   public int Age { get; set; }
}

class Demo
{
   static Customer c;
   static int n = 0;
   static bool
safe = false;

   static void Main(string[] args)
   {
      safe = args.Length > 0;

      Init();

      AppDomain.CurrentDomain.UnhandledException +=
         delegate(object sender, UnhandledExceptionEventArgs e)
         {
            Console.WriteLine("Invalid state after " + n + " assignments.");
            Environment.Exit(1);
         };

      new Thread(() => Monitor()).Start();

      for (n = 1; ; n++)
         Init();
   }

   static void Init()
   {
      if (safe)
      {
         c = new Customer() {
            Name = "Bart",
            City = "Redmond",
            Age = 24
         };
      }
      else
      {
         c = new Customer();
         c.Name = "Bart";
         c.City = "Redmond";
         c.Age = 24;
      }
   }

   static void Monitor()
   {
      while (true)
      {

         if (c.Name != "Bart" || c.City != "Redmond" || c.Age != 24)
           
throw new Exception("Bad state detected");
      }
   }
}

If you execute this application without command-line arguments it should fail pretty quickly. However if you run it with some parameter, safe will be set to true, which causes Init to use the C# 3.0 implementation of object initialization, which is atomic. So we shouldn't get in intermediate states at all.

image

Here's the reverse translation of the Init body using Reflector:

image

If you understand this sample, you know why atomic assignment matters (and that's a good thing :-)). To pinpoint the core problem, consider the following fragment of Init (in the safe-negative case):

         c = new Customer();
         c.Name = "Bart";
         c.City = "Redmond";
         c.Age = 24;

If the monitoring thread gets a chance to kick in somewhere in the middle of executing these four statements, the state is "corrupted". Obviously you can fix this by putting a lock in place (small exercise) but it's nice to know the atomic character of object initializers in C# 3.0 anyhow.

Enjoy!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

I just released the RTM bits of LINQSQO on CodePlex. This project was one of my first LINQ-related ones and goes back to the early PDC 05 days where LINQ was first shown to the world. The project contains a custom implementation of the LINQ Standard Query Operators used in LINQ. More specifically, it's a clone of the Enumerable static class and all its extensions methods you can find in the System.Linq namespace in System.Core.dll. Thus, you can see it as an equivalent of LINQ to Objects. The primary goal of the project is to give developers a better insight about how LINQ works. Together with the sources we ship a total of 148 unit tests to do some basic testing of all the query operators in there.

Enjoy!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

It's my pleasure to tell you we shipped VS2008 and .NET FX 3.5 today. Soma has all the details on his blog. Although I didn't work actively on this release - Orcas was pretty much done at the time I joined - I'm really excited about this next milestone in our .NET journey. Things I definitely like in this release are:

  • LINQ from both the language side (C# 3.0, VB 9.0) and the framework side (LINQ to Objects, SQL, XML, DataSets)
  • Lots of tools improvements for WPF, WCF and WF development
  • The new System.Addins namespace and various other framework improvements (e.g. named pipes support)
  • ASP.NET AJAX support

But there's so much more to talk about. Later this month, I'll post updates for LINQ to LDAP and LINQ to SharePoint, and maybe I'll reveal another related pet project I've been working on lately. So stay tuned and enjoy VS2008 and .NET FX 3.5!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Back from TechEd EMEA Developers 2007. It's been a great success once more and it was my pleasure to deliver a few sessions on WPF Data Binding and PowerShell and MMC 3.0 manageability for developers. Both sessions were well attended and the scores were very nice, although there's always some room for improvement.

I'm glad to announce a few media files with me have been published the last couple of days:

Needless to say, check out the other fishbowl interviews on the TechEd site as well; lots of great topics are covered over there including Windows PowerShell 2.0, VS2008 and .NET Framework 3.5, software security, etc. And of course a bunc of great speakers have their recordings: Rafal Lukawiecki, David Platt, Jeffrey Snover, David Chappell, Pat Helland, and many more. Check it out now!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

More Posts