6 Replies - 1788 Views - Last Post: 09 November 2008 - 04:59 AM

#1 rgalpin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-November 08

Code organization, design

Posted 05 November 2008 - 04:22 PM

Quick background comments
- this is my first post
- i'm a self taught ASP guy trying to learn ASP.NET using Visual Studio 2005
- C#

My first .NET project - and I ended up with one giant class in one file. It works fine. But it seems like bad design. Should I have more than one class? I have 4 main parts and I'm thinking each part could go in it's own class for organizational purposes if nothing else. Please go easy on me - I'm self taught and I may be way out in left field - so I'd appreciate some help.

It's a Search engine of sorts. Users enter search criteria, and the database returns records which contain matching items.

Logically the code breaks down into the following actions:

1. Fire off a search on button click
2. Search the database for a match and return dataset
3. Process data returned to determine correct display
4. Assign HTML to a stringbuilder variable, assign that to a panel in the web form for display

I had all this happening in a class:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;

public partial class Search : System.Web.UI.Page
{ 
	 
	  protected void Page_Load(object sender, EventArgs e)
	{  
	  //code
	}
	// FIRE  ///////////////////////////////////////////////////////////////////////
   protected void ExhibitSearch_Fire(object sender, EventArgs e)
	{
	  //code
	}
   
	// FIND  ///////////////////////////////////////////////////////////////////////
	protected void ExhibitSearch_Search(string SearchKey, string search_value)
	{
	 //code
	}

	// PROC  ///////////////////////////////////////////////////////////////////////
	public void ExhibitSearch_ProcGeo(DataSet dsInstances, DataSet dsAffiliates, string SearchKey, string search_value)
	 {
	 //code
	}

	// DISPLAY ///////////////////////////////////////////////////////////////////////////
	 public void DisplayEvents(DataSet dsInstances, string search_value)
	{
	 //code
	}
	





Each method is quite long and there are more methods than listed here...

I thought I would try and make each method its own class. But maybe that's stupid - because it sure didn't work. I got a lot of errors about things "do not exist in current context."

Am I like totally lost in the wilderness here? Or is there a good way to break this one big class into logical pieces.

My appreciation for anyone who has the patience to try and help me through the wilderness. ha ha.

This post has been edited by rgalpin: 05 November 2008 - 04:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Code organization, design

#2 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Code organization, design

Posted 06 November 2008 - 09:04 AM

Well I can't tell you exactly which methods should go into another class, and you may even need to only push parts of methods into other classes. However Unless your application is super tiny you should probably break it up into more than one class. I'm just finishing up my first asp.net page from a background in Java, so I've had a bit of a learning curve with the code, but some of the basic class breakup ideas translate. I'm also fortunate to be on a team so my coworkers were able to give me pointers on how to break things up.

It looks like the code you posted is all from a code-behind class (it has the extension pagename.aspx.cs and is right under a .aspx page in the solution explorer). You should have one code-behind class for every .aspx page in your app. You should also have a folder called App_code or something like that (it may be different if you told VisualStudio you were creating a web page rather than a web app, sorry I don't know I'm new to this too). In my application I have my database access class and a utility class.

Typically you should use the code-behind pages like a middle-man between your database access class(es) and the .aspx page. They should handle stuff like button clicks and any dynamic page formatting. You should use a database access class to do all of your database retrieval so it will probably have a lot of static methods. If you have a utility class it should probably do stuff like data checking or other common tasks that don't really fall under data retrieval or data display.

So for your example you might have something like this happen:

>User enters a search query on the page and hit's the submit button.

>The event handler method on the code-behind page yanks the query from the search box and passes it on to the search method in your database access class.

>The search method scans the database for records that match the query and shoves all of them into a dataset, or whatever you're using to pass data around your app, and returns it to the search event handler back in your code-behind class.

>The search event either displays the data on the page or passes the dataset on to other methods in your code-behind class which will format the data and push it to the proper parts of the .aspx page.

You may want to check the query string for integrity in either the event handler or the database access method. Typically if I wanted to just silently handle any errors you could put it in the database access method, but if I wanted to give the user feedback about why I was going to reject their input I'd put the check in the event handler so it could access the .aspx page.

I don't know if I've explained this well, but I hope it answers your question. If you need me to clarify anything I'll do my best.
Was This Post Helpful? 0
  • +
  • -

#3 rgalpin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-November 08

Re: Code organization, design

Posted 06 November 2008 - 10:37 AM

Thanks 5th Wall - Your answer helped me reform my question. I think my problem is I have basic confusion on how to leverage the framework to accomplish the objective of my app.

Here's an analogous, fictitious program that follows the same logic as my app.

Lunch
--Burger
----Cook burger
----Dress the burger bun
----Add burger to the bun
----Wrap the burger
--Hotdog
----Cook Hotdog
----Dress the Hotdogbun
----Add Hotdog to the bun
----Wrap the Hotdog
--Deliver Lunch [burger || hotdog]

What approach would you use to make this app?

Here's what I have now:
public partial class Lunch

//on button1_click
makeBurger {
   cookBurger()
}
//on button2_click
makeHotdog {
   cookHotdog()
}

//COOK EM
cookBurger(){
   //code to cook burger
   //call dress method
   dressBurgerBun()
}
cookHotdog(){
   //code to cook hotdog
   //call dress method
   dressHotdogBun()
}

//DRESS EM
dressBurgerBun(){
   //code to dress burger bun
   //call add meat method
   addBurger()
}
dressHotdogBun(){
   //code to dress hotdog bun
   //call add meat method
   addHotdog()
}

//ADD MEAT
addBurger(){
   //code to add burger to the bun
   //call wrap method
   wrapBurger()
}
addHotdog(){
   //code to add hotdog to the bun
   //call wrap method
   wrapHotdog()
}

//WRAP EM
wrapBurger(){
   //code to wrap burger
   //call deliver method
   deliverLunch('burger')
}
wrapHotdog(){
	//code to wrap hotdog
   //call deliver method
   deliverLunch('hotdog')
}

//DELIVER
deliverLunch(food myFood){
   //code to deliver myFood
}
}


