App.config and class libraries

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am building a class library and want to store a connection string in a
configuration file.

I've read around and it appears that this is not possible because "by
design, class libraries don't have their own config file - they read the
config files from the application they are housed in, such as a WinForm app
or ASP.NET site "

So, with that in mind, is there an alternative solution?

Many thanks,

CR
 
CodeRazor said:
Hi,

I am building a class library and want to store a connection string in a
configuration file.

I've read around and it appears that this is not possible because "by
design, class libraries don't have their own config file - they read the
config files from the application they are housed in, such as a WinForm
app
or ASP.NET site "

So, with that in mind, is there an alternative solution?
Presumeably the code is only of real use when used in a Windows/Web app so
use the app/web.config file of the application that is using your code
library
and define the connection string there? Use the registry?

HTH

AP
Nottingham - UK
 
Your .dll (read .NET code library) should NOT be dependent upon anything.

If one of the objects you expose needs a connection string to work, you
should provide a property accessor to enable the user of your library to
get/set the string.

A .dll should operate as a "black box", i.e. you know what you need to put
in and you know what you expect to get out; anyone using your library does
not want to have to remember to copy the config file too (even you won't want
to do that)
 
Hi,

You can do a couple of things:

1- Force the calling app to initialize it, this works better in an escenario
like yours, where only a couple of values needs to be defined.
2- Provide a config file and parse it yourself, depending of the requirement
this can be stored in the user's application data directory or the
installed location,
3- You can always store info in your old friend , the registry :) ,

OT: I just did a backup of my registry and it 's over 120 MB !!! and this
is computer has less than one year of use


cheers,
 
billr said:
Your .dll (read .NET code library) should NOT be dependent upon anything.

Aren't you making it dependent on the caller setting a connection string?
If one of the objects you expose needs a connection string to work, you
should provide a property accessor to enable the user of your library to
get/set the string.

The question that needs to be answered is, is the connection string part of
the blackness (of the black box) or should it be exposed to the user? Does
the caller even know that a database is used by the dll?
A .dll should operate as a "black box", i.e. you know what you need to put
in and you know what you expect to get out; anyone using your library does
not want to have to remember to copy the config file too (even you won't
want
to do that)

I don't see a difference between requiring the user to copy a configuration
file and requiring them to set a connection string property.
 
I have always felt the class libraries (or more specifically non ui layers)
should have config information passed into them via parameters instead
of having their own config files. I'll assume for the sake of argument
that you have a good reason for doing. So, no need to explain it here.

Here's how to duplicate the System.Configuration.AppSettings class
to be a specific config file. This sample talks about the .NET Compact
Framework
but is applicable to anything.

http://www.eggheadcafe.com/articles/dotnetcompactframework_app_config.asp

--
Robbe Morris - 2004/2005 Microsoft MVP C#

Earn money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp
 

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