tags:

views:

31

answers:

2

I have two executables that access a DLL containing Data Access Layer objects Each exe can have a different config containing info on server and database name, connection string etc.

How do I pass the config info to the DAL objects?

Obviously I can pass the config strings to the DAL objects every time I instanciate one, but that seems messy and repetitive as every class in the DAL will require Properties for the Config strings..

Can anyone tell me if there is a better way?

I am wondering if there is a way for the instanciated DAL objects to read some properties from the calling object without me having to pass them explicitly?

A: 

What about having all DAL classes derive from a base class which contains the connection string property?

In that case, you could call the base's constructor on the child contructor invocation and that base contructor would then be the single place for loading the Connection string.

If you have the same ConfigKey used in both Exe's eg: "DBConnectionString" - then the contructor will just load the values from config files "DBConnection" entry - no matter which exe is calling the dal and the correct value of connection string will be passed to it

Implementation would be something on below lines... Sorry for using C# - like syntax. i last used VB very many years ago.

class BaseDAL
{
  protected String _connectionString;
  BaseDAL ()
  {
    _connectionString = ConfigurationManager.AppSettings("DBConnectionString") 
  }
  ....    
}

class ChildDAL : BaseDal
{
  ChildDAL():base()
  {
  ....
  }
}
InSane
Ok I understand inheriting from a base class will remove my need for a connection string property in the DAL classes which is good.But Im not sure about what you are saying about passing the connection string from the exe to the DAL classes as they are instanciated.Are you suggesting that it is passed to each DAL object as it is created in the constructor?
zebidy
@zebidy - just edited the code - Refer constructor for BaseDAL. Its the single place which has the logic of looking up the Connection string. The DAL - on instantiation - will be able to get the connection string this way. No need to pass anything.
InSane
That is awesom- thanks very much exactly the guidance I was looking for.
zebidy
A: 

Maybe I am missing something but you can just put the connection string into App.config or Web.config.

<appSettings>
    <!-- This sets the server, the username, and the password to connect to the database -->
    <add key="SqlConnectionString"
             value="Data Source=servername;Database=dbname;User ID=username;Password=password;Charset=utf8;"
    />
</appSettings>

Then, in your code you simply read it in:

Shared connectionString As String = ConfigurationManager.AppSettings("SqlConnectionString")

You only have one App.config per EXE I would think.

If you are truly having a different connection string for each instance of an EXE running you could store it in cache instead but I do not imagine that this is what you are doing.

EDIT: The connection string I use in my example would connect to MySQL. Obviously the connection string itself will vary from database to database.

Justin
thanks but storing and retrieving the config strings is not the issue.I think I would like the instanciated DAL objects to be able to read the config strings without me having to pass each one to them explicitly as a property.
zebidy