Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 6th, 2004, 01:55 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Connection String For Classes

In an effort to understand how to best use classes I am trying to create a class that represents a person that I can then use in various applications, this class would be able to insert, delete and update records in a table "Person" as well as related tables "PersonPhone" and "PersonEmail".

In order to be able to use this class in different projects I would need to be able to set the connection string dynamically, using the web.config for web apps and whatever method is recommended for windows apps (I have not done any c# windows apps yet).

So...My Question Is.
What is the best method to enable this dynamic setting of the connection string.

Should it be a parameter in the Person constructor?
Should it be a property that must be set before use?

I am on very unfamilliar territory here so please feel free to state the obvious.



======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old July 6th, 2004, 02:26 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It depends a bit on how far you want to take this. In a pure solution, your person class wouldn't know about the database or connection string at all. It could call a method in the Data Access Layer that performs the save operation. This allows you to create a Business Layer (where the Person lives) that can be reused in multiple applications and multiple databases. The Database Layer in turn can deal with a specific database.

Anyway, even if you abstract away the data access from your business object, your question still applies to the Data Access Layer. There are still a couple of ways to do it:

One is to create a static connection string property on your DAL class. On start up of the application (for example, Application_Start in Web, Form_Load in Windows forms), you retrieve the connection string from the appropriate config file and assign it to the connection string property.

Another possibility is to create your own configuration file handler that can read Web.config and Application.config files. With a switch between Web and Config you can have the same class read multiple type of files. Take a look here (Google's cached version, as the original server could not be found): 66.102.11.104/search?q=cache:a7rY0PpP58MJ:http://www.eggheadcafe.com/articles/...onfigDoc&hl=en

(I left out http:// before 66.102.11.104 to partly fool the automatic linker that messes up these type of links)
Bottom line is: The easiest way to do is is probably a static property on the class that performs the database actions. When the application loads, you retrieve the connection string from the appropriate location, and assign it to the static property where it will stay for as long as the application lives.

I am sure there are many more possibilities, so I hope other people contribute to this thread as well.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Swallow by Placebo (Track 10 from the album: Placebo) What's This?
 
Old July 6th, 2004, 02:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks Imar,
Whilst it is a small example I do wish to take it as far as possible and will try and impliment the data access layer as suggested.

In regards to a ConnectionString property of the Data Access Layer.

If I set the value in the Application_Start for an asp.net app with somehting like

DAL.ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"]

Will that DAL object persist?



======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old July 6th, 2004, 03:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If DAL is the name of your Data Access Layer class and not the name of an instance of that class: Yes (and no). The DAL object will not really persists as there isn't a true instance, but the connection string property will still be accessible through the class name.

If you create static properties, you can access (and change) the static properties and methods of a class without the need for an actual instance.

You may also want to take a look here:

http://p2p.wrox.com/topic.asp?TOPIC_ID=12408

and here:

http://p2p.wrox.com/topic.asp?TOPIC_ID=2064

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Wüste by Einstürzende Neubauten (Track 6 from the album: Tabula Rasa) What's This?
 
Old July 6th, 2004, 03:51 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks again Imar,
You deserve a nighthood.

I'll take all that in give it a go.

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================





Similar Threads
Thread Thread Starter Forum Replies Last Post
connection string kooky SQL Server ASP 1 February 14th, 2008 09:59 AM
connection string Sheraz Khan ASP.NET 2.0 Basics 3 July 5th, 2007 08:33 AM
Connection String jmss66 Oracle 4 August 8th, 2006 03:19 AM
connection string nalla ASP.NET 1.0 and 1.1 Professional 2 January 23rd, 2006 04:03 AM
Connection String aadz5 JSP Basics 1 January 24th, 2005 04:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.