Using Connection Class VS Connection Module

  • Thread starter Thread starter Dave Johnson
  • Start date Start date
D

Dave Johnson

Working on Asp.net with access database backend,a serious Question in
the Class design phase, for accessing the database;

Which is Better to make a Connection Class that handles all the ADO
Connections for all the other Objects,

OR

Just make a Connection Module to handle the Database Access,

Wondering Which is Better and Why???? please if u have more Experience
in this Kind of "Design Issue" please Help given reasons and technical
info to support the decision,

Thanks


Sharing makes us all Better
 
If you do not care about object oriented design principles, you probably
won't find one approach more advantageous over the other. Just use
whatever you feel comfortable with.

If you do want an object oriented design, you should never have any
Modules in your code. Everything should be a class, and each class
should have a "single responsibility". One class might have the
responsibility of maintaing the state and operations of a Widget, and
another class might have the responsibility of saving that Widget's
state to the database. I cannot do justice to the principles of object
oriented design in a newsgroup post, but there are various resources
around the net.

Whatever you do, do not try create a class or set of procedures that
opens a single ADO Connection object and keeps it open for all other
procedures to use for the life of the application. You should always
open and close the Connection object immediately before and after you
make a call to the database.
 
Here is a simple example (written outside of VS.NET, so I cannot
guarantee it compiles as-is). I'll assume 1.1 since you did not specify:

public class DataRepository {
private string connectionString;

public DataRepository(){
// retrieve the connection string from appSettings in web.config
connectionString =
System.Configuration.ConfigurationSettings.AppSettings["databaseConnection"];
}

public string[] GetProductNames(){
string query = "SELECT name FROM products ORDER BY name";
ArrayList products = new ArrayList();
using (SqlConnection cn = new SqlConnection(connectionString)){
SqlCommand cmd = new SqlCommand(query, cn);
using(SqlDataReader dr = cmd.ExecuteReader()){
while(dr.Read()){
products.Add(dr.GetString(0));
}
}
}
return (string[]) products.ToArray(typeof(string));
}

// Add more public methods to retrieve data from the database
}

Try to avoid returning "database related" objects (especially not a Data
Reader) from your repository. Return primitive types or custom business
object classes. That way, the rest of your code doesn't have to do any
data interaction, and wont even need a reference to System.Data.dll.

Now to use this class from the rest of your code:

DataRepository repository = new DataRepository();
string[] productNames = repository.GetProductNames();

Notice the constructor pulls the connection string from your web.config
file. So you will need to add the following to web.config (replacing
the connection string with the correct value):

<configuration>
<appSettings>
<add key="databaseConnection" value="data source=(local);initial
catalog=MyStore;integrated security=SSPI;" />
</appSettings>
<system.web>
<!-- bunch of stuff -->
</system.web>
</configuration>


Hope this helps.

Joshua Flanagan
http://flimflan.com/blog
 
Thanks Joshua, you are really an amazing help, i had also checked your
http://flimflan.com/blog its really great and i am really into .net 2.0
but unlucky i have to use the 1.1 for this Project, your Data Repository
Suggestion is REALLY GREAT also saving the Connection String in the
web.config makes all the sense in the world as to easliy change it later
on, But you inspired me with an idea i want to share it with you to have
your feedback and guidance. Instead of making Objects of the "Data
Repository Class" can i inherit it in all my OO project Class, so that
each class inherits its own database functionailty without having to
make another OBJECT, can it be done, that for example I have the Client
Class inherits just the Client GetClientInfo method, and the Order Class
inherits just the GetOrderInfo method, to u see where i am going? is it
possible to be done and would it help to enhance my OO Design. All the
Thanks for Joshua ;)

Sharing makes us all Better
 
what are facade classes? can i by using it solve the problem?

Sharing makes us all Better
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top