TDD – Test Design Driven on visual studio 2013

TDD is usefull to be sure of what your code is supposed to do and guaranty that your test strategy is started as soon as you start to develop.

The hard work is more on correctly scoping your test on TDD ( scope at component level or integration level) because for a high level, on a similar way, we could use BDD (Behavior Design Driven at requirement/ user stories or acceptance level).

A quick start with a quick sample (famous calculator):

3 steps called

-Red because at the first iteration, test should fail (no software exist to do the job)

-Green because we create simple class in order to pass on build

-Refactor for many iterations to implement the business logic and after make code in better way

First create a test project in visual studio :


Rename unit test class to calculatorTest:


First task to implement: application  is able to add 2 integers:

1 – Red => create a test which check that application is able to add 2 integers:


We have created a unit test method called TestAdd2int in three step

Arrange => to create the context for the test

We choose to declare a Calculator class (not yet created) to do the work

Act=>do the work we want to test (here call the tested method add 2 integers)

Assert=>to check that the result is correct

The red step is done => it doesn’t compile but we have design a Calculator Class and an Add method to do the task

2/Green=>create the class with only the needed items to pass the build (not the assertion):

We only need to create a Calculator class and an Add method which return an integer:

Create a class library called Calculator:


Implement Calculator class and an add method (without the business logic)


Add a project reference of the created library class in unit test project :


Add the namespace calculatornamespace in the unit test project


now our code build successfully:


The green step is done => our code compile

3/The final step is to implement and refactor:

We launch mstest runner to check that our test fails because implement of business logic doesn’t exist.


Test fails as expected:


Now, we implement the task in Add method of our Calculator class:


And run test again:


Congratulations => you have done your first TDD

