Category: WPF

NET Event–Migrating WinForm application

Last month (21st of July 2011) I spoke at the Bermuda NET Event “Migrating WinForm applications to WPF/Silverlight”. This has been for me the first NET Event in Bermuda but for sure it will not be the last one. We are planning to have a new event this autumn where we will touch other topics like: ALM, Parallels and more.

Thanks to Alessio Bellisomi, the web developer that works in my company, I am able to share with you some nice pictures of the event and three short movie of the event. Unfortunately, for this event, we were not super organized so I do not have with me the full video of the entire event. I promise that for the next event we will provide the entire video and maybe we will be also able to stream the content.

The audience, considering that we were in Bermuda and considering that it was end of July, was more than expected. I believe we were around a 30/40 ish attendees. The feedback has been really positive and we expect a very fast grow of this UserGroup by having more events and more speakers.


The pictures are available through Google photos



The videos have been hosted on my Vimeo account and they can be viewed through their web site

Bermuda WinForm event, part 01 from Raffaele Garofalo on Vimeo.

Bermuda WinForm event, part 02 from Raffaele Garofalo on Vimeo.

Bermuda WinForm, part 03 from Raffaele Garofalo on Vimeo.


Special thanks need to go to Sandra de Silva (president of the UG and owner of Nova Ltd) and to the other members of the committee. I want also to thank all the participants and I hope this autumn there will be more!

Speaking about WPF in Bermuda.

Last year in Bermuda we had a new born, a NET community. The community has been created by some locals companies to attract developers, architects and analyst; but also anybody passionate of development.

You can check-out the web site here:

This month I will have the opportunity to present my two books:

and to talk about WPF/Silverlight.

During this event I will explain what are the common problems you may encounter when moving from legacy applications to WPF/Silverlight. When you should and when you should not migrate an application to a new UI technology. When you should use WPF and when you should use Silverlight. Oh and I will also give some free copies of my books plus some additional discounts.

This is the page of the event:

If you are planning a vacation around the 21st of July, please come here and join us in this first event about WPF/Silverlight.

Hope to see you there!

PS: I forgot to mention that one of the amazing price for the attendees is to win a 1 year subscription to MSDN.

Applied WPF in context is out.

During the last two months I have been away from Bermuda and I didn’t have a lot of time to write or post any news on this blog.

In the meantime, I didn’t realize that my new book from APRESS: “Applied WPF 4 in context” has been published so it is time to post some info about it.

The cover of the book is following the APRESS new style and it is the following one:


The new series “applied in context” of APRESS deliver content by example and practical code, so in this book you will realize a full working WPF 4 application using all the available tools of Microsoft like: Visual Studio 2010, Expression Blend 4 and SQL Server 2008 R2.

Who this book is for?

This book is for Windows application developers who want to understand the context in which WPF sits and the standards and best practices that can be employed to improve the efficiency and maintainability of their projects. This book can be used by a junior developer to learn WPF and understand how to architect a layered application, and it can be used also by a senior developer as a reference for developing scalable WPF applications.

Table of content.

Following is the table of content of the book:

  1. Introducing WPF and XAML
  2. Sample Application: Overview and Getting Started
  3. Microsoft Expression Blend
  4. Creating the Views
  5. Adding Controls to the Views
  6. The Entity Framework
  7. Data Binding
  8. Command Handling and Event Routing
  9. Testing with TDD   
  10. Reports with Microsoft Reporting Services
  11. Deploying the Application Using ClickOnce
  12. Design Patterns in WPF
  13. WPF and Multi-Threading
  14. Interacting with WCF


The book is released and published by APRESS so in this case the book can be bought from the APRESS web site or from one of their distributors. Right now, Amazon has already run out of copies so you should expect the next distribution starting the 7th of June for the Paperback copy. You can still download the e-book from or APRESS. I am not sure I will distribute this book also with Kindle as I had some issues with the previous one so for now it won’t be available directly in the Kindle web site or in the iBook application for iPad.

Right now you can buy the book here:

The are also other distributors that I do not personally follow but I believe that buying the book directly from APRESS will give you the best price/availability options.


This is the list of the official prices, if you find the book with a different price from a different distributor than the one listed in the previous section I am not aware about it, so please let me know if you believe someone is distributing the book with a non fair price.

