Friday, July 1, 2011

[Tutorial] Common Design Patterns in C# 4.0
Part7: Adapter Pattern

Pattern Name: 
Adapter Pattern

Short Description:
Match interfaces of classes with different interfaces

Usage:
Often used and easy to implement, useful if classes need to work together that have incompatible existing interfaces.

Complexity: 
1 / 5

UML Class Diagram:

image

Explanation:

  • The TradingDataImporter class acts as a client using classes with an existing Connector interface.

image

  • The abstract Adapter class defines the interface that the client class knows and that it can work with.
  • The concrete Adapter classes convert the interface of the incompatible classes into an interface the client expects. They make different existing interfaces work together.

image

  • Here are some examples of different adaptee classes that implement different interfaces. However, the client expects a generic interface that they currently don’t provide. That is why they get wrapped by the concrete adapter classes to make them compatible with the client.

image

  • In the last step we add some code to test the software design and the Adapter implementation.

image

  • When running the example you can see that everything is working as expected and that the correct classes are instantiated during runtime.
image

Source Code:

http://csharpdesignpatterns.codeplex.com/


Share/Save/Bookmark

Wednesday, June 8, 2011

[MS Days 2011] Slides of Windows Azure Session Online

Below the slides that I used for my session during the MS Days in Grenoble. The session and slides were done in French.


Share/Save/Bookmark

Wednesday, June 1, 2011

[Publication] Article in French Programmez Magazine on Entity Framework 4.1 and CodeFirst

You can find an article of 5 pages concerning Entity Framework 4.1 and CodeFirst in the French Programmez magazine No.142 written by me and Fathi Bellahcene.

image

First Page and Second Page (low resolution)

Third Page and Fourth Page (low resolution)

Fifth Page (low resolution)

The article is written in French but as always I will write some English articles on my Blog in the next weeks. So stay tuned if you are interested in getting to know the new features of Entity Framework 4.1 and the CodeFirst approach.

You can find the source code here:
http://codefirst.codeplex.com


Share/Save/Bookmark

[Tutorial] Common Design Patterns in C# 4.0
Part6: Singleton Pattern

Pattern Name:
Singleton Pattern

Short Description:
Class with only one single possible instance

Usage: 
Often used for objects that need to provide global access with the constraint of only one single instance in the application

Complexity:
1 / 5

UML Class Diagram:

image

Explanation:

  • There are multiple ways of implementing the Singleton Pattern in C#. I am going to explain the most efficient approaches that also provide thread safety.
  • Note that in the first example the default constructor is defined as private so that it is not possible to instantiate the object from the outside. The class is also marked as sealed which means that inheritance is not allowed for it.
  • The internally instantiated object is stored in a private variable that is marked as static. The public static property GetInstance is used to allow access to this object. It contains the business logic that assures that only a single instance can ever exist.
  • To achieve this behavior a lock strategy is used to assure that only a single thread is allowed to do the null check and create a new instance if it does not already exist.
  • A test function DisplayConfiguration() was added to be able to verify the class design.

image

  • Note that in the second example the default constructor is also defined as private so that it is not possible to instantiate the object from the outside. The class is also marked as sealed which means that inheritance is not allowed for it.
  • This time a private nested class is used to provide access to the instance that must only exist once in the application.
  • This nested class has a static default constructor and an internal static read-only instance of the object. Only the container class has access to the instance which will due to the software design (auto-instantiated and marked as read-only) only exist once.
  • A test function DisplayRules() was added to be able to verify the class design.
  • I recommend using this example as your default approach for your Singleton implementations since there is no locking in it which makes it more efficient in terms of performance.

image

  • In the last step we add some code to test the software design and the Singleton implementation.

image

  • When running the example you can see that everything is working as expected (it is however not as simple to test that there really is just one single instance, you just have to believe in the correct software design).
image


Source Code:

http://csharpdesignpatterns.codeplex.com/


Share/Save/Bookmark

Tuesday, May 17, 2011

