Cottleston Pie

Fernando Felman’s thoughts on software development

Archive for March, 2007

MSN Messenger History Merger

Posted by Fernando Felman on March 21, 2007

As a consultant I’ve been sent to customers’ sites quite a lot. Many times I’m provided with a dedicated desktop for my work and I’m not allowed to plug my own laptop. This is usually no biggie as I’ve a USB flash drive that allows me to keep in sync all my settings, preferences, etc.

Saying that, there is one thing which I cannot keep in sync: the history files of the MSN Messenger. MSN Messenger saves conversations history in XML files – one file per recipient. Those files get updated every time a conversation is compete, e.g. every time the Messenger window is closed. This implementation means that each desktop saves its own history files and merging those history files is not an easy task. I tried to find a merging utility in the internet with no success so I’m left without options: I must implement the merging utility myself.

So, this is a long introduction to my latest open source project: the MSN History Merger. This utility will merge MSN Messenger history files from different instances into one complete history folder. The executable file can be downloaded from the Release page (no installation is required).

I do hope you’ll find it useful. If you do, please leave me a comment. Suggestions are also welcomed so feel free to use the discussion lists and the issues tracker to log suggestions and issues.

Posted in .Net v2 | 40 Comments »

Visual Studio keyboard shortcuts every developer should know

Posted by Fernando Felman on March 16, 2007

VS has many keyboard shortcuts. Some may say even too many of them. So many that it might intimidate developers to learn and remember. Well, I thought to come up with the most crucial ones, the ones that perform actions that I know many developers use only with the mouse. So here they are: VS keyboard shortcuts to actions you are using every day, but most of you are using with the mouse (and I’m not listing the very obvious everybody knows).

Try to remember at least the highlighted ones – you’ll be amazed how much you’ll use it.

  • Outlining:
    • Ctrl-M, Ctrl-M: Toggle Outlining Expansion
    • Ctrl-M, Ctrl-O: Collapse to Definitions
    • Ctrl-M, Ctrl-L: Toggle All Outlining
  • Editing tools (refactoring, snippets):
    • Ctrl-K, Ctrl-S: Surround with
    • Ctrl-K, Ctrl-X: Insert snippet
    • Ctrl-Shift-V: Clipboard cycling
  • Find & Replace:
    • Ctrl-I: Incremental Search
    • Ctrl-Shift-F: Find in Files
    • Ctrl-Shift-H: Replace in Files
  • Misc:
    • Ctrl- -: Navigate Backward
    • Alt-Shift-F10: Show smart tag
    • Ctrl-K, Ctrl-I: Tool tip info (mimics the mouse hover tool tip)
    • Ctrl-J : List Members (IntelliSense)
    • Ctrl-Space: Auto complete word (IntelliSense or auto complete)
    • Ctrl-Shift-Space: Parameters Info

Posted in VS2005 | 5 Comments »

PLinq – Parallel Linq for multi-CPU environments

Posted by Fernando Felman on March 11, 2007

We all know about Linq – the next C# version object-querying integration language. One of the results of the Metaprogramming approach used by the Linq technology is that you can change the underlying implementation to support more features and capabilities, and doing so without the need to change the original code that is using Linq as a framework. One of such possible upgrades to the Linq infrastructure is PLinq, an internal Microsoft project to implement Parallel Linq targeting multi CPU environments. The PLinq project is explained briefly in Joe Duffy’s blog and on eWeek.

I’m very exiting about Linq. Not just because the domain-specific extensions like Linq to Xml and Linq to SQL allows developers to code the What rather then the How, but also because of the new Metaprogramming possibilities that the Linq infrastructure introduce: lambda, the Expression type for AST (abstract syntax trees) , anonymous types, etc. One very good example of how to use the Metaprogramming tools of C# v3 is Jafar Husain’s blog which explains how to use the Linq infrastructure for code generation.

Posted in Linq | Leave a Comment »

Projex, exercising with WinForms

Posted by Fernando Felman on March 9, 2007

I did quite a bit of SharePoint related projects and I learned that one of the most important things you’ve to know when you’re coding a SharePoint Web Part is the Web Control Lifecycle: what events are being called by the infrastructure, when are they called and on which conditions. To that model you’ve to aggregate the SharePoint-specific lifecycle, such as when the Web Part Communication events are being fired and how does that relate to the regular Web Control lifecycle.

So I can say I know about Web Controls quite a bit. But what about WinForm Controls? I wanted to upgrade my knowledge in the WinForms technology to know more about creating Windows Controls, considerations and restrictions I should be aware of, etc. I wanted to investigate the following key aspects in a Widows Control: how does data binding works (and not just how to use it), what are the best practices for coding controls and what internal methods should I be familiar with.

There’s only one good way to learn all of that: experiencing… So I give you my open source, free to use Projex application. This application allows you to create drawings based on a shape I call the Webex, a spider-web shape combined of two points and a mutual anchor point.
Here is how a single Webex looks like:
Simple Webex