Print version with shipment from APRESS including TAX: 49.99 USD

E-book version from APRESS including TAX: 34.99 USD

Enjoy and as usual, let me know what you think about it! Hot smile

Updates for April/May 2011

Note: Starting from the 15th of April 2011 until the 6th of
May 2011 I will be off-island. During this period I will not have access to the
internet so I will not able to approve you comments or either to read and answer
them. You can try to send me an e-mail using the contact form but I can’t
guarantee you that I will be able to read it.

I have received many queries about my first book on LOB applications so in this
post I will try to answer to most of them. Please forgive me in advance if I
can’t answer to all your queries.

  • Source code
    Many of you are wondering why the source code of the demo
    application for the MVVM book is “skinny” and why I didn’t provide a full
    working application.
    The book has been released and the work behind it is
    huge. This book is supposed to be a guideline for a senior developer on how to
    architect a LOB application so in my opinion it was not necessary to provide all
    the code required to make a fully working application. I mean the application
    works but I left the “finishing touch” to the reader. Anyway, due to the high
    demand, I will work on this and during the summer I will post new versions of
    the app probably on CodePlex so that I can be an Open Source project where each
    dev interested can put some effort on it.
  • APRESS WPF Book vs MS MVVM book
    Why Raf wrote two books and not one? First
    let me say that Microsoft and APRESS are two separated
    companies; my “MVVM” book has been published by Oreilly for Microsoft Press
    while my APRESS book will be published by APRESS … The first one talks about
    “Architecting a LOB application and the MVVM pattern” while the second one
    “teach you from scratch a layered WPF application”. If you want a reference to
    layer an application you have to buy the first book,
    especially if you are new to the topic (layering)
    , if you want to learn: WPF,
    MVVM, Entity Framework, threading on WPF and more you have to buy the second
    one, event if you are a middle expert WPF developer.

I do not like to blown my own trumpet and who has worked with me can confirm
that, but, if you are interested in learning WPF and learning how to architect a
LOB application you have to buy both books as just one will not cover all the
topics you need to master this technology. I would personally buy the MS PRESS
book, read it once, then buy the APRESS WPF book, read it and build the sample
application, then read again the MS PRESS one.

 The final note is about the comments. The book is on and
and as an author I would kindly appreciate if you can post a comment that
explains what you liked and what you didn’t like about the books. Posting a
comment with 2 stars saying “the source code is not available” while it is on, it is just silly and doesn’t help anybody … If you have a problem
with the source code, feel to contact me, even if you disagree with some of my
ideas exposed in my books. I have a wide open mentality and I love to have
“constructive discussions” with my readers.

Updates for my Microsoft Book

After one week that my book about LOB applications has been published I started to receive some additional questions that I am trying to address in this post.

First of all I want to thank all the guys and girls that are buying the book and all the people that are providing feedbacks for the book. I really appreciate. I want also to specify that the Microsoft book has not been released as a “Book about MVVM” but more as a “book to discover LOB and layered applications”. I believe that part of the misunderstanding as been caused by the book’s title but we wanted to specify the MVVM keyword in the title because the book spent two chapters on it.

The source code has been published and you can find in the book the correct address that will point you to the download. I want to thanks Ted Anderson that on has notified me about the error done by OReilly in the publishing address. The source code is available here:­ Please do not download the code and pretend to get explanations from my if you didn’t buy the book yet … Winking smile

The T-SQL to generate the database is not necessary. When you will open the Visual Studio 2010 solution, you need to run the Test show here:

TDD for generate the SQL Datab
  1. [TestFixtureSetUp]
  2. public void CanCreateDatabaseSchema()
  3. {
  4.     try
  5.     {
  6.         var cfg = new Configuration();
  7.         cfg.Configure();
  8.         cfg.AddAssembly(Assembly.Load(“CRM.Dal.Nhibernate”));
  9.         new SchemaExport(cfg).Execute(true, true, false);
  10.     }
  11.     catch (Exception exception)
  12.     {
  13.         Assert.Fail(exception.ToString());
  14.     }
  15. }
  17. [Test]
  18. public void CanGetAUnitOfWork()
  19. {
  20.     try
  21.     {
  22.         ISessionFactory factory = new SessionFactory();
  23.         IUnitOfWork unitOfWork = factory.CurrentUoW;
  24.         Assert.That(unitOfWork, Is.Not.Null);
  25.         Assert.That(unitOfWork.Orm, Is.Not.Null);
  26.     }
  27.     catch (Exception exception)
  28.     {
  29.         Assert.Fail(exception.ToString());
  30.     }
  31. }



