Design Patterns(C#): Basic example Strategy pattern

In this example I´m going to explane the strategy pattern. With this example you can do a calculation with two numbers (-/+) and expand the number of operators (/, *, etc.).

First create a interface which defines the interface of the operator classes. For this example an operator can only calculate two values.

//The interface for the strategies
public interface ICalculateInterface
//define method
int Calculate(int value1, int value2);

Next create the operators (strategies) Minus (which subtract value 2 from value 1) and Plussus (which addition value 1 with value 2). The classes need to inherit from the interface ICalculateInterface.

//Strategy 1: Minus
class Minus : ICalculateInterface
public int Calculate(int value1, int value2)
//define logic
return value1 - value2;

//Strategy 2: Plussus
class Plussus : ICalculateInterface
public int Calculate(int value1, int value2)
//define logic
return value1 + value2;

At last we need to create a Client that will execute the strategy.

//The client
class CalculateClient
private ICalculateInterface calculateInterface;

//Constructor: assigns strategy to interface
public CalculateClient(ICalculateInterface strategy)
calculateInterface = strategy;

//Executes the strategy
public int Calculate(int value1, int value2)
return calculateInterface.Calculate(value1, value2);

Now we have two operators (minus & plussus) and a client (CalculateClient) that can execute the operators. Let’s test the code. Create a new webapplication, console app or something else that can write output. For this example I will use a webpage.

Initialize a new CalculateClient with argument operator Minus of Plussus and Calculate two values.

protected void Page_Load(object sender, EventArgs e)
CalculateClient minusClient = new CalculateClient(new Minus());
Response.Write("<br />Minus: " + minusClient.Calculate(7, 1).ToString());

CalculateClient plusClient = new CalculateClient(new Plussus());
Response.Write("<br />Plussus: " + plusClient.Calculate(7, 1).ToString());

This code will give the following output.

<br />Minus: 6
<br />Plussus: 8

The great thing about this pattern is that you can easily add new opertators (strategies) to your code.


Asp.Net: Webtest trough proxy (WebTestPlugin)

The test-team came to me with a problem involving connection errors while running webtests trough the company proxy. The webrequest needed to go to the webproxy including authentication. Visual Studion 2008 doesn’t support proxy authentication out of the box, but you can create a WebTestPlugin that does the authentication for every request.

The following class inherits from WebTestPlugin and overrides the PreWebTest method. In the PreWebTest method it will authenticate the request with your credentials.

public class LoadTestProxyAuthentication : WebTestPlugin
        public override void PreWebTest(object sender, PreWebTestEventArgs e)
            // Create WebProxy (enter your proxy url)
            WebProxy webProxy = new WebProxy("ProxyAdres");

            // Use the proxy for the webtest
            e.WebTest.WebProxy = webProxy;

            e.WebTest.PreAuthenticate = true;
            NetworkCredential proxyCredentials;

            proxyCredentials = new NetworkCredential();

            proxyCredentials.Domain = "domain";
            proxyCredentials.UserName = "username";
            proxyCredentials.Password = "password";
            e.WebTest.WebProxy.Credentials = proxyCredentials;

How to use the webtestplugin
Ad the class to your test project, change the credentials and proxyadres. After a build open the webtest press the ‘add  Webtest plugin’ button (on the top screenmenu), select the LoadTestProxyAuthentication and press OK. You need to add the webtestplugin for every webtest.

Now you can run your webtests trough the proxy.

Hope it helps,

Create a Visual Studio add-in with contextmenu and selected text as input

Create a Visual Studio add-in with contextmenu and selected text as input

When working with a new way of storing settings in a database. I was frustrated how much work it was to check the value of setting from code. So I deceided to make my life a bit easier by creating a VS2008 contextmenu add-in. With this add-in I can select text within VS and use the value of the selected text within the add-in popup. The hardest part was figuring out how to create a contextmenu and how to use the selected text as input value.

In this blogpost I will show how to create a Visual Studio contextmenu add-in and pass the selected text to the pop-up. I’m not going to explain how to create an add-in you can easily find articles about this on MSDN or blogs (just try Google).

Now let’s get started. Create an new Visual Studio add-in project and add the following code to the OnConnetion Method within the Connect.cs. This code will insert add the contextmenu.

_applicationObject = (DTE2)application;
CommandBars cBars = (CommandBars)_applicationObject.CommandBars;

CommandBar editorCommandBar = (CommandBar)cBars["Editor Context Menus"];
CommandBarPopup editPopUp = (CommandBarPopup)editorCommandBar.Controls["Code Window"];

Command command = commands.AddNamedCommand2(_addInInstance,
 "GetSetting", "Bekijk Setting", "Executes the command for test", true, 733, ref contextGUIDS,
 (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,

Then to get the selected text I use the following method within the Exec of the Connect.cs and pass the selected text (return value) to a property of a Windows Form pop-up.

private string GetSelection()
    string setting = "";

    //Check active document
    if (_applicationObject.ActiveDocument != null)
        //Get active document
        TextDocument objTextDocument = (TextDocument)_applicationObject.ActiveDocument.Object("");
        TextSelection objTextSelection = objTextDocument.Selection;

        if (!String.IsNullOrEmpty(objTextSelection.Text))
 //Get selected text
            setting = objTextSelection.Text;
    return setting;

Hope it helps.


Asp.Net: invoke WCF method with WCF Test Client

When deploying a Silverlight application we ran into problems a WCF web-service, to find out what the problem was I wanted to invoke the method.

Microsoft shipped an application for invoking methods from your Windows PC (WCFtestclient.exe). The following steps explain how to use WCFtestclient.exe.

First startup Visual Studio 2008 Command Prompt. In the command prompt type wcftestclient, the application will startup.

Now we need to add the Service. Click File and Add Service.


Add the URL to your service in the pop-up and pres ok. The service will now add all methods from your service. You can add multiple service URLs.

Double click the method you want to invoke from the tree on the left pane. Enter the values that are required for the Invoke and press Invoke. The method will be invoked.

The right bottom pane will show the response. In my case this shows the Stack-trace because of the error. Normally this will show the web-service response (XML).

With the stack-trace I could located the error and fix it.

Hope this helps,


Playing with JQuery (fixing Intellisense in VS2008)

The last few years I spend a lot of time working with Asp.Net AJAX. It all worked pretty good, the only downside is that you do not have control the generated HTML. With jQuery you can manipulate generated HTML. This HTML can be generated fully controlled with a ListView.

I'm a lazy programmer so the first thing I needed to do is fix jQuery Intellisense in VS2008. I found my information for doing this on Scott Gu's blog (jQuery Intellisense in Vs 2008). Everything worked great the only bad thing is that my computer needed a restart after installing a Visual Studio Patch…

Now the Intellisense is working the only thing I need to do is blog some nice examples of my work! 😉




Visual Studio 2008 released

Since a few days the newest version of Visual Studio has been released together with the new 3.5 .Net framework.

It contains tons of new features such as:

  • Javascript Intellisense
  • Multi-targeting support (.Net versions: 2.0, 2.0 and 3.5)
  • Built in AJAX support (no need to install an extra package)
  • New Html web designer (same as Expression Web) and improved CSS support
  • Language improvements and LINQ
  • Access to the .Net Framework Library source code (for more information read this post from Scott Guthrie

When you are a MSDN subscriber you can download it here. If not you can download the 90 day trial of the Team Suite edition here.

Be sure to read this post from Scott Guthrie’s blog 

Silverlight Alpha 1.1 VS 2008 add in

just tried to install the VS2008 SilverlightTools Alpha, but I
immediatly ran in to an error. The installation couldn’t start beacause
VS2008 was not installed. This suprised me because I just installed
thirty minutes before. A some searching I found out that I needed to
install the Visual Web Developer.

You can install this by running the VS2008 setup again an choose to add features. Now the Silverlight tools setup runs flawless.