Category: MVVM

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: http://www.dnug.bm/.

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: http://www.dnug.bm/index.php?option=com_jevents&task=icalrepeat.detail&evid=5&Itemid=58&year=2011&month=07&day=21&title=july-2011-dot-net-user-group-event&uid=a620ff387a7449aaad68443c9780f7c2

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.

New content for my Microsoft book

The Microsoft’s book I have published few months ago: “Building Enterprise Applications with WPF and MVVM” has been a success but I still got some negative feedbacks that me and my editor we want to get rid off.

This book is my first real book and of course it was my first experience on writing a book (for this reason we kept the price of the book very low). Anyway .. I got some bad feedbacks about missing parts, parts not explained as expected and a misunderstanding of the book audience and target.

I personally believe that the biggest problem is in the title of book, it drives you a little bit out of the topic of the book, if you buy this book you will believe to get the “bible to LOB applications with MVVM”, which is not.

For this reasons and also to keep high the audience of the book, we have decided to deliver for free new additional content for the book! Open-mouthed smile

The list of the new content is still under discussion with my editor but this is a rough list of the topics I will touch or expand in this new context that should be composed by 3 additional chapters!

  • Design patterns
    I am planning to add 20/30 additional pages on the second chapter in order to exhaustively cover everything related to the most known design patterns
  • Advanced MVVM
    I will add a new chapter where I will explain some “well known” problems you may find when the adoption of the MVVM pattern starts to get tricky!
  • Composite Frameworks for MVVM in practice
    In this chapter we will build the same Master-Detail UI logic using the three most famous frameworks for WPF/Silverlight: PRISM, Caliburn and Light Toolkit

If you believe that the book is still missing other information, feel free to send me an e-mail and I will be glad to discuss this with my editor.

Note: Remember also that we are planning to distribute the source code of the book as an Open Source project on Codeplex.com before the end of the year.

Hope this will help!

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:

APRESS_cover

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

Distributors.

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 Amazon.com 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.

Prices.

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 Amazon.com and OReilly.com
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
OReilly.com, 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 Amazon.com has notified me about the error done by OReilly in the publishing address. The source code is available here:

http://examples.oreilly.com/9780735650923-files/­9780735650923_files.zip. 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. }
  16.  
  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. }

 

REMEMBER THAT BEFORE RUNNING THE TEST YOU SHOULD CREATE THE DATABASE IN SQL SERVER AND CHANGE THE CONNECTION STRING IN THE APP.CONFIG.

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: http://oreilly.com/catalog/0790145309686/

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: www.amazon.com, kindle, www.oReilly.com and more.

cat

Distributors

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: http://oreilly.com/catalog/0790145309686/ 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 www.amazon.com 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.

Prices

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

Thanks

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.

Comments

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!

Updates

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 Amazon.com, 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 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:

StateDiagram

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.

OrderObject

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:

CommandPattern

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. }
  7.  
  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 www.codeplex.com with the source code posted in this series; please be patient for now as I can’t upload the final source code yet.

News for MVVM lovers and MVVM Books.

A lot of things are going here, on my side I am publishing two books about WPF and more in general about MVVM and Prism; on another side someone pretty cool (Karl Shifflett) has just released a new way of delivering tutorials and lessons within the Visual Studio 2010 UI, the tool is called InTheBox. Finally, the team of Patterns and Practices is making progress on Prism and a new drop has been released on Codeplex.

In The Box and Feature Builder

InTheBox is a new way of delivering mentoring through the UI of Visual Studio 2010. The idea has been introduced a while ago whit the plug-in for Visual Studio called Feature Builder; with this plug-in, you can create  dynamic guidance for your applications and deliver them with your Visual Studio solution templates or item templates.

The following picture is a screen shot of how it appears in Visual Studio 2010; if you are interested in learning MVVM this is a good starting point but I believe Karl and his great Team should put more information in the documentation attached with the MVVM Training kit.