Also remember that the source code has been created only to show you some practical examples of how to layer a LOB application, how to use NHibernate or Entity Framework with the same data layer and how to architect the MVVM pattern. If you want to use the application in a production environment, you have to spent additional time on it in order to get a final product.

For any other additional information I am here.

Please if you find errors or mistakes in the book, I would really appreciate if you can post an errata corrigge in the corresponding section of the OReilly web site:

Next month I will publish my second book “Applied WPF in Context” with APRESS; in that book you will find whatever you need to learn WPF and the MVVM pattern.

Stay tuned!

Source code for my MVVM book.

This post is an update for all the enquires I got about the source code for my MVVM book published few days ago. (Please follow this thread: MVVM book).

The book was planned to be published for the first week of April 2011 but it was already published this week due to the high demand we received from the customers.

Unfortunately the source code of the demo application is under polishing process in these days and it will be available next week.

Please forgive me for this and stay tuned, I will update this post as soon as the source code will be released.

Thank you

My book for Model View ViewModel (MVVM) and n-tier applications is out

(a.k.a. Buy one copy!)

Finally I am excited and glad to let you know that my first book: “Building Enterprise Applications with Windows® Presentation Foundation and the Model View ViewModel Pattern” is out and available through the major IT books distributors like:, kindle, and more.



Please forgive me if I am missing some of my distributors but I do not have yet the full list. The book is available through the oReilly website at this address: in the following formats: PDF, EBOOK, KINDLE and PAPERBACK (hard copy); of course the hard copy costs a little bit more.

You can also order the book using at this address; unfortunately, Amazon will not start to deliver the book until the 1st week of April 2011 while if you order the book from the oReilly web site, you can get it right away.


On amazon, the hard copy costs $19.99 and the Kindle version costs $14.99.

On oReilly the hard copy is $19.99, the Ebook is $14.99 and both together cost $21.99

Free Copies

I have assigned some free copies to the participants of the last UGIALT conference, I will send them a free copy of the book as soon as I will get the green light from oReilly. I am planning to distribute other free copies at the next .NET community event and I will let you know when this will be. For now, buy a copy and make me happy! Open-mouthed smile


First of all I want to thanks oReilly and Microsoft to let me write this book. I also want to thank Russell Jones, my editor and primary publishing contact for this book. He is the only person who believed in me from the beginning, and he put himself on the line to get this project approved by Microsoft Press. I will be always thankful to him for that. He did also a wonderful job in helping me to complete the job on time, and to organize the whole project. Thanks to Kristen Borg, my production editor that spent a lot of time and effort to finalize and get this project done.

Of course I have to thank my wonderful wife that for the last 6 months she helped me in getting this job done by pushing me every time I was letting go the project for one or another one reason.


Please, please, please. If you plan to buy a copy of the book ( I hope so) I would really appreciate if you can leave a comment, on Amazon or on OReilly or on both of them and let me know what you really think about it. I love to get constructive criticisms on my work as they always help me to do the next thing better. Please, don’t be shy and don’t be nice if I do not deserve it!


I have received already a lot of requests about the book and the distribution. So, if you want an hard copy or an ebook you can get one, right away on the OReilly web site. If you go on, the book, the Kindle version or the EBook will not be distributed until the end of March 2011. If you need details about the book content, I am planning to write a new blog post ASAP.

State pattern using C#. Part 02.

In the previous post we saw how we can implement the state pattern (I know, I didn’t show you the purist way of using the State pattern …) and include in the state execution the flow logic.

This technique is fine but … it requires a lot of effort in the implementation and requires a lot of maintenance, plus it has the GAP of forcing us to re-run the CanExecute delegate every time we want to execute a specific action.

On the web I have found some solutions that personally didn’t satisfy me at all. I personally believe that the best way of designing a state machine workflow is to use a workflow engine and NET Framework provides with NET 4 an amazing state engine. Anyway let’s see what the web offers instead of using WF 4.

Stateless Open source project

