jueves, 30 de abril de 2015

Labeled as:

Model View View Model, a simple explanation Part 1

C# and Developing
Intended for: C# developers of all levels

I have been working about four years in WPF and every day I can see more bad examples on how people develop in this marvellous Microsoft's technology, that brings us to an upper level in graphical user interfaces.

In this way Microsoft have a CLEAR WAY OF WORK on how to operate and how to make developments for this technology, but people that comes from Windows Forms don't want to be adapted to it. Due to this I write this article, with the idea to help people that wants to adapt and trying to reach middle points in the desktop developments of applications. Besides the Silverlight future is dark, I hope it helps too to people that stills develop in this platform.

This article will be divided in five parts, because of the MVVM complexity and requieres a short time to adapt to it correctly. If you come from Windows Forms and you think that you can do same than always (work using events) you're correct, but you're making a big mistake. If you made a WPF application using events with more than two views, you are showing your ignorance about this: please, take your time to understand and update your mind.

The principle for all of this is: EVENTS ARE THE ENEMY, don't use them as you did all the time. This technology is not made to the classic click over a button or the Text_Changed in a Textbox. Change your mind and think further.

And now the answer to the question: How this work? Windows Presentation Foundation technology have a fanstatic way to operate, that allows us to use to the maximum our modern graphical cards and PCs. Not beign videogames applications (you can do it, but is not the final intentions of WPF), WPF applications could be beautiful and well designed that people fells using them as childs playing with them; as developers that must be our final goal, a user feeling like home VERY confortable using our developing tools.

The pattern MVVM consists in a serie of good practices that me as an experienced developer of it define like this (this could be of course discussed):
  • Sepparate the view from the functionallity that supports itself (this is that we call View)
  • Create a layer that connects business layer and view (which will be called View Model)
  • Finally, create a business layer that HAS NOTHING TO DEAL with the View (as you can think, this is the Model).

MVVM conceptualmente
Forbidden links: from Model is TOTALLY FORBIDDEN connect with the View.

Then, how we can communicate entities from this model? It's very simple:
  • User ONLY communicates with the View.
  • The View communicates ONLY with View Model.
  • The View Model interconnects the View and the Model.
For that, from Visual Studio we must create a Blank Solution and make the following:

We will create a Blank Solution in Visual Studio:

  • Then we must create a new WPF project, the one that will be the View.



  • At this point, we must create a Class Library projec the one that will be the new View Model.



  • Now we must create one or more projects (commonly several), of Class Library type, which will create the Model.



After all, we must have the following project tree:


How to relate them?

  • The View project must reference to the View Model project. Never will have references to the Model.
  • The View Model project will reference to one or several Model projects. It won't have references to View project (or we will have circular references).
  • The Model projects NEVER will reference to the View, and never will reference to the View Model.
With the defined relationships, the communication is only in one direction:
Communication direction due to project references
This communication is good but is not enought because we need mechanisms to inform to the rest of components in the other way. In a newer article I will explain to you a way to personalize events and a simple event manager for Weak Events.

But before doing complex things, in the next article I will explain XAML files and Bindings mechanism, vital for our goals.

I hope you enjoy this small intro and I sincerely want your comments, that will be welcome and from which I will apply as much corrections as I consider.

Unknown

Author y editor

0 comentarios:

Publicar un comentario

 

Friendly Websites:

  • Manuel Enrique Díaz Rodríguez
  • CuRadio
  • Copyright © DotNet Pathways 2015
    Distributed By My Blogger Themes | Designed By Templateism