Tuesday, March 18, 2014

INofityPropertyChanged

Publishing a classic email for public consumption...enjoy!

To All Software Developers and Enthusiasts,

Do you still set model data like this:  myModel.Description = textBox1.Text; ?
Do you still populate your UI controls like this:  textBox1.Text = myModel.Description; ?

Stop!  I’d like to propose a more dynamic way to connect your models with your views.  The above type code can make it very difficult to keep your UI and models in sync – especially in large, enterprise-style applications.

This more dynamic way involves DataBinding and INotifyPropertyChanged (INotifyPropertyChanged).

Figure 1:  Woohoo!!!  INotifyPropertyChanged!!! And DataBinding!!!

NOTE:  A best practice NOT-covered here is the use of a controller, presenter, or view model.  These “middlemen” generally accept a model via some interface and wire it to the UI.

Let’s look at a simple example.












Figure 2

My model is a list of SBUs with the currently selected SBU being determined by the selected index of the shown combo box:
sbuList[sbuUltraComboEditor.SelectedIndex]

This model has a Description property which is databound to the Description text box as follows:
descriptionTextBox.DataBindings.Add("Text", sbuList[sbuUltraComboEditor.SelectedIndex], "Description", false, DataSourceUpdateMode.OnPropertyChanged);

This provides for one-way data binding (view to model).

For demonstration purposes, we will use the Export All button to display the model data (which may or may not correspond to the view data) in a message box and to simulate back-end data changes to the model.

Here is what clicking the Export All button does:
        private void exportButton_Click(object sender, EventArgs e)
        {
            // Show model data.
            MessageBox.Show(sbuList[sbuUltraComboEditor.SelectedIndex].Description);
            // Simulate some backend process modifying the model.
            sbuList[sbuUltraComboEditor.SelectedIndex].Description = "Gig 'em";
        }

Change the view and watch the model change like magic!
In Figure 3, I changed the Description text by typing in the text box “Howdy!”.  Clicking the Export All button confirms that the model was updated.
















Figure 3

We also know that clicking the button updated the Description property to “Gig ‘em” after displaying the message box.  But the UI didn’t update to reflect this change.  Clicking the Export All button again confirms the change of the Description property to “Gig ‘em” as shown in Figure 4.


















Figure 4

Oh, no.
Figure 5:  I knew this code the Mad Hatter gave me was not going to work.

But let’s not forget about INotifyPropertyChanged which we can apply to our model to effectively enable two-way data binding.  That way when the model is updated, the view will be as well.  Here is what INotifyProperty looks like on the model:
public string Description
        {
            get { return _description; }
            set { if (value != _description) _description = value; NotifyPropertyChanged("Description"); }
        }

#region INotifyPropertyChanged Interface
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion

Figure 6 depicts the functionality after implementing INotifyPropertyChanged instead of the out-of-sync data which was shown in Figure 4.  The call to NotifyPropertyChanged notified binding clients that its bound value had been modified and, therefore, needed to update the value.


















Figure 6

In summary,  by using DataBinding and INotifyPropertyChanged, a two-way data binding solution can be implemented to ensure that views and models will remain in sync.

Wednesday, August 21, 2013

We're sorry! Can we play now?

Scott,

My friends and I woke up this morning saddened by the fact that out characters, DeathEssence, AggieCMD and Jangooni, have been banned from QONQR.  I admit that I am not surprised and believe that the course of action you took is justified.  The QONQR Terms of Service were violated by deploying bots outside of the application.   Though there is no valid excuse for our actions, I would like to take this opportunity to explain the unfortunate progression of our actions and humbly request our re-instatement.  

In short, we were addicted to QONQR.  I’ll explain the addiction by sharing the journey that we experienced while playing.  We spent days and nights battling the Swarm of Houston.  We hoped for a day when our fellow Legion comrades in Houston would vanquish the Swarm in their location and send reinforcements to help us.  When we saw that those reinforcements couldn't come in a timely fashion, we realized we had to do it alone.  So we became a collective Rambo of sorts (minus the amazing pecs and abs). We worked day and night racing to 100. QONQR was all we thought about.  When I bought groceries at the store I pitied the old lady buying milk oblivious to the fact that she was surrounded by Swarm.  DeathEssense was launching bots in church while praying that the Swarm heathens would fall to the righteous Legion.  Jangooni worried what faction his four and five year old children would choose when they grew up.  We fought, we shed invisible blood, and persevered- but the battle was long and brutal.  The longevity of it all led to the next stage of addiction: qubism.   DeathEssence was hit hard by the taste of what a qube could do and he couldn’t stop.  Overheating be damned!  DeathEssence had qubes!  But he had to stop qubing for financial reasons.  We then turned to people for support, and that began the next stage of the addiction: social media.  We joined Legion user groups and experienced the magic of companionship, comradery, and coordinated warfare.  We weren't just the lone Legion flame smothered by Swarm in Houston anymore.  We were part of a greater effort.   We were LEGION!  The path to victory seemed closer now.  And at that moment began the final and most detrimental phase of the addiction:  we became Faceless.  Not literally but in spirit.  In other words, we wanted to hack QONQR for the greater good.  We should have blushed at our purple moment, but we didn’t.  It became real and we developed an auto-bot launcher.  We didn’t mean any harm by it, we just did what we do best - code.  The true desire was more of understanding how things worked in .NET and what we could really do. 