Stateless is an Open source project hosted on Google code and available here:; it is a C# implementation of a stateless workflow using the BOO language.

We have the same domain exposed in the previous post but in this case we modified a little bit the Order object and we do not use anymore the Command pattern.


The class Order has 5 different methods that can modify its state in the following way:

Create an Order
  1. public void Create()
  2. {
  3.     this.State = OrderState.Created;
  4. }

They do not verify anymore if the action can or cannot be execute, we just know that the Create method, for example, modifies the state of the Order to “Created”.

Now it is time to wrap this code in a separated class that we will call OrderService and that is identified in DDD as a Domain Service object, a service used in the domain space to wrap business logic and keep it outside the Entity object. The final result should like this one:


The trick with is to split the service in two parts, the first one is used to Bootstrap the stateless framework in the following way:

Stateless boostrapping
  1. public sealed class OrderService
  2. {
  3.     private StateMachine<OrderState, OrderActions> workflow;
  5.     public OrderService()
  6.     {
  7.         workflow = new StateMachine<OrderState, OrderActions>(OrderState.Undefined);
  8.         workflow
  9.             .Configure(OrderState.Undefined)
  10.             .Permit(OrderActions.Create, OrderState.Created);
  11.         workflow
  12.             .Configure(OrderState.Created)
  13.             .Permit(OrderActions.Cancel, OrderState.Cancelled)
  14.             .Permit(OrderActions.Modify, OrderState.Modified)
  15.             .Permit(OrderActions.Approve, OrderState.Approved);
  17.     }

And then we add a method in the service that will be used to Fire a specific state change, like this one:

Command pattern
  1. public void Fire(Order order, OrderActions action)
  2. {
  3.     workflow.Fire(action);
  4.     order.State = workflow.State;
  5. }
  7. public bool CanFire(Order order, OrderActions action)
  8. {
  9.     return workflow.CanFire(action);
  10. }

Now, by default, Stateless raises an error (Exception) if the operation can’t be executed. The following test demonstrates the exception raised by stateless:

  1. [Test]
  2. public void CannotApproveAnOrderBeforeCreatingIt()
  3. {
  4.     var order = new Order();
  5.     var service = new OrderService();
  6.     Assert.Throws<InvalidOperationException>(() =>
  7.         service.Fire(order, OrderActions.Approve));
  8.     Assert.That(order.State, Is.EqualTo(OrderState.Undefined));
  9. }

The exception is of type InvalidOperationException.


Stateless is a good state machine framework, open source, easy to learn and it has a good and clear DSL language. Unfortunately the project is very young, the active developer is only one and it still has a huge list of ToDo and Bugs to be fixed.

It can be used to replace custom If and Switch in the Domain language but if you need to do some custom and more complicated evaluations, Stateless can result very verbose because it doesn’t have a UI so you have to prepare all the If and Switch using the Configure syntax.

State pattern using C#. Part 01

I have been busy for a while writing my two books about MVVM and WPF but I am almost done so be ready to get more posts in the next months. This one is the first of a series that I will write to solve the state pattern issue.

Today I want to start to talk about the state pattern, a design pattern used to represent the state of an object and how we can apply this pattern in a normal WPF application.

The problem we have is that based on the state of an Order we can or we can’t execute a specific action.

Before starting to talk about the pattern we need a sample application, right? So, what is better than having a nice WPF application that we will use to represents the problem? Smile

Process an order using States

The example I want to use is the classic Order entity that during the order process can be moved to different states. The following diagram create with Visual Studio shows you what I am talking about:


The previous image shows the state diagram applied to an Order:

  • You can create  an order and after it is created the state is of type Created
  • Then you can modified the order or you can cancel the order; if you cancel the order, its state is cancelled and you can’t do anything anymore
  • An order that has been created can be approved and its state will change to approved

So in the previous Use Case we have identified 4 actions (blue) and 3 states (white), for each action there is a specific state, in a specific state you can execute only a specific or a set of specific actions and from one state you can move only to one or more specific states. For instance, from the Approved state we can’t rollback to the Created state and so on …

Now, how would you express the previous diagram using a Domain Model composed by an Order entity and a State property? First thing first is to implement a Domain Entity.


Now we can implement the classic state pattern described by Martin Fowler.

Classic implementation of the State Pattern

If you are an MVVM developer you may believe that the first and almost the easiest way of implementing this pattern is to use the Command pattern, right? So, if we plan to extend that in the Order entity we should have a model like the following one:


Where the command implementation may be something like this:

Command Pattern
  1. CreateOrder = new Command(
  2.     () => true,
  3.     () =>
  4.         {
  5.             Code = “ABC123”;
  6.             State = OrderState.Created;
  7.         }
  8.     );
  9. ModifyOrder = new Command(
  10.     () => State == OrderState.Created,
  11.     () =>
  12.         {
  13.             State = OrderState.Modified;
  14.         });
  15. CancelOrder = new Command(
  16.     () => this.State == OrderState.Created || this.State == OrderState.Modified,
  17.     () => { this.State = OrderState.Cancelled; });


When an order is created, by default, we do not allow any state so the default state is undefined and these are the tests:

TDD – change state
  1. [Test]
  2. public void AssertThatANewOrderIsUndefined()
  3. {
  4.     var order = new Order();
  5.     Assert.That(order.State, Is.EqualTo(OrderState.Undefined));
  6. }
  8. [Test]
  9. public void AssertThatANewOrderCanBeCreated()
  10. {
  11.     var order = new Order();
  12.     order.CreateOrder.Run();
  13.     Assert.That(order.State, Is.EqualTo(OrderState.Created));
  14. }


The last step needs to verify that if we try to move from one state to a not allowed state, the action that we try to execute will throw an exception:

TDD – Not allowed method
  1. [Test]
  2. public void AssertThatCannotApproveANewOrderNotCreated()
  3. {
  4.     var order = new Order();
  5.     Assert.That(order.State, Is.EqualTo(OrderState.Undefined));
  6.     Assert.Throws<NotSupportedException>(() => order.ApproveOrder.Run());
  7. }


Conclusion using the Classic method

This technique is not clean and it is very verbose but it is absolutely testable, but not maintainable. For instance, if the number of state enum will increase we have to touch all over the code that execute the flow logic and probably we need also to refactor all the commands so we can say that this solution is fine but not maintainable at all.

Another gap of this solution is that every time we want to execute a command we need to fire the CanExecute method that may process some “long running” business logic behind:

Command Run() method
  1. public void Run()
  2. {
  3.     if (canExecute == null || canExecute.Invoke())
  4.     {
  5.         this.execute.Invoke();
  6.     }
  7.     else
  8.     {
  9.         throw new NotSupportedException(“The action can’t be executed.”);
  10.     }
  11. }

In the next blog post we will see a different approach. At the end of the series (04 posts) I will provide a WPF application on with the source code posted in this series; please be patient for now as I can’t upload the final source code yet.

We are hiring in Bermuda, two senior Devs.

Hi guys, I am glad to let you know that my Company is hiring two devs. The Company is growing and the IT team has to grow too!

Actually I am looking for two Senior Developers that are interested in join our cool team here in Bermuda and may also consider to relocate in the future to the awesome San Francisco, in US. The first period will be in Bermuda than it will be up to you to decide to stay in Bermuda or move to SF.

If you believe you are one of them that can make the difference, just contact me through my blog and I will let you know what to do and where to send the application.

Right now we are looking for two senior developers with the following characteristics:

  • At least 5 years of experience with Windows Form and possibly good knowledge of WPF too. I am not interested in Silverlight and we don’t use it here.
  • Good knowledge of WCF, how it works and how it can be used in a SOA environment.
  • Preferred, good knowledge of WF (Windows Workflow). The version 4, not the old version 3.5. Winking smile
  • Absolutely, at least basic knowledge of what an O/RM is, NHIbernate and Entity Framework. Related topics like UnitOfWork, Repository, Transactions and so on …
  • Preferred knowledge of a Client UI composition Framework like CAB, SCSF or Prism. This is what we use here! Winking smile
  • Good knowledge of agile techniques in general, DDD, TDD and all the other fancy words we use in our job … Smile

If you believe you are one of those guys out there that can make the difference, that you are eager for knowledge and you want to work in a nice place like Bermuda is, give me a shot! Of course you will work with me … Surprised smile

PS: Just to make it clear, I am not interested in: remote consulting, remote whatever, consulting, job agencies and so on. Winking smile