Wednesday, September 27, 2006

Garbage collection DO exists in C#

The .NET Framework is a very advanced framework as it has two different types of garbage collection. Like Java it is able to remove objects that is not in use anymore without being told and do it automatically. But .NET also has another GC that Java doesn't have. It is not the normal GC that you would assume, but it is highly integrated into the WinForms framework.

This new garbage collection is a property named Tag in the Control class. This is a place where you can put any garbage into that you could use later. The Controll class will store it for you forever, which is nice when you want to have garbage lying around. So with this enhanced garbage collection, you can store all your garbage and it looks like it is good OO.

The idea behind the Tag property is to store objects for combo box and list items, which is later used when a list item is selected and the object "representing" is needed. It seems that MS has come up with their own solution to the well known Model-View-Control pattern for GUI development. What they came up with is that every GUI control needs to have a model/data, and therefore it is wise to add such functionality to the Control class. The problem is that changing the model (Tag property) does not have any effect at all, the list or combo boxes doesn't change when the Tag property is updated. It is just a place to store objects; it isn't really MVC, is it?

The thing with Java, which tries to enforce the MVC (or at least MV) it is that the GUI devloper has to seperate the model from the view (and control). With .NETs approach you most of the time end up with a Form that has several Control objects and data in one class. I thought OO was to go away from the One-File(-To-Rule-Them-All) idea and seperate code. Now that MS still lets the developers use the Tag property, they are not helping us to seperate logic from representation. And it sucks, because we developers are lazy. We really need someone standing behind our back so we code "correctly".

Once again, I'm sitting wishing that my current project was developed in Java and not in .NET 2.0, because I do not want to work store everything in one class.

9 comments:

Anonymous said...

Why would you use the Tag property? Just add your object to the items collection and overload the class's ToString() and you are done.

You could also do a hundred variations of abstracting the two, if it makes things feel more enterprise :)

redsolo said...

I have no clue why people would use the Tab property. Using the Tag property does not make code that is easier to read or maintain. What I like are frameworks that forces you to code in one way. Coders are way to sloppy and lazy, we need a big brother watching our back so we develop good code.

Today, it is possible to put data into a List (.NET) in three different ways. When I open (for maintenance) a Form that uses Lists/combo boxes/etc, I dont want to have to look what kind of data model approach they are using here. If there is only one way to do something, then you know.

Anonymous said...

Just because there is Tag property it doesn't mean you have to use it. In some cases Tag property is very handy.

E.g. you have TreeView which has hundreds of TreeNodes. Each TreeNode has Tag property so when user clicks TreeNode you'll have reference to the actual object. All you have to do is raise event like NodeClicked(MyObject obj) and then have your controller/presenter class to listen to that event and process it. Otherwise you would have to use some kind of id (can't use text property of the node because there might be several nodes which have same name) or other solution to find out the actual object behind that node.

redsolo said...

Well, if there is a way to code bad and lazy, developers will use it. Especially if there is time pressures on a project.

For the TreeView, I would not recommend putting stuff into the Tag property. But then I dont know how to use TreeView properly. But if it would be Java, I would have to implement the TreeModel interface and therefore seperating Model from View/Control. And that is what we have to be forced to do, use MVC in seperate classes. Not everything in one class/file.

Of course that method is some what more cumbersome when you are stressed or lazy; and therefore people will use the Tag property.

Andy Roberts said...

Redsolo,

Sorry, this is off-topic. I want to contact you based on a comment you left on my blog (SwingBling).

Could you email me (the_r00@hotmail.com), or make your contact details available?

Cheers,
Andy

Anonymous said...

>Well, if there is a way to code bad and lazy, developers will use it. Especially if there is time pressures on a project.

Then you're a bad and lazy developer.

The development triangle: time, functionality, robustness. You can choose between two.

If Java doesn't offer you the first choice, tough.

hcg said...

Hey, Really great work, I would like to join your blog anyway so please continue sharing with us,

Android app developer said...

This is one of the interesting post,I like your blog quality.Thanks for share with us good.

Bandar Bola said...

If some one needs to be updated with hottest technologies then he must be pay a quick visit this web page and be up to date everyday.