Rules of MVVM??

UPDATE #2:

There is very active discussion about MVVM in WPF Disciples User Group. If you are interested then please read all posts and get invoked in discussion. Here is the link. Update: Here is another one.

As I had a MVVM session at my office, I was re-reading a few articles about MVVM. We have very interesting discussion about MVVM in WPF Disciples User Group as well. You can read that post from here.

Someone in Silverlight Forum (link) posted that ~

“There are currently three main areas of criticism regarding the MVVM pattern. The first is that MVVM currently lacks standardization from Microsoft both in implementation and in toolsets. For example, the community has some lack of clarity about where and whether to implement View logic in the View layer or the ViewModel. Given that the MVVM pattern is still relatively new, and that new tool-sets, walkthroughs, or patterns, such as Onyx, Prism, the Microsoft WPF Toolkit, Crack.net, Caliburn and MVVM Light Toolkit are being released, this problem may be solved over time. Microsoft has announced in discussion boards that the MVVM template pattern will be released in Visual Studio 2010.

The second comes from MVVM creator John Gossman himself, who points out that the overhead in implementing MVVM is “overkill” for simple UI operations. He also states that for larger applications, generalizing the View layer becomes more difficult. Moreover, he illustrates that data binding, if not managed well, can result in a considerable excess of metadata in an application. Given these limitations, MVVM may have a practical minimum and maximum size for the type of application it can support, suggesting it may not perform well with large enterprise applications.

The third is that the exercise in creating large numbers of data bindings to the ViewModel results in duplicate code and maintenance problems. Additionally, because of the nature of the semantics of data bindings, critics suggest that the ViewModel does not directly describe the View.”

So, I was thinking it would be great if John and our WPF/Silverlight community can define some simple and obvious rules for MVVM pattern.I understand that there are a lot of way to implement MVVM but at least, there are some obvious rules that everyone can follow so everyone has same understanding about that pattern.

Here are some of my thoughts about MVVM.

Why MVVM?

  • Testabiltiy ( ViewModel is easier to unit test than code-behind or event driven code)
  • Clear seperation between UX designer and developer
  • Increases the “Blendability” of your view
  • Model never needs to be changed to support changes to the view
  • ViewModel rarely needs to be changed to support changes to the view
  • No duplicated code to update views

Do and Don’t in View

  • shouldn’t contain any logic that you want to test : As Glenn said that MVVM is not code counting exercise, we can write code in code-behind. But you should never write any logic that you want to test. For example: If user select a country then you want to display the list of states or city in your view. This is the business requirement so you should have unit test to test this logic. So, you shouldn’t write it in code-behind.
  • can be a control or Data Template
  • Keep the view as simple as possible. : We can still use Data Trigger or Value Converter or Visual State or Blend Behivor in XAML with care.
  • use attached property if something is not bindable :

Do and Don’t in ViewModel

  • Connector between View and Model
  • Keep View State, Value Conversion : (You can create the data structure that you want to display in ViewModel instead of using ValueConverter. For example: You need to show the Name instead of First Name and Last name. Your Model can have First Name and Last Name but You can create Name property in ViewModel. )
  • No strong or weak (via Interface) reference of View
  • Make VM as testable as possible (e.g. no call to Singleton class)
  • No Control related Stuff in VM ( Because if you are changing the view then you will have to change VM as well. )

Model

  • can be Data Model, DTO, POCO, auto-generated proxy of domain class and UI Model based on how you want to have the separation between Domain Service and Presentation Layer
  • No reference to ViewModel

What do you think about that? Feel free to let me know if you have any comment or suggestion..  Thanks.

UPDATE:

This is the open post. I will keep on updating it based on the feedback. I’m trying to discuss about those rules in WPF Disciples User Group. I understand that there are a few people who don’t want to get invoked in this kinda discussion because it can turn into a fight. Anyway, I will try to discuss with people nicely. :) I will get the feedback several groups like SL MVP/Insiders group, Stackoverflow, Codeproject and etc.

My WPF MVVM Session at Consistel

I gave MVVM presentation at Consistel today. It was 2 hours long section and we had a lot of great discussions about MVVM pattern. Due to the problems with laptop and remote desktop, I was not able to show the demo in my session. I’m going to have another WPF session with my colleague “Nyi Nyi” tomorrow. Thanks to everyone who is attending my session. Especial special thank to Arpad and Maneesh for arranging this session and supporting me in my session.

