Monday, November 14, 2011

Programmer Beekiping

Programmers are like bees :) "You can't exactly communicate with them, but you can get them to swarm in one place and when they're not looking, you can carry off the honey". This quote is from a great article by Orson Scott Card: How Software Companies Die. A must read for all who wants to know how to scarry away good programmers and kill a software business.

Scritchy - CQRS without the plumbing?

There is a new lightwight CQRS framework/library that claims to allow quick building of CQRS applications for beginners or intermediate developers: Scritchy. It's a .NET project. The source code can be found at https://github.com/ToJans/Scritchy. There is also an example of a stocklist app that uses Scritchy: http://scritchyexample.apphb.com/. On that page there is also a video that shows how to build a CQRS application within few minutes using Scritchy. Since there is a widespread opinion that CQRS is hard to understand and an overkill for most applications let's hope that this micro framework can help to encourage developers to delve into CQRS.

Tuesday, September 27, 2011

Change the Target Framework for all Projects in a Solution

There is a great Visual Studio macro written by Scott Dorman that can be used to automate the changing of the target framework of all projects in a visual studio solution: Visual Studio 2010 and Target Framework Version. The macro is available on Scotts Sky Drive: ProjectUtilities.vb. Download it to \Documents\Visual Studio 2010\Projects\VSMacros80\MyMacros. Then open the the Macro IDE (Alt+F11), add the downloaded .vb file to MyMacros and run it. The solution must be opened in Visual Studio when you run this macro of course. The macro works well with 4.0 as target version (haven't tested it with other framework versions yet). Have fun!

Monday, July 11, 2011

Barrelfish: A New Open Source OS

On 8th july a new operating system called Barrelfish was released by ETH Zurich in Switzerland, with assistance from Microsoft Research. The source code is available in a Mercurial repository at http://hg.barrelfish.org. The system seems to be optimized for multicore architectures.

Thursday, June 16, 2011

DDD Exchange 2011

Videos of sessions from DDD Exchange 2011 are now online on skillsmatter.com: DDD Exchange 2011 SkillCast videos. Great stuff! Definitely worth watching! Among others Udi Dahan talking about Composite Applications and Domain Models and showing how to turn monolithic applications with big and cluttered models into composite applications with lean domain models using Bounded Contexts. Also Greg Young showing great things you can do with tests and Event Sourcing: Assert.That(We.Understand).

Saturday, May 21, 2011

Speeding up WCF Discovery

With the version 4.0 of the .NET framework Microsoft has enriched WCF with an implementation of the WS-Discovery protocol. Discovery allows WCF services to announce their availability and client applications to find these services at runtime without knowing the endpoint addresses. The WCF Discovery supports two modes the managed and the ad-hoc mode. In the managed mode there is proxy server that manages the information about available services. With the ad-hoc mode you don't even need a centralized server! In this mode services and client applications use multicast messages to announce their availability or to query available services. For more information about WCF discovery see WCF Discovery Overview on MSDN. This site also provides some usage examples.

In my opinion the second discovery mode (ad-hoc) is more interesting for loosely coupled distributed scenarios because you don't have to bother about a central point of failure (the discovery proxy). But playing around with WCF discovery I noticed that it takes some time on the client to discover suitable services. It's not really a big problem because you would usually do it only once e.g. during the application startup and cache the proxy or the endpoint address somehow. But if you only need only one service instance with a certain contract and you don't care which instance it is (if there are many of them) you can considerably reduce the time needed to discover the service address. You just tell the framework that you only need one result. To do so you have to setup the FindCriteria accordingly:
// Create DiscoveryClient
var discoveryClient = new DiscoveryClient(
                            new UdpDiscoveryEndpoint());
var criteria = new FindCriteria(typeof(IContract));
criteria.MaxResults = 1; // we only need one instance!
var findResponse = discoveryClient.Find(criteria);
If there is a service instance wich implements IWantedService the Find()-call would return much sooner than without adjusting the MaxResults property.

Monday, May 16, 2011

Unity: passing constructor parameters.

Unity is a dependency injection container from Microsoft. The version 2.0 is now available for download at codeplex: patterns & practices - Unity. You can use it to reduce dependencies in your .NET applications. Say you want to decouple your 'high level' code from the implementation details of the data access. With Unity you can define an interface like IDataContext and register the actual implementation in the Unity container:
var container = new UnityContainer();
container.RegisterType<IDataContext, SqlDataContext>();
In the 'high level' code you don't have to know the concrete type that implements the data access but just the contract: IDataContext. Unity will resolve the implementation you have registered at runtime:
var dataAccess = container.Resolve<IDataContext>();

Pretty easy so far but what if you want to pass a parameter to the constructor of your IDataContext implementation, e.g. a connection string? Just pass the value you want to be injected as a parameter when registering the type:
container.RegisterType<IDataContext, SqlDataContext>(
  new InjectionConstructor(connectionString));
Unity will pass the value you have defined here to a matching constructor of SqlDataContext when you will request an instance of it.

Wednesday, May 11, 2011

Dryad & DryadLINQ - academic release

An academic release of Dryad and DryadLINQ is available for download on Microsoft Research site: Dryad and DryadLINQ Academic Release.
Dryad is a distributed computing engine for clusters running Windows HPC Server 2008. DryadLINQ allows to use LINQ programming model in distributed application running on Dryad.

Friday, May 6, 2011

Parallel programming with .NET 4.0

Microsoft published 12 articles on parallel programming with .NET 4.0. You can download them here: Articles on Parallel Programming with the .NET Framework 4. These articles are mostly about TPL, PLINQ and thread safe data structures introduced with .NET 4.0.

Syntaxhighlighting for Blogger

Added syntax highlighting for the code snippets in my postings. Thanks to Heisencoder for his tutorial!

Friday, April 15, 2011

ReSharper's Object Initializer Quick Fix and IDisposable

This week I had to realize that not every Resharper suggestion is really useful. Some quick fixes carelessly applied can even introduce flaws into your code. The quick fix I have in mind is Resharpers suggestion to use an object initializer instead of creating an instance with subsequent assignments to its properties. Applying this quick fix to following code:
var person = new Person();
person.Name = "John";
person.Lastname = "Doe";
would change it to:
ver person = new Person {
     Name = "John", Lastname = "Doe" };
This quick fix is really handy and can imho improve the readability of the code. But it should not be applied when the type is an IDisposable! Why? An object initializer just creates a temporary instance of Person initializes the properties and then assignes this temporary variable to the variable person. Something like that:
var temp = new Person();
temp.Name = "John";
temp.Lastname = "Doe";
var person = temp;
The problem is that if an exception is thrown during the properties initialization you have no chance to dispose the new instance because you have no access to that temporary local variable. It's created by the compiler and is therefore not visible in you c# code. Even if you put the object initializer inside a try catch block or an using directive you will have a problem: http://ayende.com/Blog/archive/2009/01/15/avoid-object-initializers-amp-the-using-statement.aspx.

Things learned:

  1. Never use object initializers with types implementing IDisposable
  2. Resharper is a great tool but you should never apply quick fixes blindly. Think for yourself. Don't let the tools do the thinking for you ;)

Tuesday, March 8, 2011

ReSharper: remove unused references.

Today I was cleaning up unused references in a Visual Studio solution. Thanks to ReSharper it was not as tedious as I fought at first. Another great feature provided by this tool saved me a couple of minutes (or maybe hours):  http://www.jetbrains.com/resharper/features/navigation_search.html#Find_ReferencedDependent_Code.
To use it select the referenced assembly in the solution browser and choose "Find Code Dependent on Module" from the context menu. If the assembly isn't used anywhere in the current project ReSharper will show a message box saying: "Code dependent on module XXXXXXX was not found". In this case you can remove this reference safely.

Friday, March 4, 2011

SQL or NoSQL: The Hybrid.

It seems like a coexistence of SQL and NoSQL worlds is possible now. The folks from DeNA succeeded in creating a hybrid of both approaches. The MySQL plugin HandlerSocket turns the InnoDB engine into a NoSQL database. HandlerSocket allows to benefit from mature RDBMS storage engines and the lightweight NoSQL protocol that abandons the SQL processing. The results are very promising: with 750,000 queries per second the HandlerSocket achieves a considerably better throughput than the standard MySQL (105,000 qps).
For details on this benchmark and more information about HandlerSocket see Yoshinori Matsunobu's blog: http://yoshinorimatsunobu.blogspot.com/2010/10/using-mysql-as-nosql-story-for.html.
And last but not least: HandlerSocket ist open source. You can grab the code from github: https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL.

Tuesday, March 1, 2011

Magic GUIDs, ReSharper and more...

Here are some tricks I discovered today:


  • Magic GUIDs: Creating unit tests in Visual Studio is quite easy when you are doing it 'the right way'. But sometimes when I want to write tests first and create a new project for them I mistakenly choose the class library project type instead of a unit test project. However these are two different project types so when I try to run my tests from the class library project the Visual Studio won't find them. Fortunately you can convert an existing visual studio class library project to unit tests project: http://blog.drorhelper.com/2008/12/how-to-convert-net-class-library-to-ms.html
  • ReSharper: There is a great new feature in ReSharper 5.0 called value tracking.
  • Another nice ReSharper feature useful when you want to restructure you code: Move code elements up and down: [Ctrl]+[Alt]+[Shift]+[Up|Down]. You can move single lines but also entire methods.
  • Sourcemaking: A website with lots of stuff about design pattern, antipatterns and refactoring: http://sourcemaking.com/

Monday, February 7, 2011

Dynamic base address for a WCF service

Last week I've learned a nice 'trick' that allows to specify a dynamic base address for a WCF service in the configuration file. When you want to run a WCF service you need to specify the base address for the service host. You can do this in a config file or directly in your code when creating a ServiceHost instance: http://msdn.microsoft.com/en-us/library/ms733749.aspx.
Now if you are deploying your service to many machines you must either adjust the host name for each machine during/after the installation or you set the host name to 'localhost'. The problem with the last option is that if you are also exposing metadata then the MEX client will get some endpoint addresses with 'localhost' as host name and will try to call this service on its local machine unless you adjust the host name while creating a proxy.
A handy solution for this problem is to use a so called 'weak wildcard' for the host name part of the base address:
<system.serviceModel>
  <services>
    <host>
      <baseAddresses>
        <add baseaddress="net.tcp://*:1234/svc" />
      </baseAddresses>
    </host>
  </services>
</system.serviceModel>
That's it. The framework will replace the wild card with the name of the host the service is actually running on.
Edit: The solution works only with base-addresses not with endpoint addresses!

Wednesday, January 19, 2011

WF: The most useful event.

Few weeks ago a discovered the WorkflowRuntime.ServicesExceptionNotHandled event. This event can save you a lot of time debugging strange behavior of workflow instances. In my case all instances of a workflow didn't complete after an 'insignificant' change in the workflow class. No exceptions have been thrown by the runtime or the workflow code and the workflow instance just didn't failed or complete. After some 'googling' I came across the ServiceExceptionNotHandled event. So I registered a handler and traced the not handled exception. It turned out that the persistence service has thrown an exception each time it tried to save the workflow state in the database because one member of this workflow was not serializable. Without that event I would still be debugging that workflow :)