.
Anmeldung | Registrieren | Hilfe

.NET-Blogs Archiv Januar 2008

Improving Agility in Visual Studio

31.01.2008 13:46:00 | Sebastian Jancke

What Visual Studio (2005) Professional lacks out of the box, is a good support for unit testing and build scripting. Actually, VS2005 Pro does not provide any support for it. So in case you don't want to buy the Team Edition (for testing support), looking for a free alternative, the following might be a solution.

TDD (with NUnit) is definitely at the heart of my day-to-day development process. But I was in a snit to call NUnit-GUI-Runner from the custom tools menu. Further, I needed a possibility to automate my release processes (compile, test, commit, tag, zip, name with version). On the java side of our dev world, I was used to Ant. And happily, there is a .NET alternative to crude MSBuild: NAnt.

Without going into the details of NAnt, one can say: NAnt is XML-based, easy to use, has a bunch of tasks and plugins for all sort of things (vcs's, io, vs-solution stuff, compression, ...) and can be easily extended with your own tasks/plugins. What's really cool about NAnt is the fact that it's Ant for .NET - I don't need to relearn anything and can reuse my knowledge of Ant (which is the standard build scripting system in the Java/eclipse world).

Using these visual studio addins:

saved my day-to-day dev life.

NAnt-Snapshot
Courtesy of Eric Harth and Olivier Chekroun

 

NUnitAddinSourceForge
Courtesy of Eric Harth and Olivier Chekroun

Running NAnt scripts is fairly easy. Running NUnit tests directly within the IDE (without buying TestDriven.NET) made my day. Both addins are written for Visual Studio 2005 (I don't know about compatibility with 2008). They are easy to install and use.

Both addins accelerated my development process a lot, because I don't need to leave the IDE any more for testing and my release process is now automated

Perhaps, one day someone ports these for VS 2008 (if they incompatible). Both are open source, so this task should be pretty easy.

.NET-Forum.de launched

29.01.2008 13:21:00 | Thomas Freudenberg

.NET ForumA couple of days ago Jan Welker launched a new German .NET related community site, .NET-Forum.de. I didn't expect the developsphere to require just another site, but there are already 51 users registered, even though the site wasn't advertised anywhere except the dotnet-snippets.de newsletter.

Jan set up Community Server 2007.1 to drive the site (Did you know that .NET related non-profit communities may receive a free license?) I try to support Jan whenever he experiences issues with CS or has a configuration question. So for me it's an appreciated opportunity to get to learn CS's forum capabilities (Till now I only used the blogging part.)

I wish Jan success, and maybe this post will lead some more people to his site.

String.IsNullOrEmpty as Extension Method

25.01.2008 13:53:00 | Thomas Freudenberg

Most you will probably know about Extension Method introduced with C# 3.0. If not, I strongly recommend to read ScottGu's explanation.

Anyway, a couple of days ago Brad Wilson posted an experiment:

What I wasn't sure was whether or not you could call these extension methods when you have a null instance of the object, since they're instance methods. The C++ guy in me said "sure, that should be legal", and the C# guy in me said "it's probably illegal, and that's too bad". Amazingly, the C++ guy in me won!

This code executes perfectly:

using System;

public static class MyExtensions {
    public static bool IsNull(this object @object) {
        return @object == null;
    }
}

public class MainClass {
    public static void Main() {
        object obj1 = new object();
        Console.WriteLine(obj1.IsNull());

        object obj2 = null;
        Console.WriteLine(obj2.IsNull());
    }
}

When you run it, it prints out "False" and "True". Excellent!

When I read that I immediatley thought of another application. I guess all my readers are familiar with String.IsNullOrEmpty which was introduced with .NET 2.0. So I asked myself if you can make IsNullOrEmpty a parameterless extension method:

using System;

public static class MyExtensions
{
    public static bool IsNullOrEmpty(this String s)
    {
        return String.IsNullOrEmpty(s);
    }
}

public class MainClass
{
    public static void Main()
    {
        string s1 = "Hello world";
        Console.WriteLine(s1.IsNullOrEmpty());

        string s2 = null;
        Console.WriteLine(s2.IsNullOrEmpty());
    }
}

Again, it prints out False and True. And in my opinion this syntactic sugar looks much more elegant than the ordinary String.IsNullOrEmpty(s2).

If only C# would support extension properties...

Die nächsten fünf Geburtstage mit Access SQL berechnen

08.01.2008 22:10:00 | Jürgen Gutsch

für eine kleine Communityanwendung die ich, zum 10 Jährigen Klassentreffen meiner alten Schulklasse und für meine alte Schulklasse, geschrieben habe, benötigte ich eine Funktion zum anzeigen der nächsten fünf Geburtstage. Voraussetzung war, so wenig Logik wie möglich in die Anwendung zu bringen um den Pflegeaufwand klein zu halten, das heißt: Alles was per SQL gemacht werden kann soll auch per SQL gemacht werden.

Die Herausforderung war jetzt anhand eines eingetragenen Geburtsdatums, das Datum des kommenden Geburtstages zu berechnen. herausgekommmen ist folgende Formel:

IIF(
    DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()), [geburtsdatum]) <= NOW(),
    DateDiff('d', Now(), DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()) + 1, [geburtsdatum])),
    DateDiff('d', Now(), DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()), [geburtsdatum]))
)

Erklärung:

Zuersteinmal berechne ich das Datum des Geburtstages im aktuellen Jahr, hierfür ziehe ich einfach das Geburtsjahr vom aktuellen Jahr ab und addiere die Differenz zu dem Geburtsjahr hinzu:
DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()), [geburtsdatum])

In der IIF methode prüfe ich nun ob das berechnete Datum vor, oder nach dem aktuellen Datum liegt. Ist es älter, rechne ich ein Jahr dazu und ziehe das aktuelle Datum  davon ab um die Diferenz in Tagen zu erhalten:
DateDiff('d', Now(), DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()) + 1, [geburtsdatum]))

Ist das Datum neuer als das aktuelle, wird ohne ein Jahr hinzuzuzählen, die Differenz in Tagen zum heutigen Datum berechnet:
DateDiff('d', Now(), DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()), [geburtsdatum]))

Auf diese Art erhalte ich die Anzahl der Tage bis zum Geburtstag und kann diese ebenfalls ausgeben. Nachteil an Access SQL ist, dass man scheinbar nicht nach Spaltenaliasen sortieren kann wie beim SQL Server. Daher muss die komplette Formel auch bei der Sortierung (ORDER BY) angegeben werden (es wird logischerweise aufsteigend nach dem berechneten Wert sortiert).

Der komplette SQL String sieht daher (etwas formatiert) so aus:
SELECT TOP 5 * ,
  IIF(
    DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()), [geburtsdatum]) <= NOW(),
    DateDiff('d', Now(), DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()) + 1, [geburtsdatum])),
    DateDiff('d', Now(), DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()), [geburtsdatum]))
  ) AS unterschied
FROM [adressen] 
ORDER BY
  IIF(
    DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()), [geburtsdatum]) <= NOW(),
    DateDiff('d', Now(), DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()) + 1, [geburtsdatum])),
    DateDiff('d', Now(), DateAdd('yyyy', DateDiff('yyyy', [geburtsdatum], Now()), [geburtsdatum]))
  ), [name]

Um das SQL mit SQL Server zu nutzen, müssen natürlich die entsprechenden Datumsfunktionen zum Berechnen verwendet werden.

Regeln | Impressum