I am trying to make use of SOLID principles for the first time. I am re-factoring a File class that stores file information and makes file operations available. This class is then inherited to specific file types to make available methods for that type.
I have started with SRP and have tried to make a FileWriter
and FileReader
abstract class to read and write from different resources. Some include:
- File System
- Streams
- Database
- Other undefined services (
SOAP
,REST
??).
I am happy to say that the file data is handled by a byte array, but I am not sure how to handle the file path/uid for a resource.
Here is what I have...
public abstract class EFileReader
{
event EventHandler<IEFileEventArgs> ReadThreadedComplete;
public abstract byte[] Read(object source);
public abstract async Task<byte[]> ReadAsync(object source);
public abstract void ReadThreaded(object source);
protected virtual void OnFileRead(IEFileEventArgs e)
{
EventHandler<IEFileEventArgs> handler = this.ReadThreadedComplete;
if (handler != null)
{
handler(this, e);
}
}
}
I could cast the object as the required type in implementation. Alternatively it could be a generic type somehow specified in implementation (not sure how).
public abstract class EFileReader<T>
{
event EventHandler<IEFileEventArgs> ReadThreadedComplete;
public abstract byte[] Read(T source);
public abstract async Task<byte[]> ReadAsync(T source);
public abstract void ReadThreaded(T source);
protected virtual void OnFileRead(IEFileEventArgs e)
{
EventHandler<IEFileEventArgs> handler = this.ReadThreadedComplete;
if (handler != null)
{
handler(this, e);
}
}
}
...Or maybe there is some abstraction of the source I can write to accommodate the various possible use cases.
I guess I could make it take an abstract FileResource
class that exposes a stream.
What is the best way to approach this?
EFileReader
...