• (2 Pages)
  • +
  • 1
  • 2

Separating data from GUI - PLUS - serializing the data

#1 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Posted 07 July 2011 - 12:28 AM

*
POPULAR

I recently suggested to someone that separating one's data from the GUI that displays it is an important technique. They responded with... "Well how do I do that?" Which made me realize it was a good topic for a tutorial.

Why do we want to separate the data from the GUI?
One reason would be so we can keep a LOT of information, but only display a subset of it. For example we might have a lot of personal information about an employee like their pay rate and social security number. But we don't want to display that to everyone on the network. We only show that to people with Human Resources management level access.
Another reason might be so we can use one form for gathering data, and other for displaying it.
But my personal favorite need is so I can serialize/deserialize the information. Remember that GUI controls can't be serialized. So if you have a contact form like this one

Attached Image

how do you plan to save the data?

Of course you know you should NOT just directly access the GUI controls in every line of your code. GUI controls should always be kept private. Other classes should only change public properties. This way you can rearrange the form, change names, do whatever you need to do under-the-hood and not break all of your references to the properties.

Code
Spoiler


But this still makes the data (the properties) a part of the form. So what do we do?

We make a class for your contact.

Attached Image
Attached Image

ContactObject code
Spoiler


Notice that our ContactObj has more data than this form shows. That's one of the points we made earlier; that we can have more info and only show what this user's privileges allow.

We now have an object, but how do we update our earlier form to take advantage of it?
We add an instance of the ContactObj, [...]
Spoiler


and update our properties to use it.

Attached Image
Spoiler


Now it is just a matter of adding a couple methods to update the gui and update the object
Spoiler


We create a little Tester form to call our Contact form, then save the new data as an XML file after the Contact form closes.

Attached Image
Spoiler


And voila, we have a contact form with the data separated from the GUI. Our main form (tester in this case) can give the user a new contact form to obtain new data, and then save it.

Attached Image

But remember it could just as easily get a List<ContactObj> from a database. This would allow you to get a lot of contacts without creating a lot of forms for no reason. You could get a ContactObj and feed it into a different form that shows all the hidden properties and so on.

If you're running VS10 I've attached the finished solution.
If not, here's the final code after all the edits.

Program.cs
Spoiler


frmContact.cs
Spoiler


ContactObj.cs
Spoiler


TestForm.cs
Spoiler



Attached File  SeparateDataAndGUI.zip (89.92K)
Number of downloads: 502

This post has been edited by tlhIn`toq: 07 July 2011 - 12:40 AM


Is This A Good Question/Topic? 19
  • +

Replies To: Separating data from GUI - PLUS - serializing the data

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,456
  • Joined: 08-June 10

Posted 07 July 2011 - 08:40 AM

Great tutorial. I need to match this with one for WPF, since the concept is absolutely the same, but the available mechanics are different (MVVM with data binding).

You could go even further by making the contact form a UserControl, so it can be used anywhere, including it's own form, just for flexibility.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Posted 07 July 2011 - 09:59 AM

View PostCurtis Rutland, on 07 July 2011 - 09:40 AM, said:

Great tutorial. I need to match this with one for WPF, since the concept is absolutely the same, but the available mechanics are different (MVVM with data binding).
Thanks. And go for it!

Quote

You could go even further by making the contact form a UserControl, so it can be used anywhere, including it's own form, just for flexibility.


Absolutely. I'm a huge believer in UserControls. But I didn't want to preach that too strongly considering it is very much the underlying basis of these tutorials...
Bulding an application - Part 1
Building an application - Part 2
Was This Post Helpful? 1
  • +
  • -

#4 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 307
  • Joined: 30-January 11

Posted 09 July 2011 - 03:02 AM

View PostCurtis Rutland, on 07 July 2011 - 03:40 PM, said:

Great tutorial. I need to match this with one for WPF, since the concept is absolutely the same, but the available mechanics are different (MVVM with data binding).

You could go even further by making the contact form a UserControl, so it can be used anywhere, including it's own form, just for flexibility.

For WPF take a look at


http://www.microsoft...lang=en&id=4922

http://compositewpf....ases/view/55576

Quote

Prism provides guidance designed to help you more easily design and build rich, flexible, and easy to maintain Windows Presentation Foundation (WPF) desktop applications and Silverlight Rich Internet Applications (RIAs) and Windows Phone 7 applications. Using design patterns that embody important architectural design principles, such as separation of concerns and loose coupling, Prism helps you to design and build applications using loosely coupled components that can evolve independently but which can be easily and seamlessly integrated into the overall application. Such applications are known as often referred to as composite applications

already mentioned in topic in .Net forum
Was This Post Helpful? 0
  • +
  • -

#5 Theomi  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 88
  • Joined: 17-January 10

Posted 09 July 2011 - 06:09 AM

Nice tutorial, just skimmed through it but i have bookmarked it so i can read it in depth later :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10175
  • View blog
  • Posts: 37,580
  • Joined: 27-December 08

Posted 09 July 2011 - 08:13 AM

Awesome tutorial on code separation! This is definitely a concept which people need to utilize more in their programs. :)
Was This Post Helpful? 0
  • +
  • -

#7 UziTech  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 64
  • Joined: 26-October 10

Posted 11 July 2011 - 08:48 AM

I recently started Android programming and i18n is so easy because they do this with strings.xml

Since then I have started using a strings file in my c# code as well.

Very good tutorial and very good idea.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Posted 11 July 2011 - 10:26 AM

By a "strings" file, do you mean a file with all the strings used in your application?

I usually do this with the resources of the component. That makes it easy to globalize/localize an application. You just open the form/file, change the Language property and replace all the text.

Now the correct 'version' gets installed when the user installs the "english" or "Russian" or "Polish" version from the installer. This way InstallShield & Visual Studio do all the work and I don't have to do anything in code for the change, except to be sure to add all my strings to the correct resource file and use them instead of hard coding the strings.

string response = SomeMethodThatReturnsAboolForSuccess() ? Properteries.Resources.txt_Okay : Properteries.Resources.txt_Fail;



Was This Post Helpful? 0
  • +
  • -

#9 UziTech  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 64
  • Joined: 26-October 10

Posted 13 July 2011 - 10:06 AM

View PosttlhIn`toq, on 11 July 2011 - 11:26 AM, said:

By a "strings" file, do you mean a file with all the strings used in your application?


Yes, sometimes I use the resources but I have found it is easier and more convenient to create an "en.lang" file with all strings so I can localize it and others can change the strings if they want to. Especially for other languages since I just use Google translate to translate the "en.lang" file to other languages. Other people have emailed me better translated language files that they have updated to include in updates.(After checking Google translate to make sure they didn't write something bad of course)
Was This Post Helpful? 1
  • +
  • -

#10 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Posted 11 October 2011 - 04:31 AM

View PostCurtis Rutland, on 07 July 2011 - 04:40 PM, said:

Great tutorial. I need to match this with one for WPF, since the concept is absolutely the same, but the available mechanics are different (MVVM with data binding).

You could go even further by making the contact form a UserControl, so it can be used anywhere, including it's own form, just for flexibility.


Any update on this tutorial? Think it would be really useful :)
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Posted 11 October 2011 - 05:40 AM

What kind of 'update' were you thinking of?
Was This Post Helpful? 0
  • +
  • -

#12 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Posted 11 October 2011 - 07:52 AM

Sorry should have been clearer - it was regarding Curtis' idea of doing the same tutorial as you have done here, but aimed at WPF MVVM and data binding :)

Apart from that your tutorial is spot on :) unless you would like to incorporate the User Control side of things
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Posted 11 October 2011 - 08:14 AM

View PosttlhIn`toq, on 07 July 2011 - 10:59 AM, said:

View PostCurtis Rutland, on 07 July 2011 - 09:40 AM, said:

Great tutorial. I need to match this with one for WPF, since the concept is absolutely the same, but the available mechanics are different (MVVM with data binding).
Thanks. And go for it!

Quote

You could go even further by making the contact form a UserControl, so it can be used anywhere, including it's own form, just for flexibility.


Absolutely. I'm a huge believer in UserControls. But I didn't want to preach that too strongly considering it is very much the underlying basis of these tutorials...
Bulding an application - Part 1
Building an application - Part 2


I gave Curtis my blessing to take my tutorial and make a WPF version and I beleive it did that a long time ago.

As for the UserControl side of it mentioned, I pointed out that the two tutorials I already created go heavy into making UserControls.

So I think all of what you are looking for has already been created.

Hey Curtis: Could you provide a link for the WPF version of this tutorial that you did?

This post has been edited by tlhIn`toq: 11 October 2011 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,456
  • Joined: 08-June 10

Posted 11 October 2011 - 08:48 AM

You know, I forgot all about that. WPF databinding is surprisingly complex, and I'm still learning a lot about it as I use it. I can probably put together a simple binding guide pretty soon, it just won't be comprehensive (because to be comprehensive, it'd have to be a small book).
Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,456
  • Joined: 08-June 10

Posted 12 October 2011 - 03:50 PM

Alrighty, I've put it up:

http://www.dreaminco...pf-databinding/
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2