After that when implementing other methods, as substract… for different type (double …) we will make refactoring – (using generic and dynamic step which could be as follow:

Class view: (Calculator become calculator<T>



Poster un commentaire

Code UI Test in Visual studio 2010 ultimate – Acceptance test in Microsoft

with Ultimate or premium visual studio, you are able to automate your acceptance test or make some User interface test with a recorder provided by

Code UI Test project. Morever, you can customize those test by adding some code, so let’s start with a simple winform project and after create the acceptance test associated:

Create a winform as follows to make an addition:

On designer set textbox and label:


The code behind on button click will display the addition result :


Now, let’s try to test our GUI by first creating a code UI Test :

1/add a test project to our solution (in ultimate version please or premium) and call it as you want 😉


2/Notice the test project on solution explorer and delete the UnitTest.cs file:


3/add a new test:


4/select a code UI Test


5/ a dialog appears, let the record action checked in order to use a User Interface recorder

Which allows to record every User Interface action and click OK:


6/waw on the left side at the bottom, a tool appears : it is the UI recorder


7/create a shortcut on your desktop to launch our addition application and press start recording to record the events to start the application (only click on shortcut to launch application):


8/Press pause to stop recording, click on the right button to notice the event recorded (here only the application launched)

You are able to delete some record steps if there are noise on test.


9/Finally, click on the button at the right side to generate code


10/in code ui test the method is generated and allow an automatic launch of our application


11/record and put some addition data here 5 + 5 = 10 :


As you can see all click event and numbers put on application are registred, let’s stop record and  create a method as before:


The second is correctly generated in code UI test:


Now, let’s check that the result is 10 by put the target button by drag and drop on the control label we are interesting:


As you can see a blue square wrap the control we are interesting and a panel property appears to let us decide on which property we will do an assertion: for sure it is  the display text so select the display text property and click on add assertion button :


It is inserted in code test:


Now run the test by selecting it in test view and enjoy the test is automated:


Waw the test is passed ! we have now to integrate it in a tfs nightly build and the delivery continuous will come faster 😉

, , , , ,

1 commentaire

DataFlow – TPL ( Task Parallel Library) – Asynchronous / Parallel programming using Microsoft

DataFlow allows you to make asynchronous process with different step of the process (called Block).First, download the TPL library using Nuget (and visual studio 2012 if you have it ;-). Create first an application console and after use nuget as follows « Manage Nuget Package »:


Write “dataflow” on the search textbox you will obtain the nuget package as follows:


In Solution Explorer, you will notice the TPL library as follow


The simple block called action block allow you to define a function asynchronously.

To write it is very easy, you have only to instantiate an ActionBlock first :


here we use linq to make a loop ten time on our process which waits one minute and write the string parameter in a console.After declare it, you have only to call it using Post method and pass a string parameter in our case:



the whole code to run your DataFlow in a console application is:


You will notice that the dataflow will occur in the same time as the while loop where we write dot in the output.Run it and enjoys :



waw, this a parallel program 😉

Now we will add another block to make our dataflow process.We create a TransformBlock which take a string as input and return the string in UpperCase.


We link the TransformBlock to the previous ActionBlock to write in console ouput the string in upper case:



Finally, we post a string using the transform block (which transfer the string to the ActionBlock because of  the link we ‘ve made:



the whole code :


Run it :


congratulation, you have made your first dataFlow (using two blocks) in a parallel programming 😉

With TPL, you can associate as many block you want  in asynchronous way

, , , ,

Poster un commentaire

Using architecture Diagram with visual studio ultimate 2010, architecture check

We create a simple 3 Layered architecture application with presentation layer for GUI, business Layer to do our business process and Data Access Layer to get our data from different source. The purpose is to check that each layer call the layer below and if not a tool (here architecture diagram ) can detect that someone doesn’t follow architecture rules.

The solution explorer show three project (one console project as presentation layer and the two others are library project:


The DataLayer project contains Data.cs file where data access is implemented:


the business layer in a process call data access layer to make some work:


with a reference on Data access layer assembly


finally, the presentation layer call the business process and show result in a console output:



with a reference on layer below that is business layer



use Architecture ->Generate Dependencices diagram by Assembly :



you obtain a dependency graph:


we see that Presentation Layer calls business which calls DataLayer.

We want to set architecture rules, so go to Architecture -> new diagram and select Diagram Layer:


click ok and set a name to your model project which appears in our solution explorer:



in toolbox on the left panel select layer and drag and drop on diagram three times as follows:



rename each layers to have presentation, business and data layer:


Drag and drop each project on its layer (Presentation is dragged and dropped on Presentation layer and so on…) a number at the right side of the layer appears to show that the layer is associated to a project 😉


now generate the dependencies we want to keep :


Visual studio create dependencies at layer level for you 😉


arrows show all dependencies (Presentation calls Business which calls Data layer)



Suppose now our software developper friend who call from presentation directly Data by setting a reference on data assembly and call our data method which is prohibited:


You are the architect (waw you have a good job ;-), and you want to quickly check that the architecture is followed and if not you want to have an alert. Go to the Layer diagram, then call Validate from the context menu


Now we have three errors showing where the architecture is not followed as expected:


Correct it by retrieve the modification of your bad friend (code and Data reference if not your bad friend will loose his job and will ask you to let him sleep at your home ;-( ), and run Validate Architecture again:



all is good so enjoy and show this article to your manager to get the ultimate version of visual studio 2010 😉

, , , , ,

1 commentaire

Code Contract, a way to control authorized values input output method or in interface at building time ! – Microsoft Static Check

From Microsoft research, with code contract, it is allowed to make verification on input, ouput arguments on methods and make other check on Interface.

First check in object explorer if you have Microsoft.Contracts.

1/ if you have an old Visual studio download Code contract assembly on download panel:


run MSI installer as follows:


Add a reference on Microsoft.Contracts assembly (which implements Data Contract you have just installed before 😉 :


in object explorer, (double click on Microsoft.Contracts to run it), you will see the 2 namespaces and classes contained:


2/Now you have Code Contract Assembly

in BizProcess method, use Contract class and call Requires method to check input value (here we check that b is different of 3 ;-):


Go to properties solution and in Code Contracts panel (left side), Check Perform Static Contract Checking in order to make checks on build time 😉


when you build solution, you will see a warning as below because a business rule is violated (b different of 3 in our sample)


Code contract is very interesting to make some check at build time because at runtime it is too late… 😉

You can also make check on Interface by using attributes ContractClass and ContractClassFor

For sure when data are knowing at runtime, you will make your check in code.

, , ,

1 commentaire

Entity Framework 5.0 – Microsoft ORM ( Object Relational Mapping)

In this article, we will create a simple application using Entity Framework 5.0 with Code First approach. This approach is used when you want to create a database model from your application model.

Entity Framework is the ORM (Object Relationnal Mapping) in Microsoft Technologies.

First create a business object in a library class (the file is named Poco_entity.cs), client:

this is a POCO class because it doesn’t have an ORM depedency.

Secondly, create a clientContext class which will wrap the client class an is used by Entity Framework to create table

ClientContext inherits from DbContext and we use DbSet to wrap our POCO class.

It allows us to not have a dependency between class and ORM technologies. So ClientContext wrap a client collection.

Now we can create a database from our code application by initializing the database and the context for example in a console application:


Here, we first initialize the target database and indicate that each time the framework will drop and create database (only in development cycle;-). Secondly, we instantiate a ClientContext which allows us to manage our POCO class in clients collection (our DbSet property) to persist it in the target database. And finally, we call SaveChanges in order to create the database and record the client created in a table (client table).

Because of not specifying a sql connection, Entity Framework by default use SQL express:


Connect to your sql server express instance (for me Hassan-Tosh\Sqlexpress)

You will see that a database has been created for you (databaseContext.clientContext)

And in the client table, a new row is inserted 😉

you have also other approach: Database first ( you create entities code from an existing database ) and model first (you create class diagram in visual studio and Entity create database for you).

For more information:



, , , ,

Poster un commentaire

Multithreading with ThreadPool How many thread to use in a computer ?

In this article, I give a sample console application with a task contained a Sleep of ten second ( like  a process that take ten second 😉 to simulate an application with  200 task of ten second.

ThreadPool is a class that allows you to create task which are queuing and executing by the ThreadPool. It is better to use it than creating yourself all the plumbing of the management of thread ( I believe that Microsoft engineer work well ;-).

the rule is to take one thread by CPU to make real multithread but in many cases you can use as in my sample an example that demonstrate that it is not true every  time

I use a 8 cpu computer so in my ThreadPool when I call GetMinThreads it return 8 thread (one by CPU). The application launch 200 task that wait 10 second.

when running the application, you see that it took only 11 seconds instead of 200×11 second :

launch task manager to see the number of thread: first select view->Select Columns


and select Thread column:

there are 203 thread with 3 thread for console and main application:

if I set the minimum thread at 8, ThreadPool will use it so it execute the 200 task 8  by 8 . To do it modify the code by using SetMinThread :

Now the application spend more time although we respect the rule : 1 thread by CPU, we have 8 CPU so we use 8 threads 😉 Now it take 67 seconds (67011ms)!

you can observe that the ThreadPool has created new thread (more than 8) step by step which explains that application run on 67seconds and not on 200/8 x 10 seconds that is 250seconds. Don’t forget that you can set min and max thread so the ThreadPool can add or remove thread to execute all tasks.

If you want to fix the number of thread you can set the same number in SetMinThread method and SetMaxThread method

So if you have to make a lot of task try different number of threads using the SetMinThreads


, , ,

Poster un commentaire

Domain Design Driven with Microsoft Technologies – DDD

When starting a software project, the first question is : which architecture should we use ? What are the more efficient practices?

It depends of project size. There are two categories:

-RAD (Rapid Application Development) for little projects

-DDD , Domain Design Driven for big project, software we want use for several years

In RAD development, Architecture is designed by the environment development (like in visual studio) behind the designer when you drag and drop component between the GUI (Graphic User Interface) and the DataSource. Microsoft has built a framework called LigthSwitch (go the RAD article).

In DDD development, Architecture is splitted in N layers:

1- Presentation Layer:

in this layer, you can use different patterns on GUI implementation like

-MVC (Model View Controller) with MVC

-MVP (Model View Presenter) with Winforms Technologie

-MVVM ( Model View ViewModel) used with WPF technologies

Presentation Layer contains all GUI mechanism

2-Distributed Service Layer

this layer allows to communicate between Presentation layer and the other layers

it is very interesting if you want to make your application distributable in a N Tiers architecture (composed by many computer ;-))

Microsoft technologies for this layer are WCF, Web services, and windows server appfabric (this last is also used in Cross cutting layer). in this layer, SOA or REST are used to distribute services and isolate the software from the GUI.

3-Application Layer

Application layer represent the workflow of the software and can be implemented by Windows Workflow Foundation (WF)  Application Service ( provide service at Application level ) and Adapter (To make convertion between DTO to domain entities).

4- Domain Layer

Domain Layer (Business Layer) contains entities, services, repositories contract and query specifications. All around business is implemented here.

5- Data Infrastructure Layer

this layer is used to persist data in different storage. Microsoft technologies used  are

-ORM like Entity Framework 4.0


-Service Agents with Web services and WCF

6- Cross Cutting Layer

this layer is shared by all other layers and contains cache, security, technical logging and operations. Unity and MEF are used to implement in this layer and allow to decorate at reflection level the whole application especially with AOP (Aspect Oriented Programming)

Poster un commentaire

Share an assembly with nuget package – make your assembly accessible from Visual studio Gallery or Local repository

Nuget is a visual studio extension allowing to share libraries (add, remove..).

In this article, First we will create a nuget package to push our assembly in official nuget repository and get it from gallery in visual studio 2010 using NuGet command line. Secondly, we will use a GUI NuGet Package Explorer and create a local repository to share assemblies

In tools menu,click on Extension Manager to install Nuget Package Manager

Download Nuget Package Manager from Gallery. This package will allow to reference

In solution explorer, you can manage Nuget Package by right clicking in reference:

create your nuget account:

and register your API key in order to be authorized to push your nugget package on nuget website:

nuget setApiKey yourapikey

download Nuget.exe from codeplex url:

add the Nuget path in environment variables to register nugget application path

run nuget spec yourassembly.dll to create your package

in the assembly folder, you can see a new file with nuspec extension:

when editing the nuspecfile, we can see a xml configuration :

clean this xml file by set your own configuration or delete xml not used:

Create a lib folder and copy the assembly you want to package with nuget. In this lib folder create a framework subfolder (here net40 for framework 4.0) Make a nuget package by this command line:

Nuget pack MyBizservice.dll.nuspec

It creates a nupkg file, your nuget package:

push your package in the nuget server to share it with the command line:

nuget push yourpackage.nupkg

in your Nuget account, go to Package panel and see that your package has been pushed:

from gallery in visual studio , the assembly is now avalaible:

you can also use a GUI to manage your nuget package:

first download Nuget Explorer on codeplex url:

after downloading this tool, this dialog box is opened:

Create a new nuget package using the third task

Click on edit menu in order to set the xml configuration for the nuget package:

set the package name and other settings:

finally select the content you want to set in your nuget package by clicking on content menu ,Add and Lib folder (it is the folder where you set assembly for nuget) as follows:

in right panel called package contents, you will see the lib folder you have added

Right click on lib folder to select the target framework (here 4.0) of your assembly (Add .NET folder)

a net40 folder is created:

select this folder and right click to add the assembly you want to package with nuget:

we can see the assembly added in lib folder, save the nuget package by clicking on green save button or CTRL + S:

once saved the package metadata exit from the edit mode:

the package is created on the folder :

for this nuget package we will configure a local repository instead officical nuget repository . First go to options of Toos menu of visual studio:

select Package Manager, and Package Sources :

click on + button to add a new source (here a local repository called MyLocalNugetRepository but it can be a share for a team):

select the folder where you have saved the previous nuget package ( the cursor is hover the explorer button)

we have selected a local folder, click ok: now we have another source for nuget package. Right click in the project in solution explorer on Manage NuGet Packages:

on the left side in the online panel, we see the new NuGet local source we have created and when clicking on it, the previous nuget package appears:


We have seen how with console create a nuGet package shared in NuGet official package source and how use NuGet explorer to create a nuGet Package and set it in a local repository

, , , , , , , ,

Poster un commentaire

Workflow Foundation 4.0 sample application with a custom activity and using extension to call application service

We create first a console application that will be the host of  our workflow. After, we create a custom activity, and finally, we will use an extension (a business service) called from our custom activity.

Create a workflow Console Application in Visual  studio 2010 (be sure that the target framework is 4.0 😉

Click ok : Visual studio 2010 create a console application and call your workflow in the main through Invoke method of WorkflowInvoker class. On the solution explorer,You also see the xaml file of your workflow (xml file and code behind that defines your workflow foundation) named workflow1 by default

rename the workflow by right clicking on Workflow1.xaml, and call it “SampleWorkflow” :

double-click on the xaml file and you will see the workflow designer.Go the properties panel on the bottom right side and change the name of our workflow to “SampleWorkflow”

now rename the workflow instantiated in the main method:

Create  a custom activity:

Add a new item to the solution:

select class item and call it MyCustomActivity:

Make this class public and inherit from CodeActivity interface ( add System.Activities namespace). Override the execute method with a simple console print:

now, double click on SampleWorkflow xaml file and in the worklfow designer you will see that our custom activity exists in the toolbox :

drop it in the designer:

and run it, your custom activity works!

Create a local service BusinessService and its interface to make business class in it:

and its interface:

Modify program.cs file in order to use workflowApplication instead of WorkflowInvoker (we will add services called extensions in Workflow Foundation words)

the AutoResetEvent allows host to wait the completion of the workflow.

we add the businessService object using Extensions collection of WorkflowApplication: by this way, all activities can get from their context the extension – that is the service – from WorkflowApplication.

the completed event is added to set the AutoResetEvent at true when the workflow is finished

Modify the custom activity to call our service that is extension in using GetExtension method of CodeActivityContext


It is interesting to use extension in workflow in order to make business in service and not directly in our custom activity. It simplifies unit testing focus on business and not Workflow infrastructure ( that is test at level activity instead logic business).

, , , , , , , , ,

1 commentaire

Microsoft System Center 2012

All System Center 2012 products, Service Manager, App-Controller, Orchestrator, Virtual Machine Manager, Configuration Manager, Data Protection Manager, Operation Manager -Boutougha Hassan

Microsoft Windows Server 2012

IT - Windows server 2012 - Hassan Boutougha

Microsoft Hyper-V

IT - Hyper-V - Hassan Boutougha

Microsoft SQL Server 2012

IT - SQL Server 2012 - Hassan Boutougha