I know how to make pluggable things in c#. Define an Interface, Activator.CreateInstance(<class>), etc. Or I can have a code path that explicitly creates an instance of the plugged class. Many ways.
But what if the service I want to make pluggable is static (I know I could refactor so that it’s not but that’s not the point of the question)
Concrete example. I have a class that provides disk I/O abstraction (Read File, List Directory,….). Now I want different implementations of this abstraction that serves up files from , say, a real FS, a database.
Based on Olivier Jacot-Descombes reply, I will have a FileSystem class (that is real) like this
public static class FileSystem
{
static IFSImplemenation s_imple;
static FileSystem()
{
if(<some system setting>)
// converted to singleton instead of static
s_imple = new OldFileSystem()
else
s_imple = new DbFileSystem();
}
public static byte[] ReadFile(string path)
{
return s_imple.ReadFile(path);
}
...
}
To reiterate – I have a large body of code that I dont want to change so it was important to keep the calling signature the same – this solution achieves that
Use your static class as facade for non-static implementations