Archives de la catégorie Uncategorized

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 :

unitTestProject

Rename unit test class to calculatorTest:

02-renamecalculatortest

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:

03-Red-Failetocompile

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:

04-createclasslibrary

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

05-classcreated

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

06-referenceinunitest

Add the namespace calculatornamespace in the unit test project

07-solvereference

now our code build successfully:

08-buildsucced

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.

09-runtest

Test fails as expected:

10-testfails

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

11-addimplementation

And run test again:

12-testpass

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>

13-calculatorcode

14-calculatortest

Poster un 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:http://msdn.microsoft.com/en-us/data/ee712907

 

 

, , , ,

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 Asp.net 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

-ADO.NET

-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

About

I am Microsoft software Developper who is very interesting on Microsoft Technologies.

I have 7 Microsofts certifications on Framework 3.5:

  • .NET Framework 3.5, ADO.NET Applications
  • .NET Framework 3.5, ASP.NET Applications
  • .NET Framework 3.5, Windows® Communication Foundation Applications
  • .NET Framework 3.5, Windows® Forms Applications
  • .NET Framework 3.5, Windows® Presentation Foundation Applications
  • .NET Framework 3.5, Windows® Workflow Foundation Applications
  • .NET Framework 2.0

I follow Techdays  and try to answer in Stackoverflow

Poster un 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