Saturday, March 31, 2012

The best way for OOP

Hi, I'm creating an application with a drop down menu that is populated by a database. The application has 4 other sections which also use the same drop down menu, however each of the 5 sections populate the drop down menu based on the section the user is in.

Currently I am using a single code behind file that queries the db and populates the drop down. I have all 4 sections using the same code behind file. Each section sends the code behind a parameter which allows the method to run the specific section query.

My questions are:

1. Is this the right way to do this?
2. Or should I create a class that all sections use? If I should create a class, what is the code behinds purpose, just to call the class?

I guess I'm kind of confused, it seems as though the code behind really is a class, I can place methods inside it and reuse them whereever I want. I would really like to be sure that I'm following OOP and doing this the correct way.

Thanks, DanBUMP, please help. Any takers? Is this a dumb question?
It's not a dumb question. It's a very good one.

You ask whether it is the "right" way to do it. In terms of OOP, then I'd suggest it's not the right way. The way you've described it, you have to pass in a parameter to your method in order for your code-behind to know how to populate the drop-down list. I'm guessing it's like this (using pseudo-code):

Namespace MyApp

Public Class Sections

Public Function GetList( ByVal strSection As String ) As ArrayList

Dim dstToReturn As New DataSet()

Select Case strSection

Case "homepage":
dstToReturn = SQLquery( "SELECT titles FROM Pages WHERE section='home'" )
' for the home page, we also add in a link to the sitemap and search pages
dstToReturn.Rows.Add( "sitemap" ... )
dstToReturn.Rows.Add( "seach" ... )

Case "help page":
dstToReturn = SQLquery( "SELECT titles FROM Pages WHERE section='help'" )
' for this page, we also add in a link to other resources
dstToReturn.Rows.Add( "forums" ... )
dstToReturn.Rows.Add( "other resources" ... )

Case "products page":
dstToReturn = SQLquery( "SELECT titles FROM Pages WHERE section='products'" )

Case "order page":
dstToReturn = SQLquery( "SELECT titles FROM Pages WHERE section='purchase'" )

End Select

Return dstToReturn

End Function
End Class
End Namespace

If I'm correct, then it is poor OOP design because it requires that you know,in advance, all of the ways in which your function might be used. If you add another section to your site, then you'd have to change your function.

This is bad because it violates thevery fundamental Open Closed Principle. To get an understanding of this and other principles of OOP, start atthis page. Follow its links andsearch for any terms you don't understand.

There are many principles and ideas about OOP, so I wouldn't recommend getting too concerned about finding the "right way". Get the gist of the Open Closed Principle, and then just start coding. You'll soon stub your toe in places where you find you should have done something differently, and you'll remember next time.

As a kind of litmus test though ... if you're using If/Then or Select Case statements, then you're probably not creating clean OOP code. Such loops are poor for performance and mean you are looking for thingsknown in advance.

Is that any help?
Dan, what you're doing is ok providing all sections are in the same page. if not then a seperate class may be useful. Also are you using a stored proc to return the data or dynamic sql in the code? if your rdbms supports sprocs then i would use one to improve performance (and to make maintenance easier).

To clear up your confusion, the code behind file IS a class. It inherits from the page class. Your aspx file in turn also compiles to a class that inherits from your code behind class. Cool hey!

Jon.

0 comments:

Post a Comment