I'm self taught - and I work by myself - so this may be sucky design - but it works. :)

I want to find an elegant, efficient design or even just one that follows an acceptable standard - so that's why I'm here asking for help. Thanks!

This post has been edited by rgalpin: 06 November 2008 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 5thWall  Icon User is offline

  • Occasional Member

Reputation: 31
  • View blog
  • Posts: 530
  • Joined: 17-September 08

Re: Code organization, design

Posted 07 November 2008 - 08:44 AM

Alright if I understand your (fake) app correctly you just want to return a hotdog or burger based on which button the user clicks?

I'd probably do something like this:

public class lunch {

  //on button1_click
  makeBurger {
	Burger burger = kitchen.cookBurger();
		Bun  burgerBun = dressBurgerBun();
		burgerBun.add(burger);
		Food lunch = lunchutilities.wrap(burgerBun);
		deliverLunch(lunch);
  }

  //on button2_click
  makeHotdog {
		Hotdog hotdog = kitchen.cookHotdog();
		Bun  hotdogBun = dressHotdogBun();
		hotdogBun.add(hotdog);
		Food lunch = lunchutilities.wrap(hotdogBun);
		deliverLunch(lunch);
  }

  dressBurgerBun() {
	 //dress burger bun code
	 return bun;
  }

  dressHotdogBun() {
	 //dress hotdog bun code
	 return bun;
  }

  deliverLunch(food myFood) {
	  //deliver food code
  }
}

pubilc class kitchen {

  cookHotdog() {
	//cook hotdog code
	return hotdog;
  }

  cookBurger() {
	//cook burger code
	return burger;
  }
}

public class lunchutilities {
  wrap(Bun) {
	 //code to wrap either hotdogs or burgers
	 return food;
  }
}


This post has been edited by 5thWall: 07 November 2008 - 09:12 AM

Was This Post Helpful? 0
  • +
  • -

#5 Footsie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 24
  • View blog
  • Posts: 370
  • Joined: 20-September 07

Re: Code organization, design

Posted 07 November 2008 - 12:24 PM

Quote

... because it sure didn't work. I got a lot of errors about things "do not exist in current context."

This I think is caused by the fact that you must first instantiate an object of that class before you call a method of that class.

A class is just a "blueprint" of what an object will look like. The class can have different variables and methods that help to describe what the object will look and act like.

It depends how far you want to go with object oriented techniques as to how separate your classes can be. For example you could have a base class of Sandwich which implements all the methods that are similar and required (if you look at your example, all the methods are the same cook, dress, add, wrap) its only the implementation that differs (cook hotdog; cook burger). You could create an abstract class that forces you to use the methods but differently for each sandwich: (example using a console app)
public abstract class Sandwich{
  public abstract void Cook(); //empty method - no implementation, derived class will implement
  public abstract void Dress();
}



Then when you create a Burger or Hotdog class (which you could do to separate classes):
//derive from base class sandwich
public class Burger : Sandwich{
//override keyword uses abstract method and implements
  public override void Cook(){ //must implement here
    Console.WriteLine("Cooking Burger");  //change to "Cooking Hotdog" in hotdog class
  }
  public override void Dress(){
    Console.WriteLine("Dressing Burger");
  }
}


