I have a Class library that does a lot of File IO. Its a bit difficult to test so I wanted to start using the System.IO.Abstractions package. It has an interface that you can implement with either the real file system, or a mocked one.
So when the code is running in production I want the real file system but when testing I want to mock it. My class doing IO stuff looks like this.
private readonly IFileSystem _fileSystem;
public Service(){
_fileSystem = new FileSystem(); //In test we want Mock file system here
}
internal bool Run(){
string[] sourceFilePaths = _fileSystem.Directory.GetFiles(_sourceDirectory, "*.xml", SearchOption.AllDirectories);
}
Now I want to use Dependency Injection in order to populate the _fileSystem instance in order to determine what to use. The issue is that I do not know how to do this in a Class Library. I have found tutorials but it seems that the consumer of the library have to do the injection. My problem is that this class library is packaged and uploaded to a platform. This platform cannot do anything with the package before using it. So somehow the library has to figure out itself what instance to use.
One way I can think of is using the debug notation,
#if DEBUG #endif
But of course its messy and spreads the configuration around.
What is the proper way to use DI in a Class Library?