Archive for Orcas tag

Partial methods in .NET 3.5, overview and evolution

August 2nd 2007 • .NET (, , ) • 1,210 views • one response

One of the interesting new things in .NET 3.5 is partial methods which are now being used extensively by LINQ to SQL and no-doubt will be Microsoft’s cornerpost of extensibility for generated classes. Here’s a quick overview:

Extending generated code via inheritance (.NET 1.1)

When inheriting from generated classed designers often provide virtual methods for you to override and extend at a cost of being forced to inherit from the generated class instead of one of your own choosing. e.g.

class Customer
{
    private String name;
    public String Name {
        set {
            name = value;
            OnNameChanged();
        }
    }
    protected virtual void OnNameChanged() {
    }
}

class Customer : CustomerGenerated
{
    protected override void OnNameChanged() {
        DoSomething();
    }
}

Extending generated code via delegates/event handlers (.NET 2.0)

.NET 2.0 introduced partial classes and code generation moved into separate files that were pulled together at compile-time to provide a single consistent class leaving you free to choose your own inheritance.

The drawback is that you can not override generated methods because they are not inherited but rather merged at compile-time.

This means if you want to provide extensibility in your generated code you will either need to use inheritences or generate delegates/events that consumers of your class can hook up. This works but is not intuitive as demonstrated:

partial class Customer
{
    protected event EventHandler NameChangedHandler;
    private String name;
    public String Name {
        set {
            name = value;
            if (NameChangedHandler != null)
                NameChangedHandler.Invoke(this, null);
        }
    }
}

partial class Customer
{
    public Customer() {
        NameChangedHandler += OnNameChanged;
    }

    protected void OnNameChanged(object sender, EventArgs e) {
        DoSomething();
    }
}

Extending generated code via partial methods (.NET 3.5)

.NET 3.5 sees Microsoft answer the problem by allowing the partial keyword to be used at method level instead of just at class level:

partial class Customer
{
    private String name;
    public String Name {
        set {
            name = value;
            OnNameChanged();
        }
    }
    partial void OnNameChanged();
}

partial class Customer
{
    partial void OnNameChanged() {
        DoSomething();
    }
}

Thoughts

This is clearer than using delegates & events but still suffers some limitations:

  • Only one method can have a body so you can’t override generated code
  • Properties are not supported

If .NET supported multiple inheritance then partial classes and methods would be redundant. As Microsoft tout interfaces and composition as a better approach it is about time they added support to automatically wire up an interface to a compound object (a blog post for another time).

[)amien

AnkhSVN and Visual Studio 2007/2008/Orcas

June 13th 2007 • .NET (, , ) • 6,724 views • 13 responses

A newer registry file is available to provide AnkhSVN with Visual Studio 2008 and Vista support in one.

If you are using Visual Studio 2007/2008/Orcas/9.0 you will have found that AnkhSVN 1.01 doesn’t appear in the IDE.

Like many Visual Studio add-ins AnkhSVN should work just fine but won’t appear because the installer does not write an entry for it in the 9.0 section of the registry.

In this case the specific branches are:

32-bit HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\AddIns\Ankh
64-bit HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\8.0\AddIns\Ankh

All we need to do is export that section out, change the 8.0 to 9.0 and then run it back into the registry.

This technique should work for any other add-in’s you have that are only appearing in 2005.

If you installed AnkhSVN into the default folder try these registry files I have prepared for 32-bit or 64-bit machines.

[)amien