The ideal way is to remove the configuration dependency from DLLs. A dll is designed to be used by an application and configuration belongs to application not a dll.
In most of the scenarios, you can get rid of depending on/reading from config in dll code by using dependency injection either through DI container or through manual compose.
if your search dll depend on the settings, make the settings as a dependency for your dll entry point class and proceed with dll code assuming somehow your entry point class gets it's settings.
Then, you can supply the settings value from your application, be it web/windows/console and by reading from configuration file/db/web service/file system.
sample code:
In dll:
public interface ISearcherDirectorySettings
{
string[] SearchIndexPointers { get; }
}
public class Searcher
{
private readonly ISearcherDirectorySettings _searchDirctorySettings;
public Searcher(ISearcherDirectorySettings searchDirtorySettings)
{
_searchDirctorySettings = searchDirtorySettings;
}
public void SearchAlgorithm()
{
var indexes = _searchDirctorySettings.SearchIndexPointers;
// search code
}
}
In your application:
public class SearcherDirectorySettings : ISearcherDirectorySettings
{
private readonly string[] _pointers;
public SearcherDirectorySettings(string[] pointers)
{
_pointers = pointers;
}
public string[] SearchIndexPointers
{
get { return _pointers; }
}
}
public class ApplicationRootClass //Owns configuration file
{
const string FirstPointerKey = "File1";
const string SecondPointerKey = "File2";
private Func<string, string> _getFromConfig = key => ConfigurationManager.AppSettings[key];
public ApplicationRootClass()
{
var searcherDirectorySettings = new SearcherDirectorySettings(new[] { _getFromConfig(FirstPointerKey),_getFromConfig(SecondPointerKey) });
var searcher = new Searcher(searcherDirectorySettings);
searcher.SearchAlgorithm();
}
}
With this, you can achieve "fail fast". you can have the search dlls used in any application and it will be the responsibility of the application to supply settings value.
If you end up using the dll in multiple applications/projects and duplicating the settings class code, have a utility component that does the job or move the settings class into the dll but leave the instantiation of settings class to the application.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…