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