How do the pros write code?

Thu, 17 Jan 2008 23:06:53 GMT by madd0

reference_code

Find out by taking a look at the source code of the .NET Framework that, as Scott Guthrie announced today, is now available for debugger access from within Visual Studio 2008.

For now, the libraries that are available are:

  • .NET Base Class Libraries (including System, System.CodeDom, System.Collections, System.ComponentModel, System.Diagnostics, System.Drawing, System.Globalization, System.IO, System.Net, System.Reflection, System.Runtime, System.Security, System.Text, System.Threading, etc).
  • ASP.NET (System.Web, System.Web.Extensions)
  • Windows Forms (System.Windows.Forms)
  • Windows Presentation Foundation (System.Windows)
  • ADO.NET and XML (System.Data and System.Xml)

They are released under a Reference License that basically lets you take a look at the code, but you cannot reuse it for your own projects. You still get access to the full source, including comments, which will hopefully give you a better understanding on how the Framework works. To learn how to configure Visual Studio to download the Framework’s source, that a look at Shawn Burke’s tutorial.

Reading other people’s code is an excellent way of becoming a better programmer, as has been discussed by people such as Scott Hanselman. Reading the .NET Framework’s code is one way to do it, but don’t forget that there’s plenty of code lying around on the Internet; some of it is even open source. Sites like Codeplex or Sourceforge are great places to start.

 

(Cross-post from Channel 8.)

Visual Studio 2008 available for MSDN subscribers

Mon, 19 Nov 2007 10:50:40 GMT by madd0

Wow, it's been a while... Hopefully, now that I'm putting the finishing touches on the project that's been taking up all of my time, I'll be able to write more around here.

I'm starting with good news! At least for those lucky enough to have access to a MSDN subscription. As it was announced on Friday, the final version of Visual Studio 2008 is available for download as of this morning!

vsts2008

Slightly under 4GB, the ISO for Visual Studio Team System 2008 Team Suite (32 and 64-bit version) DVD is just waiting to be downloaded. Go get it quick before people actually start waking up and download speeds are affected ;)

Take a look at the .NET Framework's innards!

Wed, 03 Oct 2007 19:26:55 GMT by madd0

What am I talking about? Well, haven't you heard? Microsoft is releasing the source code for the .NET framework libraries!

I didn't say it, Scott Guthrie did.

Actually, this has been in planning for a couple of years now, and it has finally happened.

The code is being released under Microsoft Reference License (MS-RL), which is basically the equivalent of a "Do not touch!" sign in front of a painting in a museum. But, hey, that's fine with me. I just want to look at it, appreciate it, learn from it: it will be just like visiting the Louvre of source code... At first we'll get the Base Class Library (BCL), ASP.NET, Threading, Windows Forms, WPF, ADO.NET and XML. Other libraries, such as WCF, WF and LINQ will get there later.

Finally, as if getting the source code (comments included, by the way) wasn't enough, integrated debugging with Visual Studio 2008 will also be supported, which means that VS will be able to automatically download the code as needed when you debug your code. For more details on this, visit Scott's blog.

For more info in general, you might want to listen to this interview recorded by Scott Hanselman last week (working at MS has its perks).

You cannot always be that optimistic

Sun, 15 Jul 2007 12:24:15 GMT by madd0

I'm talking about optimistic concurrency of course.

As I've said before, my blog now runs on a new engine. To make a long story short, I lost my old blog, I wanted to play with the newest Microsoft technologies, so I picked up the engine my friend Patrice is working on that uses ASP.NET, C# 3.0 and LINQ (and a sprinkle of ASP.NET AJAX here and there). And it's great! What's more, I now have material for blogging.

So, what am I going to start with? If "optimistic concurrency" and the paragraph above didn't give you a hint, I'm going to talk about LINQ. LINQ to SQL to be precise, or DLINQ, however you want to call it.

Let's start from the beginning. What is LINQ?

LINQ, a codename that stands for Language Integrated Queries, is a set of extensions for C# and Visual Basic that gives these languages a native syntax to perform queries. What kind of queries? Well, for now, it can be anything from objects (LINQ to Object), XML (LINQ to XML) or databases (LINQ to SQL and LINQ to Entities).