Topics

  • Understanding MVVM
  • Motivation and benefits
  • Implementing the pattern
  • Dependency Injection and IoC
  • Unit Test and Mock
  • Supporting libraries and frameworks

Download ~

Demo ~

Here is the list of things that I used in

You can also download the demo here.  MVVMDemo.zip (5.34 MB) Note: Unity and Moq dlls are already included in zip file.

As I want to show the team how to write the test, I didn’t write anything for both View and Service. The test below are the BDD (Behavior-Driven Development) Style.  There are a few BDD framework for .NET but I don’t really like any of them so I’m using VS Unit Test with BDD-style test. The main difference between TDD and BDD is that BDD focus more on specification and readability. (Some people said that BDD is TDD done right. :) )

Feel free to let me know if you have any question or comment. Thanks.

3 new websites for Silverlight Community

Hi Everyone, I hope you all are doing well. I like to introduce new 3 websites that I created since last year.

Silverlight MVP and Insiders Web Site

URL: http://wsinsiders.com

This site is where you can find all informations about Silverlight MVP and WPF/Silverlight Insiders. You can also read the series of Silverlight tutorials written by Silverlight MVPs and Insiders. Silverlight Tutorials for Beginners is the first series of our tutorials. We are planning to write more tutorials in that site as well. Here is the list of tutorials that we have in our mind.

  • Tutorials for Silverlight Business Application Development – we can write about localization, validation, services. performances, pattern, Prism or MEF
  • Tutorials for Silverlight Control Development : We can read the source code of Silverlight Toolkit and can write something like Silverlight Toolkit Internal.
  • Tutorials for Silverlight Media or Game Development : Pixel Shader or 3D or Effect or Best Practice for video/audio streaming
  • Tutorials for “Designers/Expression Blend : I think it’s very important. the resources for designer are very less. there are a lot of designers who like to learn about how to design theme or XAML or Blend.. We can write about how to create a Mac-style theme, Glassy theme or etc.  Note: The old name of that tutorial was “Tutorials for Silverlight/XAML Designer”. Based on Michael Washington’s suggested, it has been re-named.
  • Tips/Trick : How to add the grouping feature, Excel-like Filering in Datagrid,
  • Tutorials for Silverlight Integration : This is where we can put ‘Silverlight and SharePoint” and “Silverlight and DotNetNuke” Note: Suggested by Michael Washington.

Please let me know if you like to contribute your tutorials as well.

Let me tell you a bit about the history of this site. I created this website on Jan 20, 2009. By the time when I was creating this site, there was no MVP award for Silverlight so there was no Silverlight MVP at that time. We had only WPF/Silverlight Insiders and we wanted to create one website similiar to ASPInsiders website. I wanted to register SilverlightInsiders.com but that domain was not available so I registered http://wsinsiders.com/ and started collecting all information about Insiders. Collecting Insiders information is kinda hard because I don’t have any latest list of Insiders and there are a few people who don’t have any blog or BIO or photo. Once I have collected the information about MVP and Insiders, I started planning to write the tutorials with a few MVPs. Braulio Diez Botella,  Alexander Golesh,  Emil Stoychev,  Einar Ingebrigtsen and Daron Yöndem.  Jocelyn Mae Villaraza from Microsoft Singapore helped us to review those tutorials. It took me around 1 years to finish this website. Anyway, I’m glad that we finally make it.

Please feel free to let me know if you have any suggestion or comment for that site.

Open Source Software in Silverlight

URL : http://silverlight-source.net/

I created this website based on one of my favorite website called Open Source Software in C#. There are a lot of open source Silverlight projects. The problem is that all projects are not mature or active so I was thinking it would be so useful for Silverlight community if we have a website that have all information about  active, mature and awesome Silverlight open source projects. You can also suggest me the project via this page http://silverlight-source.net/suggest-project/.

Singapore Silverlight User Group

URL : http://sgsug.org/

This is the official website for Singapore Silverlight User Group. Me and Justin Lee are running this group. We are gonna have our first meetup on 10th Feb, 2010. If you are in Singapore, please come to our meetup. It’s just a casual gathering. You can wear anything you want to come there.

Dell Workstation