InTheBox

I am personally using the core engine of Feature Builder power tools to deliver some guidance also in my office and I found the tool and the automation API delivered with Visual Studio 2010 pretty interesting; unfortunately, as usual, the documentation is pretty poor and it still lacks of features right now.

Screen shot 2010-12-07 at 11.02.52 PM

Prism V 4.0

Last month, more precisely, in the middle of November 2010, the team of Patterns and Practices has released a new version of Prism, the V 4.0 which is probably (in my opinion) the more stable and complete version of this amazing framework that allows you to build composite UI application with WPF or Silverlight.

The release has been announced here: http://compositewpf.codeplex.com/ and it’s available for download at the same address.

I am posting here some of the cool features that has been introduce with this release of Prism; feel free to go to the Codeplex community web site and have a look at Prism, trust me, Microsoft did a great job!

  • Prism is now available for WPF 4, Silverlight 4 and Windows Phone 7
  • It is delivered with the full source code and with all the assemblies already full signed
  • Well done and complete documentation plus sample applications for WPF and Silverlight

A big thanks has to go to Karl Shifflett which is making a great job on the PRISM community and also to me technical review David Hill which is another big of the team of Patterns and Practices.

My Books

Two or three weeks ago a friend of mine discovered that my books were available on Amazon for pre-order; I was personally shocked about that as no one of my editors told me anything about that.

By the way, now that the news is almost semi official I am going to publish in this post the two covers of my books. They are two total different books as they focus on two different things. The first one is released by MICROSOFT PRESS and it has been done in collaboration with David Hill (David is a Microsoft Solution Architect and right now is working at the team of PRISM in the Microsoft division of Patterns and Practices), which is my technical reviewer; it talks about architectural and UI patterns you should adopt when you build a LOB application with WPF or Silverlight. The second one is part of a new series of books APRESS is writing which is entitled “In context”; in this series and more precisely in my book, I talk about how to write, from scratch a dynamic and well done WPF application by using all the features available in Expression Blend and Visual Studio 2010.

As  you can understand, the first book is more an architectural book for an audience that has already worked with WPF or Silverlight while the second one is a for dummy book on WPF.

Said that, you should buy both and read them as soon as they will be available on Amazon the next Spring!! Smile

Building Enterprise Applications with Windows Presentation Foundation and the Model View ViewModel Pattern

Applied WPF 4 in Context

WPF and MVVM tutorial 01, Introduction.

With Microsoft WPF technology, a new pattern is born and is going to be called MVVM (Model View ViewModel). This pattern is an hybrid from the old MVC and the old MVP patterns. Why a new pattern for the presentation?

  1. First of all WPF technology is giving us a kind of technology that can completely change the approach to design and code the UI. With the VMMV we can completely design an agnostic UI that doesn’t know the Model we are going to pass to it.
  2. Recycle, I will show you in this tutorial how to simply convert a WPF application into a Silverlight app.
  3. Better delegation and better design for a real n-tier application. In this example we will use LinqToSQL and WPF to build a complete n-tier application with the VMMV.
  4. Something that I do not like, TESTING THE UI!! Yes we can test the UI with WPF and VMMV combination.
  5. Abstractation. Now the view can be really abstract and you can use just a generic.xaml file and then give a style template to your model.

This is the schema (in my opinion) on how it should work an application with WPF and the VMMV implementation:

image

  • MODEL: anyone that has already worked on an n-tier application knows what it is. The model is the group of entities that will be exposed.
  • VIEW: the view is the graphical code XAML used to generate the UI, nothing more then that.
  • VIEWMODEL: the model for the view … or … the view for the model?! Anyway is a model of the view.

Before starting this tutorial I suggest you to download:

The next tutorial will show you how to create a simple DAL layer with LinQtoSQL and how we can implement the Unit of Work pattern to build a simple but solid data layer for our application.

You can also download the Visual Studio template here.

Tags: