0

I am using c# but the question was more towards software engineering principals so I am asking the question here.

There are so many questions here but mostly they say :"How to use global variables in c#?" and "Are global variables are bad?". But my question is about "How" to avoid global variables in my scenario.

I've written a class that works as data access layer. I've 2 objects of this class. One pointing to main database and second pointing to reports database. The connection string are not constants rather a form is displayed at application startup and user enters the server name, login name and password. Now all other classes need to use these objects to work with database.

I have a strong feeling that these 2 objects should be global but I want to avoid if somehow this is possible?

MJ Khan
  • 101
  • 1
  • 3
  • 1
    Possible duplicate of [How to avoid global state without large function signatures?](https://softwareengineering.stackexchange.com/questions/257098/how-to-avoid-global-state-without-large-function-signatures) – gnat Apr 29 '17 at 08:53
  • @gnat : I don't think it is duplicate.. I saw that question earlier and I could not find any relation with my scenario... – MJ Khan Apr 29 '17 at 18:41
  • I'm not quite sure why you are creating a data access layer and then having your other classes work with items like connections that should presumably be encapsulated internals of that layer. What's the point of a data access layer if your other classes are going to do their own data access? – Bradley Thomas Apr 30 '17 at 05:49
  • @BradThomas .. Other class are neither working with connections nor doing their own data access. So how are they supposed to use the functionality of DAL class? Definitely I've created 2 objects say DAL dal1, DAL dal2. I am talking about these 2 objects that hold connections internally. – MJ Khan May 02 '17 at 03:54

2 Answers2

2

It is a question of dependencies. The connection string and credentials could be passed to the constructor of the data access layer class. Objects depending on the DAL could be passed a reference to the DAL instance in their constructor.

If you have two seperate DAL's, objects which depend on both would be passed both DAL instances in the constructor.

By passing the dependencies in constructors rather than accessing globals, the dependency chain becomes more explicit and you avoid "hidden" dependencies.

JacquesB
  • 57,310
  • 21
  • 127
  • 176
0

I'm somewhat unsure who says that global objects are always bad. There is at least one well-accepted object-oriented pattern that explicitly involves global objects - the singleton pattern. For C#: http://csharpindepth.com/Articles/General/Singleton.aspx

"Global variables are bad" probably just means that you should not store the two instances of your DAL class in some globally accessible variables. And indeed this is not necessary. As you have two instances, the singleton pattern won't work. But you could have a simple factory/pool class. It would have a getDAL(id) method that would take an identifying string ("main" or "reports") and return the DAL object (and create it if it does not exist yet). Then in each object that uses a DAL, you can retrieve the necessary DAL object into a local variable.

Here is the advantage over just having global variables. Imagine a third database is added later. You can switch to using it anywhere by just switching the ID where the DAL object is retrieved, without going through all references to a global variable.

Mikhail Ramendik
  • 1,549
  • 2
  • 9
  • 11
  • 1
    It is pretty well known that singleton is an anti pattern so it makes quite a poor argument for your case. – Esben Skov Pedersen Apr 30 '17 at 18:02
  • @EsbenSkovPedersen I have provided a reference, so at least some sources don't think singleton is an antipattern. Some very experienced developers I know in person (not online) do like it, though I will admit they use Java, not C#. It is, therefore, a matter of opinion, and I suggest we leave it at that. I think it would be nice if you provided your own answer, with your own reference(s), about they way you would resolve the DAL issue. – Mikhail Ramendik Apr 30 '17 at 20:05