I bought this work station from Dell a few month back. The idea is that I want to install Hyper-V and run several different operation systems in same machine. I’m playing with very new stuffs like VS 2010, early builds of Silverlight  and etc so it’s time-consuming process for me to uninstall/reinstall manually. I’m very happy to use that powerful machine. The only thing that I want to add to that machine is “Solid state hard disk” but I can’t afford to buy it for now. Hope the price of solid state will be deceased very soon.
Specification
  • Processor : Intel® Xeon® Processor W3503 2.66GHz/8MB L3 Cache/4.8GT/s
  • Memory : 12 GB DDR3 SDRAM Memory, 1066MHz,ECC
  • Monitor : Dell 22 E2209W Wide Screen Flat Panel LCD
  • Hard Drive : 1TB (7200 RPM)
  • Video Card : 512 MB PCIe x16 nVidia Quadro FX 580
  • Sound Card : Sound Blaster X-Fi XtremeMusic

dell-precision-t3500.3355780

CPU

Last Day in Xuenn

Note: This post is written on Oct 16, 2009.

This is my last day in Xuenn. I remember the day that I decided to accept their offer even I had a few offers from serveral companies (including NCS, UTAC, AsiaSoft and etc). I was so excited at that time because I was told that I’m going to work with latest technoliges (e.g. WPF, Silverlight), TDD and Agile (Scrum).  (There are only  a few companies that are actually using latest technologies for their projects. The most of companies are still using ASP.NET  1.1 or 2.0, winform, CRM, Sharepoint and etc so it’s really hard to find the companies that use the latest technolgies and pay well.) And also, Online gambling and sport betting domain was very new to me and I don’t mind learning new domain so I accepted the offer right away.

Here are the things that I’ve done in that company.

  • Design Pattern in details : I’ve been working with design pattern since 2005 when I was working in India. I was using very simple design pattern like Singleton, Observer and etc at that time. After that, I joined to UTAC company around 2007 and I got more chance to use the design pattern (especially MVP (Model-View-Presenter) pattern, Factory pattern, Building pattern, Event Aggregator pattern and etc ) since that company is using CAB (Composite Application Block)SCSF (Smart Client Software Factory) for their semi-conduction manufacturing related project. I joined to Xuenn last year. As I need to design the base architecture of WPF application and need to conduct a lot of technical interview, I have learnt all of GoF (Gang of Four) pattern, MVVM (Presentation Model) patterns in details. ( Yes, when you are either designing the software or conducting the interview for those who are familiar with design pattern, you must have good understanding about those patterns. Otherwise, you might have hired wrong person for projects and your project will screw up at the end.) A few good websites (like dofactory.com or c2.com) and good books (e.g. Head First Design Pattern and Gof: Design Patterns: Elements of Reusable Object-Oriented Software) are extremely useful if you want to learn about patterns. But reading books and knowing the theory are not good enough. You actually need to use them in your real project regularly.
  • Designed the architecture of WPF/Silverlight projects : I participated in designing the whole architecture of WPF/Silverlight projects. I did a lot of researches for implementing security and authentication, localizaition, template themeing, WPF and Silverlight compatibility, Prism framework, patterns (MVVM, DI) and etc. I ‘m glad that we successfully implemented all of them for our projects.
  • Composite Application Library (a.k.a Prism) and Unity : I’ve been using Prism version 2 since P&P team distributes early drop before they release. So, I got a chance to get very familiar with those frameworks in that company. I even manged to contribute some of my source code to Prism ContribUnity Contrib projects. We implemented two styles of MVVM pattern in our project. One of them was Julian’s approach (PresentationModel with DataTemplate).
  • TDD, BDD, DDD : I noticed that people like DD these days. With the help of my good friends, I was getting more into those DD terms – TDD (Test-Driven Development), BDD (Behivor-Driven Development) and DDD (Domain-Driven Development). I didn’t practice much in that company but those DD become something that I want to explore more in my current company.
  • Lead a small application team : There was two major WPF teams in our company. I was taking care of one team. As the most of people in my team are very new to WPF, MVVM pattern and Asynchronous services, I had to support them until they got familiar with our achitecture. As you all know, I’m a community guy and I do enjoy helping other people so it was fun to guide them how to do things in WPF or Silverlight.
  • Participated in interviewing and recruting people : I contacted a lot of face-to-face interview or phone interview when we were actively recuriting people for our project. It consumed a lot of my time but I know that it’s good experience to have in my career.
  • Recreation Club : I was a part of the recreation club and we used to organize a lot of events (outing, sport activities and etc) for our stuffs. It was fun.

Needless to say, I had great time working with them. I wish them to have project success in future…