Structural question regarding scope of variables

F

Froefel

Hi group,

I've been developing a number of classes, many of which have a similar
structure, as follows:

namespace
{
public class classname
{
Private Fields
private readonly static string connString =
ConfigurationManager.ConnectionStrings["SCMConnectionString"].ConnectionString;

Public Fields

Public Properties

Private Instance Methods

Public Instance Methods

Public Static Methods
// these methods use the connString variable to establish database
connections, using Microsoft.ApplicationBlocks.Data
}
}

However, the static connString is declared in every class. I'm
currently only using one connection, but I foresee using 2 or 3
different connection strings, based on where the data will need to
come from.
Can anyone suggest a better way of doing this, so that I don't have to
declare this variable in every class?
Note that I had considered using inheritance by basing all the classes
that need data on a base class that contains the static connection
string. However, that doesn't seem the right approach because I only
want to achieve the effect of a global variable. Speaking of global
variables, note that I'm targeting a web environment.

Any thoughts on this?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
Froefel said:
Hi group,

I've been developing a number of classes, many of which have a similar
structure, as follows:

namespace
{
public class classname
{
Private Fields
private readonly static string connString =
ConfigurationManager.ConnectionStrings["SCMConnectionString"].ConnectionString;
However, the static connString is declared in every class. I'm
currently only using one connection, but I foresee using 2 or 3
different connection strings, based on where the data will need to
come from.
Can anyone suggest a better way of doing this, so that I don't have to
declare this variable in every class?

Any thoughts on this?

A better approach would be to create a new class, static class, to just hold
the connection string needed. In case that you have more than one connection
then it will be up to the class to determine which one to use. That will
depend of how you divide the data and how the classes how where the data
they need is.
 

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

Top