Just as an example, I created a class library (Email.DLL) that sends an email. In order to send an email, SmtpClient
needs credentials, which are currently being read from app.config.
So now I created a separate console app (SendStuff.exe) that sends an email, so I added Email.dll as reference. Being the one that created both the DLL and the console app, I know that I need to add the appropriate keys in the console app's appSettings
of app.config in order for the email to be sent.
Now, let's say that someone else uses the DLL. How would he know that his application needs to include the appropriate appSettings
so that my Email.DLL can send the email?
Or would it be better to just eliminate app.config from the class library and find some other way to set the credentials?
Using the local config file is a perfectly standard approach. Usually, you would include this information in the documentation that accompanies the library. Often, there's a Getting Started or Tutorial page.
With that said, I'd also implement clear and actionable exceptions that are thrown when these configuration values are missing. Something like:
Could not find SMTP credentials in App.config. See documentation for more information.
Lastly, if you distribute your library with something like NuGet, you can automatically include default configurations that will be set when the library is added through the package manager.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With