And what does it look like? Here's an example in C#:

   1:  var query = from p in db.Posts 
   2:              orderby p.date_post descending 
   3:              select new {
   4:                         Title = p.title_post,
   5:                         Description = p.text_post,
   6:                         Date = p.date_post,
   7:                         p.url_post,
   8:                         Tags = p.PostsTags,
   9:                         CommentCount = p.PostsComments.Count };

Believe it or not, the code above is C#. And what am I doing? Well, I'm not going into detail, but I'm querying my database to retrieve blog posts. The query is stored in a variable of a type inferred from the operation on the right of the assignment and the results are going to be objects of an anonymous type that will contain the properties between brackets. Since this is DLINQ I'm using, the code above is automatically converted into SQL to be executed on SQL Server (yes, this is the only server currently supported.)

And what about this "optimistic concurrency" thing? What is it and what does it do?

Optimistic concurrency is a model used for updating databases in multi-user environments. It basically consists in comparing one or more fields of the record to be updated to verify that it hasn't been modified by somebody else since the last time it was accessed. This is a perfectly valid approach that can be used with ADO.NET and LINQ.

So, what's the problem?

The code above works with classes that were generated with the new LINQ designer that comes with Visual Studio Orcas, or should I say 2008. And this code works well for retrieving all posts in the database, but the problems start when I try to update a record using code like this:

   1:  var query = from p in db.Posts
   2:              where p.id_post == int.Parse(postid)
   3:              select p;
   4:  Post originalPost = query.FirstOrDefault();
   5:   
   6:  originalPost.title_post = newPost.title;
   7:  originalPost.text_post = newPost.description;
   8:   
   9:  db.SubmitChanges();

I was getting the error "SQL Server does not handle comparison of NText, Text, Xml, or Image data types" and I couldn't understand why.

From the error message above, I could guess that there was a problem with the generated query that compared all fields in my table before updating for optimistic concurrency. However, if you take a look at what the designer says, this shouldn't be the case, at least not for the only NText column:

 

dlinq_designer_update_check

Ah, but take a look at the generated code and what will you find?

global::System.Data.Linq.Column(Storage="_text_post", Name="text_post", 
 DBType="NText NOT NULL", CanBeNull=false)]
public string text_post {
    // ...
}

Well, it's not what you find that's a problem actually, it's what's missing: we're missing an attribute on the property:

global::System.Data.Linq.Column(Storage="_text_post", Name="text_post",
 DBType="NText NOT NULL", CanBeNull=false,
 UpdateCheck=System.Data.Linq.UpdateCheck.Never)]
public string text_post {
    // ...
}

Now the NText column will not be compared and LINQ will generate a proper query.

Compiler pour le Framework 1.1 sous Visual Studio 2005

Tue, 03 Jan 2006 10:22:04 GMT by madd0
Il est possible (on se demande si c'est utile, par contre?) de compiler des projets C# sous Visual Studio 2005 pour le Framework .NET 1.1. Plusieurs articles sur le Web expliquent comment le faire, mais ils sont en anglais. Je vais me contenter de traduire (plus ou moins fidèlement) ce post de Jomo Fisher, developpeur dans la team MSBuild.
  1. Copiez le fichier mis à disposition ici (ou téléchargeable ici) dans le dossier C:\Program Files\MSBuild\. Appelez-le CrossCompile.CSharp.targets.
  2. Ouvrez le fichier projet (*.csproj) du projet C# que vous souhaitez compiler pour le Framework 1.1.
  3. Cherchez la balise Import et remplacez son attribut Project par : Project="$(MSBuildExtensionsPath)\CrossCompile.CSharp.targets"
  4. Chargez le projet dans Visual Studio 2005.
  5. Le programme vous affichera un message d'avertissement vous indiquant que le fichier de projet a été modifié. Indiquez que vous souhaitez ouvrir le projet en mode normal.
  6. Dans le menu déroulant qui affiche 'Any CPU' (à côté du bouton 'Start Debugging') choisissez l'option 'Configuration Manager'.
  7. Dans la fenêtre qui s'ouvre, sous la colonne 'Platform', choisissez '.NET 1.1' et fermez la fenêtre.
  8. Pour vérifier que vous compilez bien pour la version 1.1 du Framework, essayez de compiler un projet qui contient des spécificités de la version 2, par exemple: using System.Collections.Generic, ou public partial class...