jueves, 30 de abril de 2015

Model View View Model, a simple explanation Part 1

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.
C# and Developing

domingo, 19 de abril de 2015

How important is not invent the wheel again... Programming Challenge #2

Intended for: C# programmers of all levels


A few days ago I've found a similar code than the above one... I guess that was due to I was very very tired, o because I wasn't expecting a creation like this but, is a fact that I need some seconds (lots of them) in undertand what the hell this code does.

After a conscious analisys finally I understand that it's creator only wants one thing: make a DateTime object that retrieves the current date using the useful DateTime.Now be converted in a string object, to be painted in the screen.

I don't want to blame this guy or it's proffessional capabilities, I don't want to freely complain because I know that EVERYBODY (I'm the first one) make mistakes sometimes at this job. What I want to say is that if we take 3 seconds to surf in Internet, we realize that this is not so far the best way to do this simple thing.

My reasons for this, but there will be million more, are the following:
  1. Each call to DateTime.Now have a useless and stupid additional cost, from the first call to the last one, besides beign small, there will be milliseconds difference between them (in the image you can realize that there are several calls to the functions, you only need to know the Now property).
  2. Concat operator ("+") is the slowest one for strings and is not good to use it for so extense concatenation. For it there is StringBuilder class (one of the tricks that I've learned in the official Microsoft course).
  3. This code is so far away of beign clear. I insist, my more that nine year's experience not allow me to understand it untial a difficult analisys so, a rookie trying to understand it fighting a dragon like this... Our code must be as clear as possible (there's sometimes where the code couldn't be more clear for several reasons.
No doubts about the best way to do this is the following one (people that knows about this realize at first for sure):


Both functions make EXACTLY THE SAME, but the second one is infinitely faster and clear than the first one. This means that in an application than needs a good performance, if we don't care about simple things like this it's possible that the response time will be penalized.

Because of all explained in this article I insist: don't invent the wheel again. The rule of lazy developer that will be explained in a future article must be the one that is with us every day. If we make "science" or "sorcery" like this, is normal that people cames with a crane and deletes our code.

I want feedback from you with similar experiences...

C# and Developing

Support to our dead Windows XP

Intended for: all kind of users


Windows XP maybe will be the best accepted and extended Microsoft's operating system. It's simplicity, easyness and good work if you look to its parents (we all remember the disgusting Millenium Edition...) make XP OS for several guvernamental institutions and for home users a reference for others.




A recent issue that made bleed its home users is that Microsoft announce officially the past 8 if April of 2014 that they won't bring no longer support for it, that means  that from that moment, all XP users are "totally abandoned". The decision is understandable: it's first release was in 25 October of 2001, 13 years ago... and Microsoft brings it's best to support this: it's time to update.

Companies still receiving support (of course, paid), and there's a version of Windows XP that stills having support. That updates are meant to be compatible with the rest of Windows XP versions so... There are people that discovered how to still having support. The source of this is a webpage that I'm a good fan of it NeoTeo (article for trick in Spanish).

Based on my knowledge of it, here you can download a .reg file that is intended to update this value of the Windows Registry and still receiving support for your old Windows XP without advanced knoledges: Windows XP support period extender.

As always, be aware of:
  • The fix is totally for free and you can't pay or be paid for it
  • You can put it on any web site always if you don't forget to put the source of the information (this blog and post) and you don't change the content inside the .reg file inside my web.
  • You're using this under YOUR OWN RESPONSIBILITY
  • I am free of all responsibility for any problem that you would have using it: no viruses, no errors of it's application, no human errors, NOTHING!
  • It's possible that Microsoft changes this in some way, or any of that updates hurts your PC: I'm not responsible of this too.
If you're not agree with the previous, please don't download it and don't use it. To be clear: is not my problem. I bring you this like a gift, my work and my effort and I bring it to you "AS IS". Of course, I will do my best to make my applications free of fails, but we all know that computing science have lots of historical failures that make loose millions of dollars to big companies: obviously a free app can have fails too. If you see error and want to report them, please comment right here.

To use it is enough that you download it to your PC and make double click over the file. It will ask you if you want to add to registry the new entries, click over Yes and restart your PC. With that must be enough to receive again automatic updates.

I hope it will be helpfull for you too!
Windows

lunes, 13 de abril de 2015

We are in Facebook


We create a facebook page in Spanish and English to keep you updated in real time...

You can access there following the next link:
https://www.facebook.com/pages/Los-vericuetos-NET/537146749667471

Info

How important are comments in source code

Intended for: C# Developers

Comments


Comments are vital in our daily job, because as far as we can see create code for yourself is easy; read code from other developer and understand it is often a hell. Moreover, the added value of the XML comments that Visual Studio provides us: are shown in IntelliSense and help other developers to understand what certain methods do, why you crate some properties, ...

Please, take comments in count before start coding. Often we make confusing things (because of a lack of analisys, or because it's the only way out) which is mandatory to comment it intended for other one, even yourself in a few time, will understand it.

For it we have one line comments...
public void CorkBreadMethod(bool entry)
{
     if (entry)
     {
          //Code if entry is true
     }
}

…useful to bring a fast description of what we want to do.

We can use multiple line comments…
public void CorkBreadMethod(bool entry)
{
     if (entry)
     {
     /* Code if entry is true and loren ipsum when the sun goes
     * down and the moon is dark */

     }
}
… they are usefull when the line is too large and is totally neccesary all the text that we introduced. It is very important take this in account, text must be really neccesary; if not we're making dirty code for the next code reader. Watch section about line lenght.

XML Comments

XML comments are very important because they'll be the best help for the user to undestand better our classes and methods. For use them, Visual Studio provides us an automation: before the class, property or method (over the previous line) declaration we will write three times the slash character /// and automatically will be created the neccesary headers for the XML comment:
/// <summary>
/// Class in charge of beign an example
/// </summary>

class CorkBreadClass
{

}


Previously is an example on how to identificate one class using an XML comment. After do it, outside that class you will see as follows:


/// <summary>
/// Method inside CorkBreadClass
/// </summary>
/// <param name="entry">A boolean that represents the entry</param>
/// <returns>5 if entry is true, 0 elsewhere...</returns>

public int CorkBreadMethod(bool entry)
{
     return entry ? 5 : 0;
}

With previous XML comments we have the following results:



As we can see in previous capture, we get back all the previous information inside XML comments, so we gain that people that don't know too much our code or not have access to the source can understand what happend inside.
C# and Developing

 

Friendly Websites:

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