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)

Publicités

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: http://nuget.org/account/Register

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: http://nuget.codeplex.com/releases/view/58939

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: http://nuget.codeplex.com/releases/view/59864

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:

Conclusion:

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

Conclusion:

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

PEX – Program EXploration with a simple sample – a Microsoft framework to have a complete test coverage

Pex is a Microsoft framework that explore all execution branch in a method and create all the unit test unit to have a complete test coverage.

Create a library class we will use to create unit test with pex :

create a method in BizService with a simple algorithm that shows how Pex Framework create unit test for you in order to have a complete test coverage that test all execution Branches:

Now run Pex (to download it http://research.microsoft.com/en-us/projects/pex/downloads.aspx) on right clicking on the method and select run Pex:

you obtain in Pex exploration result 4 test, using a null value, an empty value and the value you want to test. Pex run 4 times the method in order to discover all execution branch:

as you can see a failure test appears because we throw an exception : it is an expected result so we will allow this one by right clicking on this test and we will allow it:

Pex ask us to create a test project : accept

we can see the new test project in solution explorer:

 

in MyBizServiceTest.cs file, Pex create a unit test:

Pex create those  tests in MyBizServiceTest.runDifferentExecutionBranch.g.cs (solution explorer):

at the first test, we can see the expected exception NullReferenceException

re-run test by clicking Run buttong in Pex exploration Results and now all are passed:

we have also a panel Pex explorer where we can see unit test name

Conclusion : Pex create all test for us in order to test all execution path

, , , , ,

Poster un commentaire

AOP – Aspect Oriented Programming with Unity – Microsoft

AOP – Aspect Oriented Programming allows you to decorate all methods before and after execute them using a proxy pattern.

In this article, we will build a simple application using Unity Microsoft Framework to apply AOP.

First download Unity framework: http://unity.codeplex.com/releases

Create a console application solution and

Reference two Unity assemblies:

-Microsoft.Practices.Unity which allows Dependency injection (see an article below)

-Microsoft.Practices.Unity.Interception allowing Aspect Oriented Programming

Add to the solution two folders:

-AOP : Folder for the class used to decorate our Business class

-BIZ: Folder for a simple Business service

Create a PerformanceHandler class which implements ICallHandler interface.

Use the unity interception namespace:

using Microsoft.Practices.Unity.InterceptionExtension;

Below, we set a stopwatcher to mesure the execution time of the method called

And we print in console the interception before and after call really the method

The ICallHandler interface have an invoke method where you decorate your method (proxy pattern). You can make technical log, get the methode name decorated, set your security strategy…

Add a new class , PerformanceAttribute which implement HandlerAttribute and override CreateHandler method to return an instance of PerformanceHandler.

This class create an attribute you have to set before a method you want to decorate

in our folder BIZ, create a simple Business service and its interface:

and its interface IBizService. Note that we decorate execute method with Performance Attribute (our PerformanceAttribute class):

Now add the following code in the main program:

Execute, and you will stop at differents place where we set  a Console.ReadLine()

Before the execute method of our service is runned

Press enter key : the method is executed and new print appears after

the execution is completed and we return on our main program

, , ,

Poster un commentaire

Moles a Microsoft Framework allowing stub and Mocking

Go to Microsoft url to download Moles and Pex  – (in a new article I will discuss about Pex)

http://research.microsoft.com/en-us/projects/pex/downloads.aspx

install Moles and Pex:

Moles is a framework which allow to use:

-Stub type  :  Fake

-Mole type : in order to fake some external ressource like IO or external classes.

1/Create a library class where you will call your external ressource like an IO assembly, WCF service… :

-use an interface that your business layer will use to call the external method

-wrap in a class that implement the interface your external resource

2/Create a console application that use your business library:

In solution explorer click on the desired assembly you want to “Mole”, here MyBizService, referenced in BizConsumer and in the context menu click Add mole Assembly

a moles file appear in the solution explorer :

the content of the moles file:

Rebuild your solution  and a new assembly appears:

Double click in MyBizServiceMoles reference to explore the assembly:

Moles create two type of class for each class discovered in the assembly “ Moled”

-Stub class which name begins with S :  for replacing your component you control

-Mole class which name begins with M : to create detours on external component like IO…

Now we create  a unit test on Execute method by right clicking on this method and select create unit test:

click ok on Create Unit Test to create a new Test Project:

enter a name to the new test project:

 

In solution explorer, you see the new test project and the reference on Mole assembly ( we can add mole assembly after creating test project)

In AssemblyInfo file add a reference assembly to the type you want to mole, here BizService

add Mole Assembly to MyBizService assembly and rebuild Test project :

 

Go to AssemblyInfo file in test project to add an assembly attribute:

set the MoledType you want , it allows Moles not to mole whole assembly

which optimize Building Time (here we mole only MyExternalRessource class):

 

Finally, create a unit test with a mole type of your External resource like below (here we rewrite the implementation of method to verify that our external resource is called ( we set a lambda expression for all instances before calling the method that use our external resource):

Conclusion : the external resource is diverted using Mole

You can have some mistake using Mole with the following error message :

The test adapter (‘Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’) required to execute this test could not be loaded. Check that the test adapter is installed properly. Object reference not set to an instance of an object.

the solution is to reinstall mole framework

 

, , , , , , , ,

Poster un commentaire

MEF Managed Extensibility Framework from Microsoft – a quick sample – MEF vs Unity ?

First download MEF assembly from Microsoft url:

http://mef.codeplex.com/releases/view/79090

Managed Extension Framework, called Mef, is usefull when you want to extend your application. To do it, Mef allow Dependency injection between composant or layers.

When use Unity and Mef ?

-use Unity to do dependency injection during development when you know all components

-use MEF for components that will come in future, or when you want your application splitted in assembly that are not in the same solution or you want not to have reference (prefering to “bind” assembly) – interesting in a plugin architecture…

So, you can use both each one having its own purpose.

We will create three assemblies:

-Contracts.dll which provides all contracts

-MyBusinessComponent.dll which provides the implementation of our business

-Form1 : a winform where we will call dynamically our business layer through MEF and without any reference on the winform project

1/Create a Contracts assembly:

add to your solution a new Class library project and call it Contracts

add a IBusinessService interface

2/create our business assembly

add a second class library project called MyBusinessComponent:

add a reference on MEF assembly (System.ComponentModel.Composition.CodePlex ) in this project in order to expose our business service methode:

Create a MyBusinessService class and implement IBusinessService as below :

an export attribute decorate MyBusinessService class with the interface we want to expose (here IbusinessService). You have to add a MEF namespace

using System.ComponentModel.Composition;

3/Create a winform that will act as a presentation layer:

Add a windows form application to the solution

add a control button and a label called it LabelInfo

add a MEF reference as follows:

add also a reference only on Contracts assembly :

Now on winform code :

-Use Import attribute with the type of the interface and define a IBusinessService property

-Create a DirectoryCatalog and pass the location of your BusinessService assembly (you can use a relative path as below)

– Instantiate a compositioncontainer and pass the DirectoryCatalog: MEF will understand that all assemblies to explore are in the path of your DirectoryCatalog. MEF will get all attributes (here export of our business service) by reflection.

-Finally, call ComposeParts on form class, it enables Mef container to make the dependency mapping (using Export and Import Attribute)

all work fine and note that there isn’t any reference to BusinessService assembly.

Conclusion: in this sample, MEF is used as a third part that manage externe dependencies from different assemblies.

 

, ,

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