And here are some sample of what the application allows you to do (click to enlarge):
Canvax Sample 4 Canvax Sample 3 Canvax Sample 2

The code is constructed in the following way:

  • ProjectXWin: The Windows application. This application has 2 namespaces:
    • Drawing: contains the Pointex and the Webex classes. Both are mathematical representations of a drawing object and aren’t bound to any Control (so you can use them in any displaying technology, including a windows control).
      • Pointex: represents a single Point with anchoring-aware capabilities. I coded this class to encapsulate the anchoring functionality so when the user moves or resizes the drawing area (the window displaying the drawing), the Pointexes will move to their anchored locations causing the drawing to shrink or grow.
      • Webex: a single spider-web drawing represented in an abstract mathematical way. I coded this class to encapsulate the logic of calculating the lines and the resizing functionality for a single Webex.
    • Windows: contains the WinForm controls. This namespace contains many User Controls representing the various value-editors I needed (like Color selector, Pointex editor, etc.) and also have these 2 Custom Controls:
      • WebexPainter: this control display a single Webex. It was coded to facilitate the task of painting a Webex in the form and binding it to its editors.
      • Canvax: a container for multiple WebexPainters. I coded this class to allow the Webex painters to share the same drawing canvas (the same Graphics object).
  • TestProjex: the unit test project implement in MSTest . I usually use NUnit and I wanted to experience with the Microsoft unit test platform to see which is more suitable for me and my team.

Hope you’ll find this project interesting!
Enjoy, and remember that you can leave comments both on the project main page and on the relevant discussion group.

Posted in .Net v2, VS2005 | Leave a Comment »

Private methods vs. regions

Posted by Fernando Felman on March 8, 2007

Back in the days of VS2003 I had a discussion with a colleague regarding the usage of private methods vs. regions. You’re writing a long method (in our case it was a batch job activity) which will perform many actions / calculation one after the other. The question we were discussing is how to enclose the logical sections in that method, keeping in mind that each section will be executed only once.

My colleague (which from now will be called … hmmm… Adam!) supports the regions approach which means that every logical section is enclosed within a #region. In c# regions are collapsed by default and so when you review the main method code you’ve only to read the titles of the regions to understand what the method does. Should you want to inspect a logical section in more details, just open the region and you’re ready to go.

Regions Approach
Regions: neat and small. Support snapshots when mouse hovers.

I supported the private methods approach which means that every logical section is enclosed in a private method. When you review the main method you only see the name of the methods (which should hint what the method does). Should you want to inspect a logical section in more details, you’ve to navigate to the relevant method’s definition.

Private Methods Approach
Private methods: bigger and not as neat. Support method description when mouse hovers.

There is no much difference between those two approaches: both support a way to enclose logical regions and both makes readable code. Adam said that since those methods doesn’t expose shared functionality and will only be called once, there is no reason to use them. At that time I couldn’t find a reason to why I prefer using private methods, expect that “it feels right”, and so we decided to just drop the whole subject.

But now that I’m using VS2005 I’ve a good reason for preferring the private methods approach: unit tests. In VS2005 you can generate unit tests and the great thing about it is that you can test private methods – VS will generate the appropriate code to access private methods via reflection. So if you enclose logical sections within private methods, you can automatically write tests to test only that functionality. But if you chose to enclose the logical sections using regions, there’s no way to automatically strip out just that functionality and expose it to the unit test.

What do you think – what approach would you choose for enclosing logical sections in long methods?

Posted in Uncategorized | Tagged: | 2 Comments »

Setting Full Trust to Mapped Shared Folders

Posted by Fernando Felman on March 5, 2007

The project I’m involved on at the moment uses a shared folder as the base mapping point. That is, we use the drive letter W to map to a shared folder that has the sources and the builds. One of the issues with this configuration is that, by default, shared folders are only partially trusted by the .Net and so when I added some MSTests they failed to initiate (MSTest , like many unit test frameworks, extensively uses Reflection which require Full Trust in order to run). The way this issue has been solved is by redirecting the builds to the C: drive (local drives are full trusted by default).

Another way to solve this issue would be by full trusting the assemblies from drive W. This can be done by adding a new code group to the Machine level of the Runtime Security Manager (the CAS util) with Full Trust permissions and a URL matching pattern with the URL notation of the local drive, e.g.: file://W:/*

Alternatively, you can use CAS Policy utility to do the same from the command line:
caspol -m -ag All_Code -url file://w:/* FullTrust -n Drive_W -d "Grants Full Access to the development W drive"

-m: modifies the machine level
-ag: adds a new code group
All_Code: the name of the parent code group (you can also use the label ‘1’ instead of the name)
-url file://w:/* : the new code group will affect any assembly matching the URL file://w:/* which means “any assembly running from the W drive”
FullTrust: All matching assemblies will have full trust settings
-n Drive_W: the name of the new code group will be Drive_W
-d “…”: the description of the new code group