[C# and Language] Compiler as a Service (CaaS)

Lately I showed you the new async features of the next version of C# 5.0, which are already quite compelling. But Microsoft is already working on the language features that will come after the next version. These features are called Compiler as a Service (CaaS).

Everything is still in very early stage so I cannot give you any exact information, but I may give you the concepts and ideas behind it.

It will be possible to evaluate expressions at runtime and to inject them into your code. This allows for very interesting scenarios where parts of your business logic and business rules may be stored outside of your code (in the DB or in XML file for example) and be modified an extended independently.

An example could be:

image

Other use cases consists of having the possibility to translate from one language into another very easily from within Visual Studio. You could for example select code parts in C# and choose F# as the language to translate to. The CaaS feature would then scan the C# code and generate the corresponding F#  code.


Share/Save/Bookmark

Sunday, May 8, 2011

[MS Days 2011] Speaker at Microsoft Days 2011 in Grenoble
Session on Windows Azure

I am going to be speaker and animate a session with Fathi Bellahcene on Windows Azure during the Microsoft Days 2011 in Grenoble the 7th June ! So if you have the time and if you are around don’t hesitate and come to see us in action !

image


Share/Save/Bookmark

Saturday, May 7, 2011

[Tutorial] Common Design Patterns in C# 4.0
Part5: Prototype Pattern

Pattern Name:
Prototype Pattern

Short Description:
Clone or copy initialized instances

Usage: 
Easy pattern, usage depends on the preferred software design, provides an alternative to the other creational patterns that are mainly based on external classes for creation

Complexity:
1 / 5

UML Class Diagram:

image

Explanation:

  • The abstract prototype class defines the interface that contains a clone method for cloning itself.

image

  • The inherited classes implement the clone function. Note that C# provides two different ways of cloning an object: by shallow copy (only top level objects) or by deep copy (all objects). 
  • You could also manually create a new object and set its values with the values of the original object but since C# already implements everything necessary I advise using that.

image

  • The client class does not create new objects itself. Instead it asks the objects to clone themselves when needed. The cloned objects are perfect copies and contain all values of the original objects depending on the shallow or deep copy approach (see above).

image

  • You may also use the ICloneable interface that already exists in C#

image

  • In the last step we add some code to test the software design and the Prototype implementation.

image

  • When running the example you can see that everything is working as expected and that the correct classes are instantiated during runtime.

image

Source Code:
http://csharpdesignpatterns.codeplex.com/


Share/Save/Bookmark

Friday, May 6, 2011

[Tutorial] Common Design Patterns in C# 4.0
Part4: Factory Method Pattern

Pattern Name:
Factory Method

Short Description: 
Create instances of derived classes

Usage:
Frequently used, fairly easy to implement and useful for centralizing object lifetime management and avoiding object creation code duplication

Complexity:
1 / 5

UML Class Diagram:

image

Explanation:

  • The abstract creator implements a factory method that returns an objects. It also contains a method for testing purposes that serves to validate the design.
  • Each concrete creator overrides the abstract factory method and returns a specific object concerning on the context.
  • In this example the factory method is used internally to set a property but it could also be used in an external context for creating objects when needed.

image

  • The abstract class defines the interface and class structure for all objects that get build by the specific concrete creators via their factory methods.

image

  • There is also the possibility to create a C# specific solution that uses generics which also results in a valid factory method.

image

image

  • In the last step we add some code to test the software design and the Factory Method implementation in the language agnostic and in the C# specific versions.

image

  • When running the example you can see that everything is working as expected and that the correct classes are instantiated during runtime.

image

Source Code:
http://csharpdesignpatterns.codeplex.com/


Share/Save/Bookmark

Thursday, May 5, 2011

[Tutorial] Common Design Patterns in C# 4.0
Part3: Builder Pattern

Pattern Name: 
Builder Pattern

Short Description:
Separate representation and  object construction

Usage: 
Rarely used, only useful if complex objects consisting of multiple parts need to be constructed (composite objects for example)

Complexity:
1 / 5

UML Class Diagram:

image

Explanation:

  • The director (ComputerShop) implements a method that is responsible for the sequence of steps of an object creation process. It takes an abstract builder class as input parameter and delegates the real creation to it.
  • The abstract builder class defines the interface that all inheriting concrete builders will use for object creation.

image

  • The concrete builder implementations contain the parts that are assembled and that build the objects.

image

image

image

  • The final object contains all different parts that get assembled by the concrete builder classes. Those may differ from each other depending on the implementations.
  • A method was added that prints out the characteristics of the different parts to be able to validate the design.

image

image

  • In the last step we add some code to test the software design and the Builder implementation.

image

  • When running the example you can see that everything is working as expected and that the correct classes are instantiated during runtime.

image

Source Code:
http://csharpdesignpatterns.codeplex.com/


Share/Save/Bookmark

Wednesday, May 4, 2011

[Visual C# 5.0] CTP-SP1: Refresh of the new Async features containing optimizations, additions and bugfixes

Microsoft recently released a new version of the async features in a CTP-SP1 version. The goal remains the same: enable developers to write asynchronous code in an easy way, so that they do not need to learn any new skills and writing asynchronous code gets as straightforward as writing synchronous code.

The initial pattern of using Task<T> (class that was introduced in .NET 4 with the parallel features) is still the basis for the new async features. Nothing changed concerning that and allover there are no major changes. There are however multiple optimizations, additions and bugfixes (close to 400 bugs were found in the first CTP).

New features and modifications:

  • Visual Studio 2010 SP1 compatibility (the previous CTP only worked with Visual Studio 2010)
  • Support for Windows Phone 7 development
  • The old pattern of using GetAwaiter | BeginAwait | EndAwait was replaced with the new pattern GetAwaiter | IsCompleted | OnCompleted | GetResult that provides better performance and makes fast path more efficient

Some of the bugfixes concern:

  • Possible race conditions in finally blocks
  • Equality tests with Nullables could sometimes evaluate the operands more than once
  • Wrong behavior when accessing the base.property in an async method
  • Several Visual Basic .NET specific bugs

Start using it in your incubation projects to see how you can integrate it into your developments (especially Phone developments). Try it out !! But mind that this is still a CTP version that might evolve and change until the final version and that there is no support. So using it in any production projects is not advised.

You can find the download of the CTP-SP1 here:

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=4738205d-5682-47bf-b62e-641f6441735b&displaylang=en


Share/Save/Bookmark

Sunday, May 1, 2011

[Tutorial] Common Design Patterns in C# 4.0
Part2: Abstract Factory Pattern

Pattern Name:
Abstract Factory Pattern

Short Description:
Create instances of classes belonging to different families

Usage:
Very frequently  used and very useful

Complexity:
1 / 5

UML Class Diagram:

image

Explanation:

  • The abstract factory class defines the abstract methods that have to be implemented by concrete factory classes. It serves as interface and contract definition.
  • The concrete factory classes contain the real implementation that define which classes are created during run-time.
  • Note that the methods return values are also defined by abstract classes, this allows a high flexibility and independence, leading to methods that must only be implemented once.
  • The returned classes are however specific to each concrete factory class (you will see their implementation below).

image_thumb1[2]

  • Here you see the abstract classes that are used during the creation process (a method was added that serves to prove the validity of the design).
  • Based on the abstract classes some real example implementation are created, those will be instantiated during run-time, depending on the concrete factory that creates them.

image_thumb5[1]

  • When implementing the associations between the Driver class, the abstract factory class and the abstract classes you may either use the common language agnostic approach using only private members (which is the most memory efficient one).

image_thumb3[1]

  • Or you may use the C# language specific approach where everything is wrapped using private properties. This allows adding logic when accessing or changing the private members but might be a little overkill.

image_thumb11[1]

  • You may also use another C# language specific solution that uses generic classes to create objects and that is also a valid implementation for the abstract factory pattern.

image

  • In the last step we add some code to test the software design and the Abstract Factory implementation.

image

  • When running the example you can see that everything is working as expected and that the correct classes are instantiated during runtime.

image

Source Code:
http://csharpdesignpatterns.codeplex.com/


Share/Save/Bookmark

Saturday, April 30, 2011

[C# - PRISM 4.0] Building highly flexible and modular applications

I already showed you some time ago how to create good and efficient code by using the S.O.L.I.D. design principles. This time I would like to focus on PRISM 4.0 and its features.

The goal for good software architecture is always the same : produce code that is flexible, highly modular, easy to maintain, simple to extend and overall as independent as possible.

This can very well be achieved by applying best practices and having good programming skills but it will get even more powerful when using existing frameworks that already contain all foundations and have all necessary patterns in place. One of the most mature and advanced frameworks is PRISM 4.0 (patterns & practices) which will allow you to be much more productive when correctly used.

image

Prism 4.0 provides guidance to help easily design and build rich, flexible, and easy-to-maintain applications. It works with Microsoft .NET Framework 4.0 and Silverlight 4, the latest technologies currently available.

It especially works well with the following project types:

  • Silverlight Applications
  • WPF Applications
  • Windows Phone 7 Applications

Furthermore it support MVVM and MEF and is open for usage with different dependency injection containers (by default it uses Unity). It is build from ground up using Design Patterns that favor separation of concerns and loose coupling which allow Composite Applications.

The goal is to partition applications into a number of discrete, loosely coupled, semi-independent components and modules that can be individually developed, tested, and deployed by different subteams.

image

PRISM 4.0 helps to achieve a very clean separation between UI and Business Logic. Efficient reuse of existing functionalities and a clean separation of concerns between horizontal capabilities (logs, custom authentication, etc…) can be very quickly attained. When using MEF it is also extremely easy to create modular applications where functionalities can be dynamically added (even during runtime).

The existing classes and functionalities really help very much and provide a robust basis on which you can build on. The concentration can really be on the business functionalities and not the surrounding shell.

image

image

Needless to say that you will have to familiarize yourself with the framework and look into examples on how to use it correctly. You should also decide if you really need to be so modular and if you will have an added value because for monolithic and simple applications it might not be advisable to have such an approach. And surely you could also build everything yourself but you might take a look and evaluate if it is usable in your context. Did I mention that it is free ? So don’t hesitate and look for yourself and try it out, it might help you to build better applications quicker, with less stress and for less money !


Share/Save/Bookmark

Thursday, April 28, 2011

[C# and Language] DevLabs Website

Microsoft is working constantly on new incubation projects to extend existing language features and add new ones. Some of the early versions of those new features can be tested on the DevLabs website. When they are considered to be accepted and they have finished their incubation period they are moved to the respective product sections (as it was done for the Reactive Extensions) or are directly integrated into the corresponding products such as Visual C#.

image

Currently there are the following incubation projects available:

  • TC Labs: Solver Foundation (build and solve real optimization models)
  • TC Labs: TPL DataFlow (extensions to the .NET 4.0 TPL addressing additional scenarios)
  • TC Labs: Dryad (process large volumes of data in many types of applications and enable LINQ on HPC systems)
  • TC Labs: Sho (connect IronPython scripts with compiled .NET code for fast and flexible prototyping)
  • Doloto (AJAX Download Time Optimizer)
  • Code Contracts (extensions to the existing .NET 4.0 version)
  • Axum (parallel applications development based on the actor model)
I am personally currently looking into TPL DataFlow, CodeContracts and Axum (since some time already) and will take some time to give you feedback on my experience in the next weeks.


Share/Save/Bookmark

Thursday, April 14, 2011

[Tutorial] Common Design Patterns in C# 4.0
Part1: Introduction Gang of Four Design Patterns

Design Patterns provide standardized and efficient solutions to software design and programming problems that are re-usable in your code. Software Architects and developers use them to build high quality robust applications.

However, you have to take care to select the right pattern for the right problem. If you need to modify the initial pattern too much, this may indicate that it is not adapted to your needs and may in the worst case lead to unmaintainable, complex and inefficient code. The opposite of what you intend when using Design Patterns !!

You may also create your own custom Design Patterns. Whenever you come up with a certain solution that is reusable in a vast majority of your projects, you may decide to abstract a design pattern out of it. Then you may create your own library of patterns and share them within your whole company, thus creating standards and ameliorating maintainability.

In this series of blog posts I am going to show you how to use the well known Gang of Four (GoF) Design Patterns in C# 4.0 code. Those patterns will work well in any project that uses C# but especially in WPF, WCF, WinForms, ASP.NET projects.

The GoF Design Patterns are divided into 3 categories : Creational Patterns, Structural Patterns and Behavioral Patterns. In my following blog posts I am going to explain each GoF Design Pattern in detail and will show you examples of how to write good C# 4.0 code that implement those patterns.

Creational Patterns

  • Abstract Factory: Create instances of classes belonging to different families

  • Builder: Separate representation and  object construction

  • Factory Method: Create instances of derived classes

  • Prototype: Clone or copy initialized instances

  • Singleton: Class with only one single possible instance

Structural Patterns

  • Adapter: Match interfaces of classes with different interfaces

  • Bridge: Separate implementation and object interfaces

  • Composite: Simple and composite objects tree

  • Decorator: Dynamically add responsibilities to objects 

  • Facade: Class that represents subclasses and subsystems

  • Flyweight: Minimize memory usage by sharing as much data as possible with similar objects

  • Proxy: Object that represents another object

Behavioral Patterns

  • Chain of Responsibility: Pass requests between command and processing objects within a chain of objects

  • Command: Encapsulate a method call as an object containing all necessary information

  • Interpreter: Include language elements and evaluate sentences in a given language

  • Iterator: Give sequential access to elements in a collection

  • Mediator: Encapsulates and simplifies communication between objects

  • Memento: Undo modifications and restore an object to its initial state

  • Observer: Notify dependent objects of state changes

  • State:Change object behavior depending on its state

  • Strategy: Encapsulate algorithms within a class and make them interchangeable

  • Template Method: Define an algorithm skeleton and delegate algorithm steps to subclasses so that they may be overridden

  • Visitor:  Add new operations to classes without modifying them

There are also many other types of pattern such as: Parallel Patterns, SOA Patterns, Enterprise Architecture Patterns, etc… So if you work in the respective area don’t hesitate to look up patterns that may help you to be more efficient and build better applications.


Share/Save/Bookmark

Thursday, April 7, 2011

[MVP Nomination] Happy to be awarded as MVP Visual C# !!!

Beginning of the month I got an email from Microsoft containing that I am awarded Microsoft MVP Visual C#. After working in the industry since soon more than 14 years I am very happy and honored for being selected and proud to call myself MVP !!!

MVP

I want to take some time and thank Loic Baumann (MVP Visual Studio ALM) and Michel Perfetti (MVP Visual Studio ALM) for their support as well as my MVP Lead for having me nominated.

Also a big thank you to Microsoft for putting their trust in me. I will do my best and continue sharing my knowledge with the community. So be prepared for many new blog posts and articles in the technical press on Software Architecture & Design, Visual C# and Windows Azure, which are my main interests. I will also concentrate on speaking much more at conferences so I would be glad to see you at any Microsoft events (TechDays, MS Days, etc…).

Who knows, I may even write a book on Software Architecture if I find the time. I really would love to do that. So stay tuned and continue following me if you like this Blog !


Share/Save/Bookmark

Friday, February 25, 2011

[Certifications] MCPD: Windows Developer 4, MCPD: Web Developer 4 and MCPD: Azure Developer 4

Just to let you know that I passed the upgrade exams 70-521 and 70-523 to upgrade my MCPD: Enterprise Application Developer 3.5 certification lately.

I am happy to announce that I am now certified MCPD: Windows Developer 4 & MCPD: Web Developer 4 !

I also received the information that I have passed the BETA exam “71-583 PRO: Designing and Developing Applications for Windows Azure” that I did in November 2010.

So I am MCPD: Windows Azure Developer certified as well !!

image


Share/Save/Bookmark