A bunch of useful C# extension methods – Part I: “IEnumerable” interface

Introduction

In my article I describe how a programmer can extend the default functions and methods of any object by adding custom functionality. By creating your own extension methods you can make your every day programmer’s life much easier. In my former article I only discuss a few extension methods to give you some examples how the mechanism of extensions methods is working. Now I want to give you a collection of very handsome and powerful extension methods I use constantly.
Read the rest of this entry »

How to exchange database query results in loosely coupled .net Applications

Introduction

Modern software programming patterns offers great possibilities for creating reliable, stable and easy maintainable software systems. A huge advantage of those concepts is the ability to loosely connect all those parts together so that the programmer can easily exchange some parts of the system instead of being forced to replace all of it. But usually you have some search results or data queried from a database or a model object that you need to pass through the system or the hierarchy or however your code is organized. By directly passing this data between the layers or loosely coupled parts of your system you inject the dependencies again which you were trying to get rid of. In this article I will show a simple and effective way to share database results or business models between those layers by using a so called abstract generic QueryResult Class. My sample code is implementing a Model Repository Pattern and will capsule the results of select statements to pass it to the application.
Read the rest of this entry »

Avoid ugly ToString results with the capsuling pattern in C#

Introduction

While developing an UI for an application there always comes the point when you have to offer the user the possibility to make a choice from a list of options. This could be anything for example: A list of customers or a list of available connections and so on. But when using standard Windows Forms controls to solve this problem you always have to deal with some difficulties when trying to get a human readable representation of this bunch of objects the user has to choose from. To make a programmers life easier I’ll present a simple solution of this problem by using a capsuling technique.

Getting started

To illustrate the problem and to present my solution I’ll use the good old ComboBox in this article. But this method will also work for any other situation. So let’s get started. When using some standard windows forms mechanisms like ComboBox.Items.Add one often realizes that those standard Windows Forms controls use the ToString() Method of an object when a textual representation of the object has to be displayed.
Please have a look at the following code

  public class Customer
  {
    public int Id = 0;
    public string Salutation = null;
    public string Lastname = null;
    public string Firstname = null;

    public Customer(int id, string salut, string fname, string lname)
    {
      Id = id;
      Salutation = salut;
      Firstname = fname;
      Lastname = lname;
    }

Read the rest of this entry »

How to make and use C# Extension Methods

A tutorial explaining the techniques behind extension methods by implementing a bunch of highly useful C# extensions and static functions.

In .net you have the ability to write extensions to already existing classes and types even if the original code is a third party one and you can’t access the sources directly. In this tutorial I will show you how to implement those extensions method and I will also show my workaround for static extensions because those are still not possible.

To begin with let’s have a lock at the IntelliSense suggestions for the “object” type .

As you can see by default there are 4 methods available. In former times if you want to extend this list you would have to have access to the original “object” implementation to add the functions you desire. Nowadays you can achieve the same goal by using extensions method. For example consider calling the ToString() method while the myObject instance is still “null”. The result will by a NullReferenceException.

Read the rest of this entry »

Inheritance in nunit test classes

Since I started using nunit I really love the possibility to inherit from a general test class to save time and code lines while writing new test.

Usually most of my tests share the same basic [SetUp] and [TearDown] scenarios. So by putting those code blocks into a base class I can write new tests more easily.

Here’s a base test class example for a recent application I’m working on
Read the rest of this entry »

Visual Studio Search and Replace multiline code with regular expressions

Do you know the following problem. You want to implement the following interface:

  public interface IPerson
  {
    string Lastname { get; set; }
    string Givenname { get; set; }
    string Street { get; set; }
    string StreetNumber { get; set; }
    string City { get; set; }
    string ZipCode { get; set; }
  }

By using Visual Studio code generation tools you get the implementation code quickly but you want to get rid of those “throw new NotImplementedException();” parts quickly. Usually Search & Replace would do the trick but sadly it doesn’t support multiline replacements.

Read the rest of this entry »