When scripting, use the -q (for quite) in order to suppress the verification messages.

Posted in CAS | 1 Comment »

Variance and Generalized Constraints, MS Research Publications

Posted by Fernando Felman on March 4, 2007

A very good document with recommendation for Variance and Generalized Constraints implementation in C# by Burak Emir, Andrew J. Kennedy, Claudio Russo, and Dachuan Yu.

Let me just quote the Introduction so you can understand what it is all about:

The Generics feature of C# 2.0 introduced parametric polymorphism to the language, supporting type parameterization for types (classes, interfaces, structs,and delegates) and methods (static and instance). Being object-oriented, C# already offers subtype polymorphism, namely the ability for a value of type T to be used in a context that expects type U, if T is a subtype of U.
As it stands, though, subtype and parametric polymorphism interact only through subclassing. In particular, there is no subtyping relationship between distinct instantiations of the same generic type type parameters are said to behave invariantly with respect to subtyping. This leads to a certain inflexibility: a method whose parameter has type IEnumerable<Control>cannot be passed an argument of type IEnumerable<Button>,even though this is safe: since Button is a subclass of Control, something that enumerates Buttons also enumerates Controls. Dually, a method expecting a parameter of type IComparer<Button> cannot be passed an argument of type IComparer<Control>, even though this is safe: something that can compare Controls can also compare Buttons.

The document then details possible extensions to C# in order to express Variance and Generalized Constraints.

I hope MS will implement the recommendations since I already found myself dealing with this issue in the past and I’m not entirely happy with the workaround I used.

Posted in Generics | 1 Comment »

Using Generics to code type-free helpers

Posted by Fernando Felman on March 2, 2007

The other day I received a small WinForm application to review. The application uses many forms, each designed for a single-specific task. I noticed that in the main form, the developer duplicated the following code snipped to initiate various forms:

if ((this.frmUtil1 == null) || this.frmUtil1.IsDisposed)
    this.frmUtil1 = new Forms.Util1();

if ((this.frmUtil2 == null) || this.frmUtil2.IsDisposed)
    this.frmUtil2 = new Forms.Util2();

One of the best practices I found is that if you’re duplicating a code section, it’s a good indication of a shared functionality that can be encapsulated. We’d like to have a service that will receive an object representing the WinForm and handle it by properly initiating the form when required:

public void ShowForm(Form sourceForm, Form reusableForm);

In .Net 1.1 we must go through reflection in order to create an object of the correct class dynamically, but in .Net 2.0 we’ve got Generics to facilitate things:

static class FormHelper<T>
    where T : Form, new()
    public static void ShowForm
        (Form sourceForm, ref T reusableForm)
        if (reusableForm == null || reusableForm.IsDisposed)
            reusableForm = new T();

Notice the usage of the new constraint after the description of the type T: this tells the compiler that the type T supports a default public constructor and so we can easily create a new object of the T class using new T();

To use the service you’d pass the type of the forms and both forms varibales:

FormHelper<Froms.Util1>.ShowForm(this, ref this.frmUtil1);

This would both initiate this.frmUtil1 with a new instalse of Froms.Util1 when needed and also show the form.

Posted in Generics | Leave a Comment »

And so it begins

Posted by Fernando Felman on March 2, 2007

Cottleston Pie / A. A. Milne

Cottleston Cottleston Cottleston Pie,
A fly can’t bird, but a bird can fly.
Ask me a riddle and I reply
Cottleston Cottleston Cottleston Pie.

Cottleston Cottleston Cottleston Pie,
Why does a chicken? I don’t know why.
Ask me a riddle and I reply
Cottleston Cottleston Cottleston Pie.

Cottleston Cottleston Cottleston Pie,
A fish can’t whistle and neither can I.
Ask me a riddle and I reply
Cottleston Cottleston Cottleston Pie.

I chose this poem as the title for my technical blog because I think it emphasizes many aspects of the enterprise software development process: there are many riddles and ambiguity involved in the process of delivering a quality solution for an enterprise. I intend to use this blog as a stage to hopefully answer some of the riddles I know and maybe bring some insights to how to deal with others.

I’m a senior software consultant with more than 7 years of experience in the Microsoft-based software field. In the last 4 years I’ve been focusing on delivering enterprise web-based applications for medium and medium-large enterprises using Microsoft technologies and frameworks. Many times I’ve been called to “open a blog, mate. You’ve got so much content to share!”. Today, I respond to that call. 🙂

This blog will focus on my work life as a senior consultant in Unique Word and I’ll add posts whenever I feel I can contribute or share. I’ll focus on the .Net v2 and the great new things you can do with .Net v3 as these are the fields that I’m passionate about, but every while and then some general technical post can sneak into the flow.

Enjoy the ride!

Posted in Uncategorized | 2 Comments »