Solved

Variables Or KeyValuePairs

Posted on 2006-07-17
10
237 Views
Last Modified: 2010-04-16
Hi all,

I am designing an app, and would like ot make it as scalable as possible, i have a number of objects, most corresponding to tables in a database.  At the moment i have the basic - Private Variables, Public Properties set up.

However someone at work told me it is better have a single hashtable per objects so you can store you variables Names/Values in there, that way you are not tied down.

I cant see the point, however is it really the best way to go about it?

Thanks in advance

Apresto
0
Comment
Question by:apresto
  • 5
  • 5
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17120370
It depends on the situation, but its certainly an option. The downside is that the HashTable will store everything as an object (or string if you use the specialized version), so you will have to cast each value when you retrieve it from the HashTable and return it from your property.

So, unless you are only storing strings then its probably more trouble than its worth.
0
 
LVL 23

Author Comment

by:apresto
ID: 17120395
Hi there, Thanks for the reply, I can see what you mean.

I personally don't like the idea, i find it much easier, and tidier to use members and properties, however i'm after scalability.

The argument the guys at work have is that if you were to add a field into your database you would need to change your code etc etc to suppose that field, so a hashtable is ideal.  I am in the frame of mind that if the database was right in the first place additional fields shouldnt be that frequent, but its always a possibility i suppose.

I'm torn between the 2 to be honest....i'll see what anyone else has to say before i close

Thanks again
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17120410
This is probably a situation where you should be using interfaces. You create an interface for the initial version of the class, then if you add new propeties at a later date you simply create a second interface with the new properties. That way you don't "break" any client that relies on the original version.
0
 
LVL 23

Author Comment

by:apresto
ID: 17120436
i see, so if i use an interface, do you mean to use it in conjunction with the hashtable so i can access everything via property. And if i create a second version, have the new interface have the same properties as the old one plus any additions?

Thats a good idea, i have implemented some interfaces at the moment - but i think that this suggestion could solve many problem!

Thanks very much
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17120462
I would go with the interfaces, but not the HashTable (unless you are using purely string data). Plus you are always going to take a slightly bigger performance hit using a HashTable than you would using built-in types.

>> And if i create a second version, have the new interface have the same properties as the old one plus any additions?

Even simpler, have the new one derive from the original, plus the new properties:

   public interface Shape
   {
        double Width
        {
            get {}
            set {}
        }

        double Height
        {
             get {}
             set {}
        }
   }

    // new interface, extends original
    public interface Shape2 : Shape
    {
        double Diameter
        {
            get { }
            set { }
        }
    }
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 23

Author Comment

by:apresto
ID: 17120495
As i see, i do like the idea of using inteface, as i was always told this was good practise.

I agree that hashtable would prob hit performance, however the reason i was told to consider hashtables was because i could easily add variables to an object, simply by adding a key/value pair to the hashtable within that object, so its scalable in the sense that i would not need to bebuild the app and implement it every time i add a field. i would just add it to the hashtable and 'theoretically' that object now contains an assigned variable.

I aplolgise for naivety as i'm an not expert on interfaces, but if there a way i could use interfaces to implement a similar solution that hashtables provide above?

>>>Even simpler, have the new one derive from the original, plus the new properties

Genius! :o)
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17120526
How would you go about adding the new value to the HashTable without changing the code ? How would the rest of your code know it was there in order to retrieve it ? If you added it via some sort of config whats to stop anyone else adding rogue values ? How would you app be able to alter the value, and how would you prevent it being set to an invalid value ? Would your HashTable be read-only ?

If you added a new value to a HashTable dynamically without modifying the code you would have a hard time treating it as the correct type.
0
 
LVL 23

Author Comment

by:apresto
ID: 17120559
I see what you mean, however the app in question is a data trafficing application.

it has a client on the application and server.

Data is retrieved from a stored procedure and it added to an object,
the object then converts its properties to xml and passes down to the app sitting on the clients machine
which then creates a summary report depending on the xml passed down to it.  
At the moment i use reflection to retrieve a list of variable names within the object and their values for the xml conversion, and i am happy with it. however i can imagine it would be easier with hashtable

However i ask this question, not only for this task, but also for future devewlopment "Do's and Dont's".

I am against using hashtable as variables and properties are there for a reason and i refuse to believe that it is better practise to use hashtable - which is why i ask question.  But i agree with the interface advise, i understand your point above, and was kind of expecting it :o)

As long as there is not something magical that interfaces can do, i think i have gained what i came here to gain from this thread. :o)

do you understand what i mean above?

Thanks you very much for your participation
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17120608
Good rule of thumb:  If it feels wrong then it probably is.

At the end of the day you have to go with what best suits your situtation, even if it isn't 100% good OO. As long as you are aware of the downsides to the design and can justify them then go with it.
0
 
LVL 23

Author Comment

by:apresto
ID: 17120622
Thats a good Rule,

Thanks for your help, thats some good advise

Apresto

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now