Now when you want to use these classes as objects you create one first then call the method.
//object is of class Burger
Burger myBurger = new Burger();
//call the objects methods
myBurger.Cook();
myBurger.Dress();


Output should be:
Cooking Burger
Dressing Burger


If you had a Hotdog class and different impementations for the methods and you created a Hotdog object, the output could be:
Cooking Hotdog
Dressing Hotdog


They can be situated in different code files in your project but as long as they are part of the same namespace you can use them. Otherwise you must add the using [your-namespace-here] directive at the top of your page to access the namespaces classes.

Whew!
Sorry for the long post, but once I got going I couldn't stop. ;)
Hope you can understand and that it helps a little.
Was This Post Helpful? 0
  • +
  • -

#6 rgalpin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-November 08

Re: Code organization, design

Posted 08 November 2008 - 09:43 AM

Thanks! The scope of my question is huge and I appreciate you taking the time to address it!


View PostFootsie, on 7 Nov, 2008 - 11:24 AM, said:

public abstract class Sandwich{
  public abstract void Cook(); //empty method - no implementation, derived class will implement
  public abstract void Dress();
}



Then when you create a Burger or Hotdog class (which you could do to separate classes):
//derive from base class sandwich
public class Burger : Sandwich{
//override keyword uses abstract method and implements
  public override void Cook(){ //must implement here
    Console.WriteLine("Cooking Burger");  //change to "Cooking Hotdog" in hotdog class
  }
  public override void Dress(){
    Console.WriteLine("Dressing Burger");
  }
}


This is a help. It helps me to consider an approach that takes advantage of the object oriented techniques - something I am not used to. Instead I'm forcing my old approach into the object oriented framework. I gotta get past that.

So... why does it help to create the Sandwich class? It seems like you could just skip that and start at the Burger class since the methods are not defined until the Burger class anyway. I'm not questioning the validity of doing that. I'm just wanting to understand the reason/benefits of it.
Was This Post Helpful? 0
  • +
  • -

#7 Footsie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 24
  • View blog
  • Posts: 370
  • Joined: 20-September 07

Re: Code organization, design

Posted 09 November 2008 - 04:59 AM

Looking back at my post I can see the confusion.
I'll try to explain a bit better.

Quote

It seems like you could just skip that and start at the Burger class since the methods are not defined until the Burger class anyway.

This is true and you could just have 2 classes (burger and hotdog) here in a small project, but you'll find that even with just 2 classes you'll have a lot of duplicated code, or code that is very similar. This is the main drawcard of a base class to inherit from - it enables code re-use. (I didn't indicate this clearly in my previous post).
The use of abstract methods forces the inherited class to create their own implementations. So, if you know a method needs to be used but you don't know the details of how it will you can create an abstract method.

Sandwich class: (must be abstract if it contains any abstract methods)
public abstract class Sandwich
    {
        public abstract void Dress();
//grill method
        public string Grill(string item)
        {
            // imagine a really long method here
            //
            return ("Sandwich Base Class:" + item + " Grilled");
        }
//fry method
        public string Fry(string item)
        {
            // imagine a really long method here
            //
            return ("Sandwich Base Class:" + item + " Fried");
        }
    }



Burger class which inherits from Sandwich class above.
//derive from base class sandwich to enable code re-use
  public class Burger : Sandwich
    {
        string meat = "Patty";
        
        //override keyword uses abstract method and implements
       
        public override void Dress()
        {
            Console.WriteLine("Burger Class: Overriden Dress Method called");
        }
        //constructor for burger - this will automatically call the methods
//contained when a burger object is created
        public Burger()
        {
//because we have inherited from the base class sandwich all we do is call
//base.Grill() or Fry() from the sandwich class. We don't need to re-code 
//the long method again
           Console.WriteLine(base.Grill(meat));
           Console.WriteLine(base.Fry(meat));
        }
    }


Now imagine you had many classes (hotdog, burger, steak roll, vege burger) all which need to either grill or fry their meat and you can see the amount of work it can save.

Now to create the object and generate output on the console screen:
//create a burger object - calls methods which are derived 
//from sandwich class
Burger myBurger = new Burger();

myBurger.Dress();



Output:
Sandwich Base Class: Patty Grilled
Sandwich Base Class: Patty Fried
Burger Class: Overriden Dress Method called



I hope you can see how you can derive from a base class here and enable code re-use to save coding time.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1