Our QONQR access is gone, but our addiction is not.  I sincerely apologize for our actions and  I humbly request that our access be re-instated with our assurances that we will only use the official user interface from now on.  If I were you and worked so hard on a game and achieved success, I would be upset too if people took an unfair advantage like we did.  I understand.  Our passion for the programming aspect of it all led us astray.  

Regards,
AggieCMD
DeathEssence
Jangooni


Friday, November 30, 2012

Windows 8 is not a Usability Disaster

Jakob Nielsen is one of the most respected usability experts in the field of computer-human interaction so his opinions on Windows 8 are a must read.  While many of his points are well-taken, I don't think Windows 8 is dissappointing.  If anything, it is misunderstood.

The Double Desktop Problem

I understand the confusion surronding the two interfaces, but I also understand the utility.  The desktop is there because, well, it's the desktop - safe, familiar, and comfortable.  Microsoft's intent is to do away with the desktop because they believe they have a better, more sophisticated and modern interface solution.  But the desktop is so iconic that its replacement has to be gradual.  With that said, the Start Screen (Modern UI) is not a second desktop in Windows 8.  It is a glorified Start Button that serves as a single entry point to all of your applications.

Lack of Multiple Windows

As a software developer, I have more windows open any just about anyone.  I must have a multiple monitor set-up.  Windows 8 supports my need to view and see multiple windows on a multi-monitor desktop environment, but also scales very well to the laptop and tablet form factor which is a must for a modern operating system.  Regardless, Alt-Shift is a staple of Windows for quickly shifting between windows as are tabs in the browser.  Both functions remain in Windows 8.

Flat UI

Microsoft has addressed the usability decisions behind the flat (Modern) UI.  In the early days of the graphical user interface, designers made clickable objects look like buttons to let users know that the object could be interacted with.  The button analogy is no longer needed today because people know how to use graphical interfaces.  With this in mind, the decision was to remove "chrome" from the UI to focus on content.

Low Information Density

Nielsen points out some examples of Modern UI applications that offer low information density and his points are clear.  However, there are apps that do not suffer from this problem.  USA Today, ESPN, and Xbox Smartglass offer a generous amount of on-screen information is a beautiful interface.

 
 
Other points made by Nielsen I think are merely learning points which are acceptable for a new operating system.  Live Tiles are a great tool when used properly (which Nielsen says).  The Charms work in a very similiar manner to the well-known taskbar.  You just have to "learn" to use it.  Windows 8 is gesture heavy, but once you learn it you are better off.
 
New technology requires some learning.  Windows 8 learning curve is about half-an-hour to a hour for any user well aquanited with a previous Windows OS.
 
Windows 8.  It's pretty darn good. 

Tuesday, October 23, 2012

Hosting RavenDB on IIS 7.5

Hosting RavenDB on IIS 7.0 is simple and well-documented, but you may find yourself running into a few issues.  It will also take some additional work if you want to secure your database and restrict access.  The following tips will get you up and running.

Word on IIS Configuration

If you haven't used IIS before or are starting this installation from scratch you may be wondering, where is IIS?  Assuming your version of Windows has IIS, you'll need to go to the Turn Windows features on or off menu from within the Control Panel.  From here, you can enable Internet Information Services.  It is important that you explicitly enable ASP.NET and any security features that you want to use.  This article will demonstrate how to set-up Basic Authentication.

Selecting IIS Features
One more thing to keep in mind. You may need to install ASP.NET from the command line using the aspnet_regiis.exe -i command. 

ASP.NET Installation

Accessing The Management Studio from the Web

RavenDB comes equipped with a nice Silverlight-based management interface.  Accessing it via the web in convenient and, more importantly, let's you know that your database is operational.  To set-up:
  • Download RavenDB and extract the Web folder.
  • Create a new web site with the physical path set to the Web folder just extracted.
At this point, going to localhost will bring up the Default instance of your RavenDB.  However, you won't be able to access it from any other machines on your network.
  • Add an inbound rule to your machine's firewall allowing communication over the port (TCP) assigned to you RavenDB installation.  By default, the port is 8080.
Now you will be able so get to the database from any machine with in your network.  A step in the right direction for sure, but you still won't be able to access it from outside your network.  To allow this you will need to configure any hardware, such as a router, sitting between the machine hosting RavenDB and the Internet proper.
  • Configure your network firewall to allow communiction over the same port as defined on your machine's firewall.
To verify that everything is working as expected, use your external IP address to get to RavenDB's Management Studio.

Securing RavenDB with Basic Authentication

It's great that you can access your RavenDB from anywhere, but maybe not so great that this also applies to everyone else.  To secure your database:
  • Create a Local User account.  This account will be used specifically to provide credentials for accessing RavenDB.
  • Add the new user account to the IIS_IUSRS group.  This will provide the user access to resources required to use the database.
  • In IIS, disable Anonymous Authentication and enable Basic Authentication for the RavenDB website set-up earlier.
That's it.  Now a username and password will be required to access The Management Studio.

References

Installing IIS 7
RavenDB - Deploying as an IIS application
What is my IP address?