Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

From where a class library (.dll) should read its configuration? [closed]

I am already working on a .net project but I think this question can apply to any programming language/framework.

The question is what are the good practices for reading configuration for a class library. I can think of the following options:

  1. Read from the application domain's config file. For example in case of .net web app, read from web.config
  2. Read from specific custom file made for this class lib. Like create a xml file for your class lib and put your configuration there.
  3. The caller should pass the configuration needed to class lib (thru class constructor). So in this case class lib does not directly read from a configuration file but instead expects the caller to send the required information.

Any ideas/preferences?

like image 304
kaptan Avatar asked Sep 27 '12 19:09

kaptan


People also ask

How do I run a class library in Visual Studio?

Right-click on the solution in Solution Explorer and select Add > New Project. On the Add a new project page, enter library in the search box. Choose C# or Visual Basic from the Language list, and then choose All platforms from the Platform list. Choose the Class Library template, and then choose Next.

How to Add class library in console application c#?

Procedure to Create a Class LibraryStart a new project in Visual Studio from the New Project dialogue box, select Class Library Template from the Project Template Window. In the Name box, type a name for your Class Library, say Calculator. Then click OK.


1 Answers

Surprisingly (or unsurprisingly?) it depends.

From a developer's point of view, having the caller supply configuration via library API is preferable because it allows the library to be used in a wider variety of contexts, not dependent on how the library is being hosted. Providing a configuration loader that can pull from app domain or a private file is gravy, and reasonably usable defaults if no configuration is performed are a sanity-saver.

From an operator's point of view, it is also useful to be able to override whatever settings a developer supplied because they don't make sense in your environment. It's not clear to me how this might work if the developer is deliberately trying to force the shape of the environment on you, though.

I think the best compromise is to allow for all three possibilities, as log4net does.

like image 147
Jeffrey Hantin Avatar answered Oct 29 '22 16:10

Jeffrey Hantin